File input problem.

Everything seems to work when main() runs newFile() except for the fact that it only writes everything after the first word. So if I input: this is a test file. It will write the text to the file as " is a test file."

Any suggestions?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  void newFile()
{
	std::ofstream file_;

	std::cout << "New File Name: ";
	std::cin >> gFileName;
	file_.open(gFileName);
	std::cout << "Save To: ";
	std::cin >> gFileLoc;

	if (file_.is_open())
	{
		std::cout << "File saved in '" << gFileLoc << "'. File ready for writing. Press ENTER when finished." << std::endl;

		std::cin >> gFileText;

		std::getline(std::cin, gFileText);
		file_ << gFileText;
	}
}
http://www.cplusplus.com/reference/fstream/ofstream/ofstream/

Set the mode to app it won't rewrite the file
Here is my code with your solution. Still the same results.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void newFile()
{
	std::cout << "New File Name: ";
	std::cin >> gFileName;
	std::cout << "Save To: ";
	std::cin >> gFileLoc;

	std::ofstream file_(gFileName, std::ofstream::app);

	if (file_.is_open())
	{
		std::cout << "File saved in '" << gFileLoc << "'. File ready for writing. Press ENTER when finished." << std::endl;

		std::cin >> gFileText;

		std::getline(std::cin, gFileText);
		file_ << gFileText;
	}
}


So basically at line 14 you enter the text you want inside the file. For example if you type "Cplusplus is a great resource" it WILL write the file, but it will cut the first word off and read " is a great resource" inside the file.
Last edited on
Ah, okay sorry I midunderstod your problem, delete that cin
Ithink the reason is the first word read by cin after meet space cin will stop to read, rest of the sentence will be read by getline
could you elaborate or give example? simply deleting the cin is a syntax error

my way:
 
std::getline(std::cin, gFileText);


your suggestion:
 
std::getline(gFileText);
I mean cin>>gFileText not the one in getline
ah right. Ok I removed that one and it just exits the program before you can enter any text.

Current code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void newFile()
{
	std::ofstream file_;

	std::cout << "New File Name: ";
	std::cin >> gFileName;
	file_.open(gFileName);
	std::cout << "Save To: ";
	std::cin >> gFileLoc;

	if (file_.is_open())
	{
		std::cout << "File saved in '" << gFileLoc << "'. File ready for writing. Press ENTER when finished." << std::endl;

		std::getline(std::cin, gFileText);
		file_ << gFileText;
	}
}
Last edited on
It's because getline read '\n' from previous cin>>
Putting cin.ignore(/*some numbers here*/,'\n')
See here http://www.cplusplus.com/forum/articles/6046/
There is no \n in my code though.
After nputtin wth cin>> it'll leave '\n' in the buffer
So based on my current code update posted a few posts up. What do you suggest I do? Because I'm lost at this point.
Add cin.ignore(1024/*or something*/, '\n') before getline to get rid of that \n
looks like just adding std::cin.ignore(); with no parameters was enough. Not sure what the whole 1024 thing was you mentioned but with no parameters, it seems to work.
Ts about the length and delimiters, but if its work. Then its Great :D
Topic archived. No new replies allowed.