0

I want to use fscanf() to read a whole line(including space) from a file. The file looks like this:

//data.txt

lek yuen
3
wo che
5
wo che
8
wo che
5
wo che
7
sha kok
0
hin keng
9
lung hang
8
sha kok
2
sha kok
4
lung hang
8

When I use buffer to store it and use printf() to show it, the result lek yuen just pops out less than 1 second and the whole cmd window disappeared. In the compiler, it shows that : Process finished with exit code 0 . Below is my code:

int main() {
    FILE *fp=fopen("data.txt","r");
    char buffer[255];

    if(fp==NULL){
        perror("File not exist!\n");
        exit(1);
       }

    fscanf(fp,"%[^\n]s",&buffer);
    printf("%s",buffer);
    fclose(fp);

    return 0;
}

Does anyone know what happened? Thank you very much!

HetaXD
  • 23
  • 5
  • 3
    The command window probably disappears because you run the program from some IDE and the program simply finished. You only read one line and print it. If you want to read more than one line, a loop would come in handy. – Gerhardh Mar 22 '20 at 18:12
  • probably you are under windows, so the windows is closed at the end of the execution, just read a char/line to block your program at the end – bruno Mar 22 '20 at 18:12
  • 1
    BTW: For `fscanf` you should use `buffer` instead of `&buffer`. It already has proper address type. – Gerhardh Mar 22 '20 at 18:13
  • also *printf("%s",buffer);* is complicated for nothing, and fsanf can write out og the buffer, limit to 254 – bruno Mar 22 '20 at 18:13
  • `fopen` may fail for many reasons other than "File not exist!". Try `if(fp==NULL) { perror("data.txt"); ...` – William Pursell Mar 22 '20 at 18:15
  • 1
    @bruno Weelll, if you cannot trust the user input it may be safer. Little Bobby Tables... – Peter - Reinstate Monica Mar 22 '20 at 18:15
  • `"%[^\n]s"` this does not make sense - Read the manual page. Also new line will still be in the buffer. See @bruno comment above – Ed Heal Mar 22 '20 at 18:15
  • See https://stackoverflow.com/questions/16952846/how-to-keep-console-window-open. If using Visual Studio, also https://stackoverflow.com/questions/1775865/preventing-console-window-from-closing-on-visual-studio-c-c-console-applicatio – Nate Eldredge Mar 22 '20 at 18:16
  • @Peter-ReinstateMonica **NEVER** trust a user input :-) – bruno Mar 22 '20 at 18:17
  • 1
    @bruno So don't tell people to use user input as a format string ;-). – Peter - Reinstate Monica Mar 22 '20 at 18:28
  • @Peter-ReinstateMonica I never said to do _printf(buffer)_ if this is what you imagine, just imagine it contains % ! The goal is just to write the string as it is, to ask _printf_ to look at the format for 'nothing' is much more complicated than to directly use _fputs_ for instance – bruno Mar 22 '20 at 18:30

1 Answers1

0

If you are concerned that the terminal window disappears:

(1) Open one manually, find the location of your executable and start it manually. That Window is almost guaranteed to not close.

(2) Alternatively you can google for the proper option to set the corresponding setting in your IDE.

(3) At the end of main(), you can try to read from the Console with a prompt "hit enter to end the program". As foolproof as this seems, I would actually discourage it: Many useful programs can read from the standard input if no file name is passed, and output to standard out. Mixing in unrelated diagnostic output, or making an essential input/processing/output program wait at a prompt, is inelegant and is making users' life harder than necessary. As a minimum, ask for input on standard error, not on standard out, and simply accept an empty line as an EOF marker, if your expected output does not have any. That empty line would then be a requirement for input on standard in which comes e.g. from a redirected file.

If you wonder why only one line is printed: It's because you only print one ;-). As Gerhard said, use a loop to read and print more than one line. In fact, with a file format like this lines are not special; simply read "words" (characters between whitespace) if you can be sure that the words do not contain whitespace, and that there are always two, then one number.

Peter - Reinstate Monica
  • 15,048
  • 4
  • 37
  • 62