Refer to my post #4. Notice that I declared the functions with parameters.Quote:
Originally Posted by Williham
In the context of your function, fptr was not declared.Quote:
Originally Posted by Williham
Indeed, and that is why your print_numbers implementation is wrong in its approach. How can you print what you have not read? So, what you end up doing is reading and printing, but why read from the file so many times when you only need to read once?Quote:
Originally Posted by Williham
The way you use fscanf is wrong. fscanf returns the number of items read, or EOF on read failure. This is how I would implement read_numbers:Quote:
Originally Posted by Williham
Notice that when I call fscanf, I provide a pointer to numbers[size] so that fscanf can write the integer value read to numbers[size]. Then, I check that fscanf returns 1 because that means that fscanf did indeed read an integer.Code:size_t read_numbers(FILE *fp, int numbers[], size_t max_size)
{
size_t size = 0;
while (size < max_size && fscanf(fp, "%d", &numbers[size]) == 1)
{
++size;
}
return size;
}
Implement print_numbers according to my proposed function declaration. If your function does not take any arguments, it is wrong. Period.
EDIT:
In your thread Trying to put numbers from a txt file into an aray, without a huge amount of luck!, Salem mentioned the functions rewind and fseek to you, with good intentions. However, you are wholly unprepared to use them properly. At least until you are done with this particular program, ban yourself from using rewind and fseek.