fstream Help please.

I am making a program to manage a text file. I have completed the program to add text, append and then read the text file, however when it comes to the inputs, using cout <<, cin >> string, only adds 1 word and not the full line of text entered. How can I add a full line of text. I worked out getline() works for reading from the file.

1
2
3
4
5
6
7
8
9
10
11
12
  fstream file;


              file.open("WriteTextFile.txt", ios::out);

              file << UserInput << endl;

              file.close();


              cout << "Your file has now been written!\n";
std::cin reads until (white)space. use std::getline(std::cin,...) there too.
Last edited on
Can you tell me what I am doing wrong, as it still wont read input to file.

1
2
3
4
5
6
7
8
9
10
11
12
13
  
cout << "write a line of text\n";
            std::cin >> In;   //store input

              fstream file;  //file variable


              file.open("WriteTextFile.txt", ios::out); //open file 

              file << getline(std::cin, In);  //add string content to file

              file.close();
As pandasd stated, cin >> will read only upto the first whitespace. Therefore line 3 places only up to the first whitespace into In.

Line 10: This line makes no sense. getline return a reference to an istream, so you're going to attempt to write an address to file.

Consider the following:
1
2
3
4
5
6
7
8
9
  string In;
  cout << "write a line of text\n";
  getline (cin, In);  // read entire line 

  fstream file;  //file variable
  file.open("WriteTextFile.txt", ios::out); //open file 
  file << In;  //add string content to file
  file.close();
 


Okay so I have this code now, however it does not ask for an input. so I included cin also, but with cid it will record all the data entered apart from the first word?

1
2
3
4
5
6
7
8
9
10
11
12
13
cout << "write text to a text file\n";

             std::getline (std::cin, Input);   //store input in string

              fstream file;  //declare file variable


              file.open("WriteTextFile.txt", ios::out); //open file for input

              file << Input;  //add string content to file

              file.close();
 
it does not ask for an input

What do you mean "it does not ask for input"?

You haven't shown a full program, but from what you've shown it appears it should ask for input.

but with cid it will record all the data entered apart from the first word?

I don't understand what you mean by "apart from the first word"?
Are you reading in the first word elsewhere? The code you've shown should read in the entire first line.
Last edited on
Example of reading .txt
string file is name.txt.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int ReadeWholeTxt( string file )
{
		ifstream fin( file);
		if (fin.good())
		{
			char ch;
			while (fin.get(ch)) 
				{
					cout << ch;
				}
			return 1;
		}
		else
		{
			cout << plik << " File doesnt exsist \n";
			return 0;
		}
}

Example of writing something in .txt
1
2
3
4
5
6
7
8
void Save ( )
{
	string file;
	file = "name.txt"; //Will create name.txt file
	ofstream fout(file, ios_base::out | ios_base::trunc);
	fout << "Something"; //Will put "Something" in the .txt file. Remove ios_base::trunc if you dont want to delete previously wroten things.
	fout.close();
}

Example of writing something you will write in console in .txt
1
2
3
4
5
6
7
8
9
10
void Save ( )
{
	string file;
        string text;
        cin >> text;
	file = "name.txt"; //Will create name.txt file
	ofstream fout(file, ios_base::out | ios_base::trunc);
	fout << text; //Will put "text" in the .txt file. Remove ios_base::trunc if you dont want to delete previously wroten things.
	fout.close();
}
Last edited on
My code is very big. this is the function I am trying to get to work. I'll will try and explain what happens. I run the function and it will output both couts and not ask for an input. That is what I meant by it not asking for an input. HOWEVER when I included cin to request and input and included getline under it, it would save the text from the cin ( ALL but the first word..so I enter hi babe sup..and it saves babe sup.) I hope this is more clear?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
   

{

            cout << "write text to a text file\n";

             std::getline (std::cin, Input);   //store input in string

              fstream file;  //declare file variable


              file.open("WriteTextFile.txt", ios::out); //open file for input

              file << Input;  //add string content to file

              file.close(); //close file


              cout << "Your file has now been written!\n";

              }


Thank you Furjoza. I did not ignore your post. Those are nice examples.
Last edited on
Do you have other inputs from cin that precede this code?
It's possible there is data (such as a \n) left in the cin buffer from a previous cin operation. This would cause the getline to terminate immediately. If this is the case, use cin.ignore
http://www.cplusplus.com/reference/istream/istream/ignore/
Topic archived. No new replies allowed.