0
{
    char word[MAX_WORD_LEN];
    // for (int i = 0; i < 1000; i++)
    {
        int i = 0;
        fd_array[i] = fd;
        FILE* fdd;
        fdd = fd_array[0];
        // FILE* fdd;
        // fdd = fd_array[i];
        fscanf(fdd, "%s", word);
        printf("here : %s\n",word);
        fscanf(fd_array[0], "%s", word);
        printf("here : %s\n",word);
    }
    return;

}

here fd passed into the function has the pointer to first position of file. fd_array is a global array of type FILE*.

Why are both the printf printing different things when i have here used only a copy of the file pointer

output:

here : the

here : of

the file from which i am reading is a text file with a word in each line the first two lines have the following words:

the

of

Atharva
  • 99
  • 6
  • 1
    Hello and welcome to StackOverflow! Would you mind showing us more of your code (see [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example))? – Daniel Walker May 21 '20 at 03:48
  • 1
    I think you said it yourself: you copied the **pointer**. But both copies still point to the same `FILE` object, so changes made through one are seen through the other. You can't copy the `FILE` object itself because it's opaque. If you want two independent `FILE` objects on the same file, open it twice. – Nate Eldredge May 21 '20 at 03:52
  • 1
    What he said. You may find this helpful: https://stackoverflow.com/questions/6099315/duplicating-file-pointers – Daniel Walker May 21 '20 at 03:53
  • Note that many Unix programmers use `fd` for a file descriptor (an `int`) and `fp` for a file stream (a `FILE *`). Seeing `fd` used for a file stream is mildly jarring. – Jonathan Leffler May 21 '20 at 04:21

0 Answers0