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
Solved

fwrite problem

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Prototype for MessageDlg in Embacadero XE 2 2 42
java inner class, for the sole use of parameter passing 21 77
sorting efficency of sorting algorithm 30 121
max float value 3 51
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

856 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