Note that there are some explanatory texts on larger screens.

plurals
  1. POfgetpos() behaviour depends on newline character
    primarykey
    data
    text
    <p>Consider these two files:</p> <p>file1.txt (Windows newline)</p> <pre><code>abc\r\n def\r\n </code></pre> <p>file2.txt (Unix newline)</p> <pre><code>abc\n def\n </code></pre> <p>I've noticed that for the file2.txt, the position obtained with <code>fgetpos</code> is not incremented correctly. I'm working on Windows.</p> <p>Let me show you an example. The following code:</p> <pre><code>#include&lt;cstdio&gt; void read(FILE *file) { int c = fgetc(file); printf("%c (%d)\n", (char)c, c); fpos_t pos; fgetpos(file, &amp;pos); // save the position c = fgetc(file); printf("%c (%d)\n", (char)c, c); fsetpos(file, &amp;pos); // restore the position - should point to previous c = fgetc(file); // character, which is not the case for file2.txt printf("%c (%d)\n", (char)c, c); c = fgetc(file); printf("%c (%d)\n", (char)c, c); } int main() { FILE *file = fopen("file1.txt", "r"); printf("file1:\n"); read(file); fclose(file); file = fopen("file2.txt", "r"); printf("\n\nfile2:\n"); read(file); fclose(file); return 0; } </code></pre> <p>gives such result:</p> <pre><code>file1: a (97) b (98) b (98) c (99) file2: a (97) b (98)   (-1)   (-1) </code></pre> <p>file1.txt works as expected, while file2.txt behaves strange. To explain what's wrong with it, I tried the following code:</p> <pre><code>void read(FILE *file) { int c; fpos_t pos; while (1) { fgetpos(file, &amp;pos); printf("pos: %d ", (int)pos); c = fgetc(file); if (c == EOF) break; printf("c: %c (%d)\n", (char)c, c); } } int main() { FILE *file = fopen("file1.txt", "r"); printf("file1:\n"); read(file); fclose(file); file = fopen("file2.txt", "r"); printf("\n\nfile2:\n"); read(file); fclose(file); return 0; } </code></pre> <p>I got this output:</p> <pre><code>file1: pos: 0 c: a (97) pos: 1 c: b (98) pos: 2 c: c (99) pos: 3 c: (10) pos: 5 c: d (100) pos: 6 c: e (101) pos: 7 c: f (102) pos: 8 c: (10) pos: 10 file2: pos: 0 c: a (97) // something is going wrong here... pos: -1 c: b (98) pos: 0 c: c (99) pos: 1 c: (10) pos: 3 c: d (100) pos: 4 c: e (101) pos: 5 c: f (102) pos: 6 c: (10) pos: 8 </code></pre> <p>I know that <code>fpos_t</code> is not meant to be interpreted by coder, because it's depending on implementation. However, the above example explains the problems with <code>fgetpos</code>/<code>fsetpos</code>.</p> <p>How is it possible that the newline sequence affects the internal position of the file, even before it encounters that characters?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload