This is my program:
#include <iostream>
int main()
{
// open file1.txt
FILE* file1;
fopen_s(&file1, "file1.txt", "wb+");
// write array of 5 integers to file.txt
int array1[5] { 1, 2, 3, 4, 5 };
for (int i = 0; i < 5; i++)
{
fwrite(array1, sizeof(array1[0]), 5, file1);
}
fseek(file1, 0, SEEK_SET);
int tempValue;
fread(&tempValue, sizeof(tempValue), 1, file1);
// fseek(file1, 0, SEEK_CUR);
fwrite(&tempValue, sizeof(tempValue), 1, file1);
}
At runtime the program crashed with informatoin:
> Expression ("Flush between consecutive read and write.",
> !stream.has_any_of(_IOREAD))
But if I uncomment fseek(file1, 0, SEEK_CUR);
everything would be fine considering file pointer has not been moved. So why is that?
I use Visual Studio 2019
P.S. Why this works just fine?
#include <iostream>
int main()
{
FILE* file1;
fopen_s(&file1, "data.txt", "wb+");
int value = 7;
fwrite(&value, sizeof(value), 1, file1);
fwrite(&value, sizeof(value), 1, file1);
fseek(file1, 0, SEEK_CUR);
fwrite(&value, sizeof(value), 1, file1);
fread(&value, sizeof(value), 1, file1);
}