Search code examples
pascal

Problem with file reading, stuck without output when run


I'm trying to do a program that reads numbers from a file, outputs them into a vector and then writes them. The code compiles nicely, but when run, it gets stuck with just a prompt without delivering any output.

Program LectorDeEnteros;

type 
    Arreglo = array [1..30] of integer;
var  
    //Arch:text;
    Prom:byte;
    i:integer;
    ArregloA:Arreglo;

Procedure CargadorVectorialdeArchivo (var ArregloA:Arreglo);
    var 
        Arch:text;
        i:integer;

Begin 
    assign (Arch,'Numeros.txt');
    reset (Arch);   

    i := 1;
    while not eof(Arch) do
        Write(Arch);Read(ArregloA[i]);
        i := i + 1;
End;

Begin 

    CargadorVectorialdeArchivo(ArregloA);


    for i := 1 to 14 do 
        WriteLn(ArregloA[i]:3);

End.

As i said, there are no error messages, just the prompt and no output. I have to CTRL-Z to get it out of this "loop". The expected output would be the numbers of the array, one on each line.


Solution

  • Rewrite the procedure as this:

    Procedure CargadorVectorialdeArchivo (var ArregloA:Arreglo);
        var 
            Arch:text;
            i:integer;
    
    Begin 
        assign (Arch,'Numeros.txt');
        reset (Arch);   
    
        i := 1;
        while not eof(Arch) do
           begin 
            Read(Arch,ArregloA[i]); 
            i := i + 1;
           end;
    End;
    

    Putting Arch in front of the file tells the compiler that you want to read the contents from that file, not from the keyboard.