Trying to read .txt file name and contents

im trying to read .txt files that my program is making so i can create an if statement but i cant find a away to read them. Thank you if you help.

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
 #include <iostream>
#include <fstream>
#include <string>

using namespace std;

void personCreator();

int main()
{

	for (;;)
	{
		system("cls");
		personCreator();
		cout << "Press Enter to restart or press 1 to exit" << endl;
		cin.get();
	}
}

void personCreator()
{
	string firstname, lastname, birthyear, birthmonth, date;
	cout << "Whats your firstname? "; getline(cin, firstname);
	cout << "Whats your lastname? "; getline(cin, lastname);
	cout << "Whats your birth year? "; getline(cin, birthyear);
	cout << "Whats your birth month? "; getline(cin, birthmonth);
	cout << "Whats your birth date? "; getline(cin, date);


	ofstream myfile;
	myfile.open(firstname + "_" + lastname + ".txt", ofstream::out | std::ofstream::app);
	myfile << "firstname: " << firstname << endl;
	myfile << "lastname: " << lastname << endl;
	myfile << "birthyear: " << birthyear << endl;
	myfile << "birthmonth: " << birthmonth << endl;
	myfile << "date: " << date;
	myfile.close();

	myfile.open("name.txt", ofstream::out | std::ofstream::app);
	myfile << firstname <<  "  " << lastname << endl;
	myfile.close();

}
Last edited on
closed account (48T7M4Gy)
Use this as a model and make sure the .txt files are accessible/in the right directory/path:
http://www.cplusplus.com/doc/tutorial/files/
Do the same thing as you did with ofstream but with ifstream.
@integralfx so like this?

1
2
3
4
5
6
7
ifstream myfile;
myfile.open(firstname + "_" + lastname + ".txt");
myfile >> firstname;
myfile >> lastname;
myfile.close()

cout << "Welcome back " << firstname << " " << lastname << endl;


Last edited on
Topic archived. No new replies allowed.