why won't my program display the contents of a file?

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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <fstream>

using namespace 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();
}
Your 'Data' struct contains a string... and therefore it is not a POD type... and therefore you cannot dump it to a file like that.

I've gone over this numerous times in the past. Check this post for some info:

http://www.cplusplus.com/forum/beginner/116010/#msg633357
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..

here is my updated program.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <fstream>

using namespace 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();
}




What am i doing wrong?
Last edited on
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.
the data value char only allows 1 charactor in the sentence so i you inputed "hi how are you" you would get (char sentence = h)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <fstream>
#include <string>

using namespace 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();
}
1
2
3
string sentence;
...
file.write(reinterpret_cast <char *> (&sentence), sizeof(sentence));	//writing the contents of object to the file 


You cannot do this. Strings are not POD types and cannot be written raw to a file. See my earlier post.
Last edited on
Topic archived. No new replies allowed.