0

I've looked through a few different things (cant seem to find a good file i/o tutorial on pluralrsight), and I've read tutorialspoint and cplusplus websites on file writing but mine won't seem to work the same.

I copied rather similarly, the tutorial fro tutorialspoint on C++ file I/O:

ofstream out;
out.open("D:\\cpp_files\\test_iofile.txt", ios::out);

cout << "Writing to file" << endl;
cout << "Enter your name: ";
string name;
getline(cin, name);

out << name;

cout << "Enter age: ";
int age;
cin >> age;

out << age << endl << "This is an insert" << endl;
out.close();

There's some more stuff in the middle you probably don't care much about, and then there's this section:

out.open("D:\\cpp_files\\test_iofile.txt", ios::ate);
out << endl;

string inp;
cout << "Write some random crap: ";
getline(cin, inp);

out << inp << endl;
out << "-----------";
out.close();

The weird thing is, it creates the .txt file in the right location, but its output equates to 2 blank lines and the dashes. So I end up with (The '>' added to indicate blank lines):

>
>
----------

I know it must be something I am missing, but I can't seem to catch it. No build errors from the compiler either.

glampert
  • 4,371
  • 2
  • 23
  • 49
aescript
  • 1,775
  • 4
  • 20
  • 30

1 Answers1

1

You have to flush the buffer after you cin >> age;,

cin.ignore (std::numeric_limits<std::streamsize>::max(), '\n');

and change ios::ate to ios::app

must #include <limits>

ios:ate truncates the file, whereas ios::app appends at the end.

An alternative to cin.ignore is to use after cin >> age the getline as

getline(cin >> ws, inp);

This instructs the input stream to discard previously accumulated "whitespaces", i.e. newline, tabs etc

vsoftco
  • 55,410
  • 12
  • 139
  • 252
  • I thought ios::trunc truncated. and ate just put the file pointer at the end (like append) but allowed you to move? Also whats for? and im not sure i understand the cin.ignore() part - that wasnt in the tutorial. Ill have to look it up – aescript May 20 '14 at 18:16
  • `` is for the constant `std::numeric_limits::max()`, `ate` truncates actually the file, see http://stackoverflow.com/questions/12929378/difference-between-iosapp-and-iosate, and the `cin.ignore` part is because after you read the age, you get an extra newline that will be eaten by next `getline`, so in the subsequent `getline` you end up with an empty string. The ignore part just tells the stream to flush its content http://stackoverflow.com/questions/10553597/cin-and-getline-skipping-input. – vsoftco May 20 '14 at 18:19
  • Thanks, this helped. the main problem was that ate (which after reading the article you linked im still trying to figure out why its included if it just truncates anyway) and it seems most likely ill have to get into file seeking and do things like app on my own to avoid other issues. As far as the cin >> ws part - this worked, which is great. But i dont 100% understand the concept. Ill be reading up as well, but are you saying any endl; i add in the actual code are being read as the next input so its never getting to ask me for the getline? and >> flushes it, but still writes it? hmmm – aescript May 20 '14 at 19:13