Don't know if it is duplicate, if so, mark it ( I havennot found answers anywhere).
Having this:
#include <stdio.h>
#include <stdlib.h>
int main (void) {
FILE *fp = fopen("txt2", "r+"); // does not help a+ or w+
fprintf(fp,"20");
fseek(fp,1,SEEK_CUR);
fprintf(fp,"19");
for(char c;(c=getc(fp))!=EOF;)
putchar(c);
fclose(fp);
return 0;
}
Cannot write to the file, and at the end view the file via putchar. how to open file to read as well as read? (I have tried r+,w+,a+
, non helped). Still confused with their differencies(r+/w+
- both are positioned at the beginning of file, so in which differ? only a+
makes sense, as only it writes at the end (after everything else) in the file).
I don't know it the (r/w)+ mode makes some effect, since noone has yet explained the differences and usecases, but I have changed the mode from r+
to w+
and the offset of fseek
:
FILE *fp = fopen("txt2", "w+");
...
fseek(fp,10,SEEK_CUR);
but, the output is 2019
instead of 20 19
. So does that mean fseek
trims white spaces? or why is not the desire output?
and it