B
Brady
Hi,
I'm having a problem reading and writing to a file. What I'm trying
to do is read a file, modify the portion of the file that I just read,
and then write the modified data back to the same location in the
file.
What is happening, is I can read the file, either in entirety or only
part of it. And no matter what I try setting the position to, using
fsetpos, it always gets set back to the very beginning of the file. I
wrote sample code which demonstrates this.
#include <stdio.h>
int main()
{
FILE* input= fopen("test.dat", "rb+");
int read = 0;
while(!feof(input))
{
fread(&read, sizeof(int), 1, input);
}
fpos_t pos = 10; /* some random location in the file, 10 used as
example */
fsetpos(input, &pos);
int temp = 3;
fwrite(&temp, sizeof(int), 1, input);
fclose(input);
return 0;
}
When I call fsetpos(input, &pos), I am watching the values of the
FILE*, input. No matter what the value of pos is, the _ptr member of
input, gets set to the same value of _base.
I've read the posts saying you need to use an fsetpos, rewind, or
fseek, to change from read or write back into undetermined mode, and I
don't see why I'm having this problem.
Any help would be greatly appreciated. This problem has been plaguing
me for the last week.
Thanks,
Bryan
I'm having a problem reading and writing to a file. What I'm trying
to do is read a file, modify the portion of the file that I just read,
and then write the modified data back to the same location in the
file.
What is happening, is I can read the file, either in entirety or only
part of it. And no matter what I try setting the position to, using
fsetpos, it always gets set back to the very beginning of the file. I
wrote sample code which demonstrates this.
#include <stdio.h>
int main()
{
FILE* input= fopen("test.dat", "rb+");
int read = 0;
while(!feof(input))
{
fread(&read, sizeof(int), 1, input);
}
fpos_t pos = 10; /* some random location in the file, 10 used as
example */
fsetpos(input, &pos);
int temp = 3;
fwrite(&temp, sizeof(int), 1, input);
fclose(input);
return 0;
}
When I call fsetpos(input, &pos), I am watching the values of the
FILE*, input. No matter what the value of pos is, the _ptr member of
input, gets set to the same value of _base.
I've read the posts saying you need to use an fsetpos, rewind, or
fseek, to change from read or write back into undetermined mode, and I
don't see why I'm having this problem.
Any help would be greatly appreciated. This problem has been plaguing
me for the last week.
Thanks,
Bryan