By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,442 Members | 1,437 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,442 IT Pros & Developers. It's quick & easy.

Simple fwrite after getc macro question..

P: n/a
Two sections of code, in the first one fwrite works, in the second one
it doesn't (ms VC++) both are identical except in the working one fseek
is used twice to set the file pointer, once just before the fwrite.
WHY???

Works:
fseek(fp, itemloc, SEEK_SET);
tmp = ReadLongBE(fp);
if (*itemsize == tmp){
printf("\n%X\n",ftell(fp));
fseek(fp, itemloc + 4, SEEK_SET); //added to make fwrite
work.
r += fwrite(itemdata, 1, *itemsize, fp);
printf("\n%X\n",ftell(fp));
Just goes through the motions, but doesn't write (even returns proper
values!!)
fseek(fp, itemloc, SEEK_SET);
tmp = ReadLongBE(fp);
if (*itemsize == tmp){
printf("\n%X\n",ftell(fp));
//no fseek, but ftell is correct.
r += fwrite(itemdata, 1, *itemsize, fp); //but this doesn't actually
write!!
printf("\n%X\n",ftell(fp)); //but the file pointer is advanced
properly
BOTH return the same values for ftell before and after the write.
The second one doesn't wrrite, but acts like it does during the debug.

The ReadLongBE (big endian) macro:

#define ReadLongBE(Stream) ((getc(Stream)<<24) + (getc(Stream)<<16) +
(getc(Stream)<<8) + (getc(Stream)))
Thanks!
Oct 27 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Andrew Kibler <00@cwru.edu> wrote:
fseek(fp, itemloc, SEEK_SET);
tmp = ReadLongBE(fp);
if (*itemsize == tmp){
printf("\n%X\n",ftell(fp));
fseek(fp, itemloc + 4, SEEK_SET); //added to make fwrite work.
r += fwrite(itemdata, 1, *itemsize, fp);


When reading and then writing on the same stream, the C standard
requires an fseek/fsetpos/rewind in between, unless the read reached
EOF. Writing and then reading requires fflush/fseek/fsetpos/rewind in
between. (See C99+TC2 (WG14/N1124) 7.19.5.3p6.) Any given
implementation may or may not work without the fseek; yours apparently
doesn't. The usual idiom, which you might like to wrap up in a macro
or function, is fseek(fp, ftell(fp), SEEK_SET). Or, if you want to
check for errors and be able to handle file positions that don't fit
in long:
int reset_fp(FILE* fp) {
fpos_t pos;
if (fgetpos(fp, &pos)!=0) return -1;
if (fsetpos(fs, &pos)!=0) return -1;
return 0;
}
paul
Oct 27 '05 #2

P: n/a
Andrew Kibler wrote:
[fwrite after fread question redacted]
Thanks!


I don't have a copy of the C standard (only the C++ standard) available,
but I believe that's defined as required. That is, after you fread, you
*must* fseek before you fwrite.

Unfortunately, that's one of the "included by reference to C standard"
areas. Anyone got the C standard?
Oct 27 '05 #3

P: n/a
> When reading and then writing on the same stream, the C standard
requires an fseek/fsetpos/rewind in between, unless the read reached
EOF.


Thanks for your fast reply. I was using
http://www.cplusplus.com/ref/cstdio/fwrite.html
as my resource, and they don't have that requirement documented :(
Strange that you have to do that.. after all, that's the point of the
file pointer, no? Having to flush the write buffer before a read makes
sense, though. Strange that fwrite would return the correct value,
too. Oh well.
Oct 27 '05 #4

P: n/a
TIT
Andrew Kibler sade:
Two sections of code, in the first one fwrite works, in the second one
it doesn't (ms VC++) both are identical except in the working one fseek
is used twice to set the file pointer, once just before the fwrite.
WHY???


Let me quote the C99 Standard 7.19.5.3/6:

"When a file is opened with update mode [...], both input and
output may be performed on the associated stream. However,
output shall not be directly followed by input without an
intervening call to the fflush function or to a file positioning
function (fseek, fsetpos, or rewind), and input shall not be directly
followed by output without an intervening call to a file positioning
function, unless the input operation encounters end-of-file.[...]"

TIT
Oct 27 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.