Here is the code:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* argv[])
{
FILE* fp = fopen("sample.txt", "w");
if (fp == NULL)
return 1;
int error = 0;
if ((error = fseek(fp, -5, SEEK_END)) != 0)
{
printf("Error fseek failed: %d\n", error);
fclose(fp);
return 1;
}
fprintf(fp, "%d", 1);
fclose(fp);
return 0;
}
My sample.txt file:
adsfasdfasdfasfasfasfasdfasfasfasfas
asdfasdfasdfasdfasfadsfsadfadsfsafad
In the above program fseek return -1 and makes the file blank and same with "w+" mode. The same program works in "r+" correctly. I want to know what is the relation between fseek and file modes in C language? I try to search but I found nothing on the web till now.