File input problem.

Sep 25, 2015 at 9:44pm
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;
	}
}
Sep 26, 2015 at 12:01am
http://www.cplusplus.com/reference/fstream/ofstream/ofstream/

Set the mode to app it won't rewrite the file
Sep 26, 2015 at 12:28am
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 Sep 26, 2015 at 12:39am
Sep 26, 2015 at 12:45am
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
Sep 26, 2015 at 12:51am
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);
Sep 26, 2015 at 12:55am
I mean cin>>gFileText not the one in getline
Sep 26, 2015 at 12:58am
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 Sep 26, 2015 at 1:07am
Sep 26, 2015 at 1:08am
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/
Sep 26, 2015 at 1:09am
There is no \n in my code though.
Sep 26, 2015 at 1:11am
After nputtin wth cin>> it'll leave '\n' in the buffer
Sep 26, 2015 at 1:12am
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.
Sep 26, 2015 at 1:19am
Add cin.ignore(1024/*or something*/, '\n') before getline to get rid of that \n
Sep 26, 2015 at 1:22am
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.
Sep 26, 2015 at 1:39am
Ts about the length and delimiters, but if its work. Then its Great :D
Topic archived. No new replies allowed.