fwritef and fgets operating on one FILE pointer

Is this possible? If not, why not?

For example, of the following two programs, only the first works:

FIRST

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <stdio.h>

int main ()

{

    FILE *rFile;
    FILE *wFile;

    char mystring [100];
    char Deutsch[] = "Etwas Text.";

    rFile = fopen("readfile.txt", "r");
    wFile = fopen("writefile.txt", "w");

    fprintf(wFile, "%s", Deutsch);

    fgets (mystring, 100, rFile);
    puts (mystring);

    fclose (rFile);
    fclose (wFile);

    return 0;
}


SECOND

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/* fgets example */

#include <stdio.h>

int main ()

{

    FILE *rwFile;

    char mystring [100];
    char Deutsch[] = "Etwas Text.";

    rwFile = fopen("readwritefile.txt", "w+");

    fprintf(rwFile, "%s", Deutsch);

    fgets (mystring, 100, rwFile);
    puts (mystring);

    fclose (rwFile);

    return 0;
}
Topic archived. No new replies allowed.