Writing data to txt file

I've been going through a c++ book and came across this practice problem in which part of the problem involves writing the arrays into a txt document. I have this so far, it creates the file but is empty when i open it. I'm extremely new to this so explanation would be greatly appreciated. Thank you!


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
#include <iostream>
#include <fstream>
using namespace std;

struct studentInfo
{
	char name[30];
	int age;
	float gpa;
	char grade;
};


int main()
{
  studentInfo s[4] = {{"Dan Jonson", 10, 1.10, 'A'},
		      {"Billy Bobson", 15, 2.20, 'B'},
	              {"Jim Carlton", 20, 3.30, 'C'},
		      {"Donnie Darko", 250, 4.00, 'D'}};
	fstream fFile;

	fFile.open("c:\\windows\\temp\\students.txt", ios::out);
	
	if(!fFile)
		cout << "ERROR";

	for(int x=0; x<4; x++)
	{
		fFile.getline(s[x].name,30);
		fFile >> s[x].age;
		fFile >> s[x].gpa;
		fFile >> s[x].grade;
		fFile.ignore();
	}

	fFile.close();
	return 0;
}
1
2
3
4
5
6
7
8
for(int x=0; x<4; x++)
	{
		fFile.getline(s[x].name,30);
		fFile >> s[x].age;
		fFile >> s[x].gpa;
		fFile >> s[x].grade;
		fFile.ignore();
	}


the >> operator takes information FROM the file and gives it to your program. the << operator is what you need to write information TO the file.
Topic archived. No new replies allowed.