C++ super confused..

Hey guys, i am having a very bizarre error with my code for my homework assignment and i'm not experienced enough to know what the heck is going on.

Basically its spitting out the correct output for the text mode array. And a bunch of garbage for the binary array. But if i change the text name to something like "B\\students", which i actually did on accident i just missed the colon, but anyways i noticed it then spits out garbage for the text array and the binary one is perfect!

I am super confused i've tried searching around forums a bit but everything is so specific i'm really having trouble understanding what is going on!

Thanks for any advice you can give!

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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include "ConsoleApplication36.h"
using namespace std;

// define structure studentInfo
struct studentInfo
{
	char name[20];
	int age;
	float gpa;
	char grade;
};

int main()
{
	//create array of structures
	studentInfo student[4] =
	{ { "Ann Annson", 10, 1.10, 'D' },
	{ "Bill Billson", 20, 2.20, 'C'},
	{ "Carl Carlson", 30, 3.30, 'B'},
	{"Don Donson", 40, 4.40, 'A' } };

	// Create fstream file.
	fstream dataFile;

	// Open fstream file in out mode.
	dataFile.open("students.txt", ios::out);

	// Write array to text file.
	for (int i = 0; i < 4; i++)
	{
		dataFile << student[i].name << "\n"; //writing line 1, name
		dataFile << student[i].age << "\n";  // writing line 2, age
		dataFile << student[i].gpa << "\n";  // line 3, gpa
		dataFile << student[i].grade << "\n"; // line 4, grade
	}
	dataFile.close();

	// Write the array of structures to binary file.
	dataFile.open("students.bin", ios::out | ios::binary);
	dataFile.write(reinterpret_cast<char *>(&student), sizeof(student));
	dataFile.close();

	// Declare two more arrays of structures for reading from files.
	studentInfo studentsText[4];
	studentInfo studentsBinary[4];

	// Read text file to the array, studentsText
	dataFile.open("students.txt", ios::in);
		int i = 0;
		while (dataFile)
		{
			dataFile.getline(studentsText[i].name, 20);
			dataFile >> studentsText[i].age;
			dataFile >> studentsText[i].gpa;
			dataFile >> studentsText[i].grade;
			if (dataFile) // increment count only if file access was successful
				i++;
			dataFile.ignore(); // remove trailing newline 
		}

	// read the binary file to studentsBinary array of structures.
	dataFile.open("students.bin", ios::in | ios::binary);
	dataFile.read(reinterpret_cast<char *>(&studentsBinary), sizeof(studentsBinary));
	dataFile.close();

	// Output studentsText.
	cout << "Students Text" << endl;
	cout << "-------------" << endl;
	for (int i = 0; i < 4; i++)
	{
		cout << studentsText[i].name << endl;
		cout << studentsText[i].age << endl;
		cout << studentsText[i].gpa << endl;
		cout << studentsText[i].grade << endl;
	}

	// Output studentsBinary.
	cout << "Students Binary" << endl;
	cout << "---------------" << endl;
	for (int i = 0; i < 4; i++)
	{
		cout << studentsBinary[i].name << endl;
		cout << studentsBinary[i].age << endl;
		cout << studentsBinary[i].gpa << endl;
		cout << studentsBinary[i].grade << endl;
	}

	return 0;
}
You should check that the file openings succeed.

1
2
3
4
5
6
7
8
9
10
	// Create fstream file.
	fstream dataFile("students.txt", ios::out);

        if(dataFile)
        {
             std::cerr << "students.txt failed to open for text output.\n";
             return 1;
         }




At my I also got garbage at the output from the binary file in binary mode using your code.
But then I tried it by using two different file streams, one for binary and one for text, and that works fine.
Well, now found your error: you forgot to close the dataFile (should done at line 64).

As @jib mentioned, you should always check whether a file could successfully opened, but of course branch for errors with
if (!dataFile) { ...
Last edited on
Wow it's always something simple like that.. thank you! I need to work on my attention to detail.
Topic archived. No new replies allowed.