[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

fwrite problem

Posted on 1997-03-21
2
Medium Priority
?
316 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 210 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

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 …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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 use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

650 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