-6

Good day, here's my code :

FILE* fp;
fp = fopen("a.txt","r+");
if (fp != NULL){
    int c = 0;
    while(!feof(fp)) {
        char k = fgetc(fp);
        if (k == '$') {
            c = 0;
            printf("inside '%c' %d\n", k, c);
            continue;
        }
        c++;
        printf("outside '%c' %d\n", k, c);
    }

This is the file :

123456$test$pan$test$

The output :

outside '1' 1
outside '2' 2
outside '3' 3
outside '4' 4
outside '5' 5
outside '6' 6
' 7side ' //*
inside '$' 0
outside 't' 1
outside 'e' 2
outside 's' 3
outside 't' 4
' 5side ' //*
inside '$' 0
outside 'p' 1
outside 'a' 2
outside 'n' 3
inside '$' 0
outside 't' 1
outside 'e' 2
outside 's' 3
outside 't' 4
inside '$' 0
outside ' ' 1 //*

I am failing to understand what is happening at //*

Thanks for your help!

GDGDJKJ
  • 209
  • 1
  • 7
  • 2
    Don't post images of code and don't post links to code. Your question should contain a [mcve] – UnholySheep Jan 05 '19 at 21:41
  • done, done! my bad – GDGDJKJ Jan 05 '19 at 21:50
  • There seems to be some CR in the text file that is not shown in the editor. Is it an editor on Windows and a file with Linux EOL? Did you view your file in a hex viewer? – Gerhardh Jan 05 '19 at 21:52
  • 1
    https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong – Shawn Jan 05 '19 at 21:52
  • This is a good chance to start learning to use a debugger. Step through your code and inspect the values of your variables – Gerhardh Jan 05 '19 at 21:52
  • @Luyw I think the text is in UTF8 or something like that, notepad manages that but of course not fgetc, this is my proposal in the answer – bruno Jan 05 '19 at 21:53
  • @Gerhardh yes, there's got to be CR. But Linux EOL is LF. – Antti Haapala -- Слава Україні Jan 05 '19 at 21:54
  • actually there were three lines in the file not one, tricky notepad, sublime text revealed them. – GDGDJKJ Jan 05 '19 at 21:54
  • @AnttiHaapala OK, I always mix them. ;) Thanks. – Gerhardh Jan 05 '19 at 21:54
  • @bruno of course it is UTF-8, it is also ASCII. Carriage return characters, ascii 13. – Antti Haapala -- Слава Україні Jan 05 '19 at 21:55
  • @Luyw that is not exactly correct either, there is just one line, would be true for both Linux and Windows alike - or 3 incorrectly terminated lines. – Antti Haapala -- Слава Україні Jan 05 '19 at 21:55
  • 1
    Not an answer to your question, but some other problems with your code: 1. `while (!feof(fp))` is bad. `feof` will return non-zero only *after* you've attempted to read from the `FILE*` *and failed*. 2. `char k = fgetc(fp);` is bad because `fgetc` returns an `int`, not a `char`. `fgetc` returns `EOF` on failure, which you aren't checking for (and can't check for since you truncate the return value to `char`. Instead, your loop should be `int k; while ((k = fgetc(fp)) != EOF) { ...`. – jamesdlin Jan 05 '19 at 22:02
  • Also, as a stylistic issue, it's really confusing/unreadable that you have a variable named `c` that *isn't* used to store the character returned by `fgetc` and isn't used as the argument to the `%c` format specifier. – jamesdlin Jan 05 '19 at 22:03

1 Answers1

0

I think you have non printable characters before each '$' in a.txt even there are not indicated through notepad, as you can see the count written reach 7 then 5 etc, this is not possible if the text is the one we see

a.txt contains only ascii characters or is UTF8 or something like that ? notepad can manage the encoding, but of course fgetc don't

bruno
  • 32,421
  • 7
  • 25
  • 37
  • Actually you're true, I have just opened the .txt with sublime text and there were three lines not one, notepad showed only one though. – GDGDJKJ Jan 05 '19 at 21:53