I have a question about the fseek function. Everything arises from seeing the following sentence:
fseek(f, (-1)*sizeof(int),SEEK_CUR);
Which I thought would generate an error. However I see that you can multiply by a negative number. But what exactly does the compiler interpret? Return to the beginning of the file?
The complete code is the following.
int main()
{
FILE *f;
int x;
int y = 1000;
f = fopen("prueba.laquequieras","wb");
int vec[] = {91, 92, 93, 94, 95, 96, 97, 98, 99, 100};
fwrite(vec, sizeof(int), 10, f);
fclose(f);
f = fopen("prueba.laquequieras", "rb+");
fseek(f,0,SEEK_SET);
fread(&x, sizeof(int), 1, f);
fread(&x, sizeof(int), 1, f);
fread(&x, sizeof(int), 1, f);
fseek(f, (-1)*sizeof(int),SEEK_CUR); //Duda.
fwrite(&y, sizeof(int),1,f);
fseek(f,0,SEEK_SET);
fread(&x, sizeof(int), 1, f);
while(!feof(f))
{
cout << x << ";";
fread(&x, sizeof(int), 1, f);
}
fclose(f);
return 0;
}
and the output is
Where would the cursor move?
The function
std::fseek
is part of the C language libraries ported to C++, it is included in the header<cstdio>
which is the C++ equivalent of<stdio>
, the C Input/Output header.The function
std::fseek
has the following input parameters:std::FILE
.So this call:
It could be translated as: