Solved

fwrite problem

Posted on 1997-03-21
2
287 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

759 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now