txt not being compleated

Well I am trying to make a program that save the user's input into a txt file but for some reason when I open the txt file the whole word is not there.

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

using namespace std;

string one;
string input();
void help();


int main()
{
	do{
	input();
	cout<< one <<endl;
	help();
	}
	while(one!= "0");
	return 0;
}

string input()
{
	getline(cin,one);
	return one;
}
void help()
{
	fstream app;
	app.open("Three.txt");
	app<< one <<endl;
	app.close();
}
If you just want to write to the file, try
app.open("Three.txt", ios::out);
or use ofstream instead of just fstream.
alright so how do I get the text in ofstream to add to the end of the document instead of replacing it
If you're using ofstream, then either
app.open("Three.txt", ios::app);
or
app.open("Three.txt", ios::ate);
should work.

If you're sticking with just fstream, I would also add ios::out to that, e.g.
app.open("Three.txt", ios::out | ios::app);
(Either way, it doesn't hurt to add it, even for ofstream.)
Or you could not close the file each time (keep in mind that the destructor would close the stream)
1
2
3
4
std::ofstream output("foo.txt");
int n;
while( std::cin >> n and n not_eq 0 )
   output << n << '\n';
Alright thank you now is there a way to have the program creat a new txt file each time the program is run or would I always have to specify a file to open
If I understand you correctly, you want your program to create a new (different) text file each time?
So for instance, if your program wants to create a file foo.txt, but foo.txt already exists, then create bar.txt instead?

In any case, your program needs to know what to name the file.

That being said, it's possible to use a loop and std::ostringstream to create files foo1.txt, foo2.txt, foo3.txt, etc.

Since you want it to create a new file each time the program is run, you can do this:
initialize i to 1
while fooi.txt exists (replace i with the value of i)
    increment i
create file fooi.txt

You can converting numbers to strings using one of these methods:
http://www.cplusplus.com/articles/D9j2Nwbp/
For checking whether a file exists or not, you could try opening it up in read-only mode (perhaps using an std::ifstream), and then checking whether the file is actually open or not.
Alright thanks you know is there a way to have the program check everything in that file like the following code, so if Three.txt has both 50 and 1 in it then both would set thetxt try again
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
#include <iostream> 
#include <string>
#include <fstream>
#include <time.h>

using namespace std;

string one;
string input();
void help();


int main()
{
	do{
	input();
	help();
	string Three;
	fstream inout;
	inout.open("Three.txt", ios::in);
	while (!inout.eof())
	{
	inout>>Three;
	if( one == Three)
	{
		cout<<"try again!!!"<<endl;
	}
	inout.close();
	}
	}
	while(one!= "#");
	return 0;
}

string input()
{
	getline(cin,one,'\n');
	return one;
}

void help()
{
	fstream inout;
	inout.open("Three.txt",ios::out|ios::app);
	inout<< one <<endl;
	inout.close();
}
Topic archived. No new replies allowed.