473,569 Members | 2,799 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Simple fwrite after getc macro question..

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(Stre am) ((getc(Stream)< <24) + (getc(Stream)<< 16) +
(getc(Stream)<< 8) + (getc(Stream)))
Thanks!
Oct 27 '05 #1
4 2608
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
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
> 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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

51
8233
by: Alan | last post by:
hi all, I want to define a constant length string, say 4 then in a function at some time, I want to set the string to a constant value, say a below is my code but it fails what is the correct code? many thx!
13
15106
by: William L. Bahn | last post by:
I'm sure this has been asked before, and I have looked in the FAQ, but I'm looking for an explanation for the following: The functions pairs: gets()/fgets() puts()/fputs() printf()/fprintf() scanf()/fscanf()
8
5099
by: Ross A. Finlayson | last post by:
I'm trying to write some C code, but I want to use C++'s std::vector. Indeed, if the code is compiled as C++, I want the container to actually be std::vector, in this case of a collection of value types or std::vector<int>. So where I would use an int* and reallocate it from time to time in C, and randomly access it via , then I figure to...
11
3465
by: TTroy | last post by:
Hello C programmers, Can someone tell me why ungetc can't sent back EOF, but it's sister function getc has no trouble sending it to us? For a file, this might not make a difference, but for an interactive terminal, it is probably nice to push EOF back (because to user doesn't want to provide an EOF twice). How is it getc can send EOF...
11
4280
by: David Mathog | last post by:
In the beginning (Kernighan & Ritchie 1978) there was fprintf, and unix write, but no fwrite. That is, no portable C method for writing binary data, only system calls which were OS specific. At C89 fwrite/fread were added to the C standard to allow portable binary IO to files. I wonder though why the choice was made to extend the unix...
27
5077
by: Jeff | last post by:
Im trying to figure out why I cant read back a binary file correctly. I have the following union: #define BITE_RECORD_LEN 12 typedef union { unsigned char byte; struct { unsigned char type; /* 0 Type */ unsigned char sn; /* 1-3 Serial Number */
25
15531
by: Abubakar | last post by:
Hi, recently some C programmer told me that using fwrite/fopen functions are not efficient because the output that they do to the file is actually buffered and gets late in writing. Is that true? regards, ...ab
3
4956
by: ANURAGBAJPAI | last post by:
The getc() is a macro while fgetc() is a function in the stdio.h library.Please tell me what is macro and explain the sentence.
3
9620
Parul Bagadia
by: Parul Bagadia | last post by:
getc gets the charachter from file stream and points to next charachter, fgetc gets the charachter from file stream and i don't know whether it points to next charachter or not; is that a significant change? In the help menu of Turbo C, it's given that its a function version of getc macro. what is a function version?
0
7612
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7924
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8120
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7968
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6283
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5512
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3640
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.