Writing to a file.

Can someone tell me how to write multiple entries to a file without having to declare each entry like

string entry1;
string entry2;
etc.

and is there a way to read from a preexisting file without putting an entry in the file?
Could you give a more detailed example? Like a short snippet of code that does it the way you don't want to?

And yes, you can read from existing files without having written to them. I'm not sure if that's exactly what you're asking though.
Zhuge, what I am trying to do is make a little program that lets the user input diary entries each day. My problem is I only know how to write to a txt file one time without overriding the previous entry. This is my writing to a file code so far.

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
string newEntry(string entry1, int userID)
{

	string date;
	ofstream outFile;
	outFile.open("journal.txt");
	if (outFile.fail())
	{
		cout << "Error could not open file.";
		return entry1;
	}
	int pw;
	cout << "Enter your userID: \n";
	cin >> pw;
	cin.ignore();
	if(pw == userID)
	{
	cout <<"Enter the date: " << endl;
	getline(cin,date);
	cout << "Whats on your mind\n\t" << endl;
	getline(cin,entry1);
	outFile << entry1;
	cout << endl << endl;
	}

	

	cout << "Entry submitted. \n\tpress enter to return to main menu\n";
	outFile.close();
	return entry1;
}


and u answered my 2nd question. thank you! do you know where i can find out how to read from existing files?
closed account (3qX21hU5)
Change it to this
outFile.open("journal.txt", ios::app);

This is telling it to not overwrite everything in the file everytime and instead append it to the end of the file.

By default ofstream is set to just overwrite everything in the file with the new data. Which is why we need to set the optional flag ios::app which will make it append to the file instead.

More about files and the others optional flags can be found here http://www.cplusplus.com/doc/tutorial/files/ or in the documentation on this site.
Last edited on
Ooo thanks! that worked perfectly! @zereo
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
94
95
96
97
98
99
100
101
#include<iostream>
#include<string>
#include<fstream>
#include<iomanip>
using namespace std;

string newEntry(string entry1, int userID);
void viewEntry(string entry1, int userID);

int main()
{

	int ch;
	int userID = 130464;

	string entry1;
	
	do
	{
		system("cls");
		cout<<"\n\n\n\tMAIN MENU";
		cout<<"\n\n\t01. NEW JOURNAL ENTRY";
		cout<<"\n\n\t02. VIEW JOURNAL ENTRIES";
		cout<<"\n\n\t03. EXIT";
		cout<<"\n\n\tSelect Your Option (1-3) ";
		cin>>ch;
		system("cls");
		switch(ch)
		{
		case 1:
			newEntry(entry1, userID);
			break;
		case 2:
			viewEntry(entry1, userID);
			break;
		 case 3:
			cout<<"\n\n\tGoodbye\n";
			return 1;
			break;
		 default :cout<<"\a";
		}
		cin.get();
	}while(ch!='3');
	return 0;
}
string newEntry(string entry1, int userID)
{

	string date;
	ofstream outFile;
	outFile.open("journal.txt", ios::app);
	if (outFile.fail())
	{
		cout << "Error could not open file.";
		return entry1;
	}
	int pw;
	cout << "Enter your userID: \n";
	cin >> pw;
	cin.ignore();
	if(pw == userID)
	{
	cout <<"Enter the date: " << endl;
	getline(cin,date);
	cout << "Whats on your mind\n\t" << endl;
	getline(cin,entry1);
	outFile << "\t" << date << endl;
	outFile << entry1 << endl;

	cout << endl << endl;
	}

	

	cout << "Entry submitted. \n\tpress enter to return to main menu\n";
	outFile.close();
	return entry1;
}
void viewEntry(string entry1, int userID)
{
	int pw;
	cout << "Enter your userID:\n";
	cin >> pw;
	if(pw == userID)
	{
	ifstream inFile;
	inFile.open("journal.txt");
	if(inFile.fail())
	{
		cout<<"File could not be open !! Press any Key...";
		return;
	}
	while(! inFile.eof())
	{
	cin.ignore();
	getline(inFile,entry1);
	cout << entry1;
	}
	}
}


This is my code so far to my journal project, and its working! Thanks guys very much! Im so excited right now.
Topic archived. No new replies allowed.