I have a exist file named "Test.txt" which contents a simple string "0123456789".
Look at the code,
1.open the file in the mode "r+" //read and write
2.read 3 bytes from the file
3.at the the 3rd byte position, write 10 bytes
==>Problem:just can write 7 bytes to file, and the file not changed.
#include <stdio.h>
int main(void)
{
FILE* fp = fopen("Test.txt", "r+");
char ReadBuff[3] = {0};
int ReadLength = fread(ReadBuff, 1, 3, fp); int FpPos = ftell(fp); char WriteBuff[10] = {0};
char* Str = "abcdefghij"; int WriteLength = fwrite(Str, 1, 10, fp);
fclose(fp);
return 0;
}
What I have tried:
After reading from the file, If I call fseek() to reset the file descriptor, the code works well.