Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problems with writing to a file 1

Status
Not open for further replies.

mattias1975

Programmer
Jul 26, 2004
36
SE
My program is supposed to write some char[] in a struct to a file.
It works but the program also add some other crap characters to the file that i dont want to have.

Why? And how do i fix it?
Thak you.

The program looks like this:

FILE *reg;
reg = fopen("/home/cproject/register.dat","w");

fputs (post.a, reg);
fputc ('\n', reg);

fputs (post.b, reg);
fputc ('\n', reg);

fputs (post.c, reg);
fputc ('\n', reg);

fputs (post.d, reg);
fputc ('\n', reg);
fputc ('\n', reg);

fclose(reg);
 
Try opening the file in binary mode, using "wb" instead of "w":

Code:
reg = fopen("/home/cproject/register.dat","wb");
 
Is there a reason you are using old style C file handling when the STL ifstream and ofstream classes could accomplish it more readably and intuitively?
 
Yes there is. I am taking a c course and are not allowed to use other stuff than this.
 
I have already tried with a binary file and a text file.
"wb" and "w" and .dat and .txt.
But i got the same problem then.
 
What does the struct look like, and what kind of crap does it put in the file?
Try stepping through the program one line at a time and keep looking at the output file to see how it looks. Identify which lines cause the crap to be added to the file...
 
Are the strings p.a, p.b, ... null-terminated? How look the definition of the struct and the assigments of values to these strings?

cpjust:
Try stepping through the program one line at a time and keep looking at the output file to see how it looks
The output file buffer will not be flushed at that time and you will be able to see output data only later. Try better to watch values of the strings - are they like you expected?
 
Really no true problem explanation. What is it:
but the program also add some other crap characters to the file that i dont want to have?..
 
Oh yeah, that's right, he's using fopen()... instead of just open().
BTW, what do you mean by 'p.a, p.b'?
 
mattias1975: rather than just saying its fixed, it would be better to put in what the problem was, as this might help others that have a similar problem and come across this thread while searching.
 
Ok. Well.

I think it works like this.
When i use the functons that starts with an f.
For example fopen, fwrite, ....
The .bin file is unreadable if i open it in a texteditor.
But that does not really matter, because when i use the method fread i get the information as it looked like when i wrote it to the file with fwrite.

But i have used the methods open, write and read many years ago and i remember that then the file was readable in a texteditor.
There might also be a diffrence between textfilen and binary files.
I dont remember if i used a .txt or .dat or .bin file then.
But i have not examined that yet.
I also know that the c++ stream functions write the information to the file so that it is possible to read it in a texteditor.

I hope you are satisfied with my answer. It was at least all i needed to know for now to be able to continue with my project.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top