0

Hello i got a problem im trying to solve problem and learn how the learning from files works .I did this code by tutorial and when i execute instead of learning my file and write on console something like 1 4 6 5 1 etc.. its just spam only 0 0 0 0 0 0 0 and then repeat If you would say me where is the problem it will be good thanks for ur time :)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


#define UNUSED(__ARG__) ((void)__ARG__)

int main(int argc, char** argv)
{
    UNUSED(argc);
    UNUSED(argv);

    int i = 0;
    FILE* x =fopen("cisla.txt","r");
     fscanf(x,"%d",&i);
 while (!feof (x))
 {
     printf("%d",i);
     fscanf(x,"%d",&i);


 }
 fclose(x);


return 0;
}
  • 1
    NB: https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong – Govind Parmar Feb 08 '19 at 17:37
  • while (!feof (x)) Is that right? Shouldn't it be eof? Also, does x need to be a pointer? https://stackoverflow.com/questions/12337614/how-feof-works-in-c – Millar248 Feb 08 '19 at 17:38
  • 1
    @Millar248 You should probably take a step back and read a book or tutorial. Because yes it's right (but not correct) and yes it must be a pointer. – Some programmer dude Feb 08 '19 at 17:42

1 Answers1

3

Whatever tutorial this is, throw it out.

First, you need to check whether your file operations succeeded, otherwise the program will blindly continue along. Likely the fopen failed. It returns NULL on failure so you can check for that and get an error message with perror.

FILE *x = fopen("cisla.txt","r");
if( x == NULL ) {
    perror("Could not open the file");
    exit(1);
}

Then, as others have mentioned, you don't check for end of file. Instead do the IO operation and check whether it succeeded or failed. In this case, fscanf returns the number of matched items which should be 1.

while ( fscanf(x, "%d", &i) == 1 ) {
    printf("%d",i);
}

Note that the scanf family is fraught with gotchas. But you'll get to them later.

Finally, that UNUSED stuff is very clever and totally unnecessary. Just declare main with no arguments. This is perfectly valid.

int main() {
Schwern
  • 153,029
  • 25
  • 195
  • 336
  • @RábelDavid `fscanf(x, "%d", &i)` will only read integers. `while ( fscanf(x, "%d", &i) == 1 )` means the loop will stop once it sees something that is not an integer. What's in the file? Since you're just starting out it might be simpler to use [`fgets`](https://www.tutorialspoint.com/c_standard_library/c_function_fgets.htm) rather than `fscanf` and just read lines out of the file without trying to process them. – Schwern Feb 08 '19 at 23:47
  • Oh then it will stop when i have space between numbers because it read space like \ ? i would like to read text file which contains only numbers now cuz i want to get how it works . Then in file is just this 94643134 nothing more already But still dont have idea why it writes on console Zav for no reason – Rábel David Feb 09 '19 at 00:23
  • @RábelDavid It will handle spaces for you. What is "console Zav"? – Schwern Feb 09 '19 at 04:49
  • Oh ok nice . I dont have idea. When i execute its just write ZAV insted of my list – Rábel David Feb 09 '19 at 09:48
  • Im using QT creator :) – Rábel David Feb 09 '19 at 13:18
  • Ok i found the main problems first one Zav = End of program in QT And it works now but i have to use whole path like C:/Users/Rabeles/Desktop/ADS_1ukol/task1-master/input.txt If i use just "input.txt" it doenst work it pass throught test and open file but then it didnt write anything. do you have some ideas why? when it pass over the test if x==null? – Rábel David Feb 09 '19 at 13:29
  • @RábelDavid Without seeing your source code and having never used QT Creator, I can only guess. Opening `input.txt` will look for it in ***the directory where the program is run***. Since you're running the program via an IDE I don't know what directory that is. You can find out with [`getcwd`](https://stackoverflow.com/q/298510/14660). If you use an absolute path there is no ambiguity. – Schwern Feb 10 '19 at 02:25