0

im pretty new to programming i had this issue, i have writtern this program to read the formated data from a text file named "num.txt" and the info in that file is :

raju : 12  
lokesh : 11  
dinesh : 10
#include<stdio.h>
int main()
{
    FILE *f1;
    f1=fopen("D:/num.txt","r");
    int mark;
    char name[20];
    while(!feof(f1))
    {
        fscanf(f1,"%[^:]:%d",name,&mark);
        printf("the %s marks are %d",name,mark);
    }
}

and the output put is:

the raju  marks are 12th
lokesh  marks are 11the 
dinesh  marks are 10

Here, i can clearly understood that the first '\n'(0f the first line) in the text file is being in the buffer and been taken by the second %s string,now i tried to flush the buffer by using the fflust & also thw while(getchar()) method but no use. My main problem here is lack of undersanding of , how the new line '\n' is actually kept in the text file, for every new line.And how newlines are handled..

Im sorry that, i may have not expressed the question in a proper way but as i said i new and learning even ENGLISH.

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
  • 2
    Who taught you that? Please see [Why is `while ( !feof (file) )` always wrong?](http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – Weather Vane Apr 30 '22 at 07:39
  • The `fscanf` conversion stops at the first character it cannot convert, which is typically (but not necessarily) a space or a newline, and that character remains in the input buffer. It will be read by the *next* `fscanf()`. Most format specifiers automatically filter leading whitespace characters from the input, but `%c` and `%[]` and `%n` do not. You can instruct `fscanf` to do so by adding a space just before the `%`. – Weather Vane Apr 30 '22 at 07:41
  • So, `"%[^:]:%d"` does not filter the newline that was left in the buffer, it would need to be `" %[^:]:%d"` with the added space. Note too, that `fflush` cannot be used on an input stream. You only need to flush the stream if there is a character blocking it. – Weather Vane Apr 30 '22 at 07:44
  • hooooo, that's cool and now i got to understand this tq so much,and if possible can i know how to know more about what said in the documentaion or somewhere , because i want to know more about that// – varun prasad Apr 30 '22 at 07:46
  • There are other things wrong with the code, for example `char *name` has no memory allocation. – Weather Vane Apr 30 '22 at 07:46
  • Spend an hour reading about the `scanf` family and playing with test code. It is really quite tricky to use. Some say avoid using it at all. – Weather Vane Apr 30 '22 at 07:47
  • sorry, i got that wrong about char *name , now i changed .and tq so much for u r time.. – varun prasad Apr 30 '22 at 07:49
  • Welcome to StackOverflow! Please don't edit the code on-the-fly, just copy/paste the exact code that you had. It isn't a a site for live running progress updates. – Weather Vane Apr 30 '22 at 07:50
  • sorry,will do from next time. – varun prasad Apr 30 '22 at 07:51
  • If there is a space each side of the `:` in the file, the code will fail. You'll find that `name` will contain a final space, though it might be hard to spot. – Weather Vane Apr 30 '22 at 07:52
  • sorry to prolong this but ,as you said when we add the **' '** before the ** %s ** then it will avoid the '\n' for the next scanf ,but we gave **' '** to look out the ' ' but what we are actually getting is newline '\n' na . so ( scanf cosider the both the space and the newline as same ? and if so , as there will be three iterations in my code na, the from where will the ' ' **space** will come for that .(as we know if somthing doesn't match in scanf fuction it will faill. – varun prasad Apr 30 '22 at 08:01
  • Space and newline etc are all 'whitespace'. A space in the format string will filter *any amount* of whitespace from the input stream, Formats `%s` and `%f` and `%d` do this automatically. Formats `%c` and `%[]` do not: they read *every character*. Don't try to filter the *next* newline - see [What is the effect of trailing white space in a scanf() format string?](https://stackoverflow.com/questions/19499060/what-is-the-effect-of-trailing-white-space-in-a-scanf-format-string) – Weather Vane Apr 30 '22 at 08:04

0 Answers0