Stuck with a file IO problem

Hey, I am writing a small program that saves the text the user enters and then displays it when they choose the load option next time they run the program. My problem is this:

1
2
3
4
5
6
7
8
9

ofstream myOutFileStream("save1.txt");
myOutFileStream << "I like cheese";
myOutFileStream.close();

ifstream myInFileStream("save1.txt");
string save1((istreambuf_iterator<char>(myInFileStream)), istreambuf_iterator<char>());
cout << save1;
myInFileStream.close();


That works fine, load the whole "I like cheese" thing no problem. However when I went to make my user version I found I could only get it to open the first word of the string entered. When I tried using cin.getline() it would not even let me enter the text. Here is my current code:

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

using std::cin;
using std::cout;
using std::string;
using std::ofstream;
using std::ifstream;
using std::istreambuf_iterator;

int main()
{

int choice;

cout << "Hello user! I was told to greet you in a nice and polite way! Lets be friend?\n";
cout << "So, do you want to create a new file or load the last one?\n";
cout << "1)new 2)load: ";
cin >> choice;
if (choice == 1){
    char text[50];
    cout << "Enter the text: ";
    cin >> text;
    ofstream myOutFileStream("save1.txt");
    myOutFileStream << text;
    myOutFileStream.close();
    }

if (choice == 2){
    ifstream myInFileStream("save1.txt");
    string save1((istreambuf_iterator<char>(myInFileStream)), istreambuf_iterator<char>());
    cout << save1;
    myInFileStream.close();
    }
}


using cin.getline(text,50); did not work as I said. Kind of at a loss. I want the user to be able to type a sentence, save that sentence to a file, then load it and print it.

I am pretty sure the problem is on the line "cin >> text" and I know that cin stops at a space (" "), so I know that it is only getting the first word. When I Google'ed getline and such I found http://www.cplusplus.com/reference/iostream/istream/getline/ but for some reason it won't work for me. Any ideas?
Fisrt > Maybe u can try to add an function such an getchar() to catch a char between line20 and line21.
Second > use getlin( ) instead cin in line 24 and use string in line 22 maybe better.
Nope, I was way off ( I tried all of that). Needed to use
 
std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );


Works great now.

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

using std::cin;
using std::cout;
using std::string;
using std::ofstream;
using std::ifstream;
using std::istreambuf_iterator;
using std::getline;

int main()
{

int choice;

cout << "Hello user! I was told to greet you in a nice and polite way! Lets be friend?\n";
cout << "So, do you want to create a new file or load the last one?\n";
cout << "1)new 2)load: ";
cin >> choice;
if (choice == 1){
    string text;
    cout << "Enter the text: ";
    std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
    getline (cin,text);
    ofstream myOutFileStream("save1.txt");
    myOutFileStream << text;
    myOutFileStream.close();
    }

if (choice == 2){
    ifstream myInFileStream("save1.txt");
    string save1((istreambuf_iterator<char>(myInFileStream)), istreambuf_iterator<char>());
    cout << save1;
    myInFileStream.close();
    }
}


Thats the full code, so happy.
If you want to use the iterators you should turn off the skipws flag on your stream.
myInFileStream >> noskipws;
Then use the iterators.
To be honest I am still learning about iterators. I don't fully understand my own code :)
Topic archived. No new replies allowed.