I am trying to write binary "raw" data to a file and so far I am able to do that. However, I am having a bit of trouble trying to display the data from the file in the program. Can anyone tell me what i am doing wrong?
#include <iostream>
#include <fstream>
usingnamespace std;
struct Data
{
int number;
string sentence;
char character[50];
}object;
int main ()
{
fstream file;
file.open ("example.txt" , ios::binary | ios::in | ios::out | ios::app);
cout << "Input a number to the file" << "\n";
cin >> object.number;
cout << "Input an entire sentence to the file" << "\n";
cin.ignore ();
getline (cin,object.sentence);
cout << "Input a bunch of random characters to the file" << "\n";
cin.getline(object.character,50);
file.write (reinterpret_cast <char *> (&object), sizeof(object));
cout << "Here is what you stored in the file" << "\n";
file.read (reinterpret_cast <char *> (&object), sizeof(object));
while(!file.eof()) //this loop will not display the content of the file
{
cout << object.number << "\n";
cout << object.sentence << "\n";
cout << object.character << "\n";
}
file.close();
}
Ok so I've looked at your link and removed the string from my data structure, but for some reason, the program still will not display the contents of the file..
#include <iostream>
#include <fstream>
usingnamespace std;
struct Data
{
int number;
char sentence;
char character;
}object, object1;
int main ()
{
string line;
fstream file;
file.open ("example.txt" , ios::binary | ios::in | ios::out | ios::app);
cout << "Enter a number to the file" << "\n";
cin >> object.number;
cout << "Enter a sentence to the file" << "\n";
cin >> object.sentence;
cout << "Enter a single character to the file" << "\n";
cin >> object.character;
file.write (reinterpret_cast <char *> (&object), sizeof(object)); //writing the contents of object to the file
cout << "Here is what you stored in the file" << "\n";
file.read (reinterpret_cast <char *> (&object1), sizeof(object1));
cout << object1.number << "\n";
cout << object1.sentence << "\n";
cout << object1.character << "\n";
file.close();
}
The big thing is that you do not reposition the file cursor after writing.
After line 32, your data is written to the file, but the file pointer is now at the end of the file (after everything you just wrote). So when you attempt to read, you are trying to read from the end of the file... which gets you nothing.
After writing, you need to seek back to the start of the file so that the data can be read:
1 2 3
file.write( ...whatever... );
file.seekg( 0 ); // seek back to the beginning of the file
file.read( ...whatever... );
Also note: Since your 'sentence' var is now a char, it can only contain a single character, not a full sentence.
#include <iostream>
#include <fstream>
#include <string>
usingnamespace std;
int main()
{
int number;
string sentence;
char character;
string line;
fstream file;
file.open("example.txt", ios::binary | ios::in | ios::out | ios::app);
cout << "Enter a number to the file" << "\n";
cin >> number;
cout << "Enter a sentence to the file" << "\n";
cin >> sentence;
cout << "Enter a single character to the file" << "\n";
cin >> character;
file.write(reinterpret_cast <char *> (&sentence), sizeof(sentence)); //writing the contents of object to the file
cout << "Here is what you stored in the file" << "\n";
cout << number << "\n";
cout << sentence << "\n";
cout << character << "\n";
file.close();
}