Solved

fwrite problem

Posted on 1997-03-21
2
312 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

739 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