C++ How to write strings to a Binary file?
In class weve been shown how to read from a binary file and read from a text file but Im unsure how to write text to a binary file?
Heres my attempt so far,
Can you help?
#include <iostream>
#include <stdio.h>
using namespace std;
int main()
{
FILE *namefile = NULL;
char line[35];
namefile = fopen( "s:\names.dat", "wb");
fwrite(line, sizeof( line ),35,namefile);
while (feof( namefile ) == 0)
{
cout << "Name1" << endl;
cout << "Name2" << endl;
cout << "Name3" << endl;
cout << "Name4" << endl;
cout << "Name5" << endl;
fwrite(&line, sizeof( line ),35,namefile);
}
fclose( namefile );
cin.ignore;
cin.get();
}
Thanks for any reply.
Run.[it]
Junior Poster in Training
59 posts since Jun 2007
Reputation Points: 10
Solved Threads: 1
You're lucky it doesn't compile so that you don't have an unconditional loop continually writing unknown garbage to a file.
I'd say your first step here might be to have some text to write to the file. And given your loop, I'd say skip the looping for the moment too.
What are you expecting writing text to a 'binary file' to do?
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
You're lucky it doesn't compile so that you don't have an unconditional loop continually writing unknown garbage to a file.
I'd say your first step here might be to have some text to write to the file. And given your loop, I'd say skip the looping for the moment too.
What are you expecting writing text to a 'binary file' to do?
Im trying to get a binary file to store 5 names, so I can then use said 5 names to be re-read and display in another program.
We were advised to use binary files instead of text because of error handling.
Run.[it]
Junior Poster in Training
59 posts since Jun 2007
Reputation Points: 10
Solved Threads: 1
We were advised to use binary files instead of text because of error handling.
Hm. Perhaps you misunderstood (unless I am), because that makes little sense to me.
If you have some updated code to post, if you posted it I might be able to read it.
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
Hm. Perhaps you misunderstood (unless I am), because that makes little sense to me.
If you have some updated code to post, if you posted it I might be able to read it.
I probably have, the jist of what my lecturer was hinting at was that with a textfile it is more hassel to check inputs and outputs whereas (presumably according to him) with a binary file to process with reading/writing is less obstacle ridden.
Im sorry if Im not explaining myself clearly enough, its blatant I havent got a good grasp on the difference between the two.
I'll post up the two working programs I have which are both reading of a binary and a text file.
Run.[it]
Junior Poster in Training
59 posts since Jun 2007
Reputation Points: 10
Solved Threads: 1
#include <iostream>
#include <stdio.h>
using namespace std;
int main()
{
int no = 1;
FILE *nofile = NULL;
char cont = 'n';
// read numbers from file
if ( (nofile = fopen( "numbers.dat", "rb" )) == NULL )
{
cout << "Cannot open numbers file" << endl;
exit( -1 );
}
cout << endl;
cout << "Stored numbers are" << endl;
fread( &no, sizeof( no ), 1, nofile );
while ( feof( nofile ) == 0 )
{
cout << no << endl;
fread( &no, sizeof( no ), 1, nofile );
}
fclose( nofile );
cin.ignore();
cin.get();
}
Run.[it]
Junior Poster in Training
59 posts since Jun 2007
Reputation Points: 10
Solved Threads: 1
#include <iostream>
#include <stdio.h>
using namespace std;
int main()
{
FILE *txtfile;
char line[1024];
// open text file for read only
if ( ( txtfile = fopen( "names.txt", "r" ) ) == NULL )
exit (-1);
// Prompt user
cout << "The names in the text file are:" << endl;
cout << endl;
// keep reading until end of file is reached
while ( fgets( line, 1024, txtfile ) != NULL )
cout << line;
// close file
fclose( txtfile );
cin.get();
}
Run.[it]
Junior Poster in Training
59 posts since Jun 2007
Reputation Points: 10
Solved Threads: 1
I probably have, the jist of what my lecturer was hinting at was that with a textfile it is more hassel to check inputs and outputs whereas (presumably according to him) with a binary file to process with reading/writing is less obstacle ridden.
Hm. Sounds vastly oversimplified and I am still curious about the writing of text in binary mode.
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
Hm. Sounds vastly oversimplified and I am still curious about the writing of text in binary mode.
Can you write text to a binary file, Yes or No.
Then let this thread die.
Run.[it]
Junior Poster in Training
59 posts since Jun 2007
Reputation Points: 10
Solved Threads: 1