Solved

fwrite problem

Posted on 1997-03-21
2
310 Views
Last Modified: 2012-05-04
Though a Windows program, for compatibility reasons and a greater simplicity with pointers, I am using fwrite to write to files with VC 4.2, with all patches. The problem I am having is that I am getting a corrupt file when writing to it. What happens is that I read the file, do an fseek to specific offset and write to that position. This works fine in all other cases, except that I seem to be writing more than I have indicated with this line of code:

       fwrite (&buffer, sizeof(char), 3, prt);

Once again, I have an fseek just before this (which is working ok) and then write the contents of the buffer. So I shouldn't have a corrupt file problem. I used fopen with r+b, so read-write binary files. That's ok.

I've tried everything possible to solve the problem, but no luck; it's still corrupt! Your suggestions will be greatly appreciated!

Roger
0
Comment
Question by:RJV
2 Comments
 

Accepted Solution

by:
syed_dgreat earned 70 total points
ID: 1162184
You should have mentioned in greater detail by what you mean
by corrupted file.? Is it unreadable or is the content just plain garbage.?
BTW, I notice someting not quite right (not really sure though).
The line
fwrite (&buffer, sizeof(char), 3, prt);
suggests to me that buffer is not a string (probably an int or
non-pointer variable).
if it's a string, it should be like this:-
fwrite(buffer, ....);
or if it's integer,
fwrite(&buffer, sizeof(int), ...);
This is probably the bug!! If that's so, it probably the one causes the corrupted file.
0
 

Author Comment

by:RJV
ID: 1162185
Indeed, my example is wrong, though the & character is ignored. More characters seem to be written to the file, which is text but opened as binary due to extended characters (above 128 decimal, or 7fh. I have to go the a part of the text file and change some control characters there, but more than those characters are changed. This despite the fact that writing is limited to 3 characters. I haven't managed to find the problem. At this stage I'll try other ways to access the file, particularly as this has never happened before!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question