Reading Entire Line

Using this statement, [code=cpp]getline( LoadText, loadTextString );[/code], how come it only saves the first word to 'LoadTextString'? Is there any way to store all words typed out on that line?

Here is my code for that section:
[code=cpp] case 2: //RETRIEVE TEXT -- LoadText("SAVEDATA.txt")
if (LoadText.is_open())
{
while (! LoadText.eof() )
{
getline( LoadText, loadTextString );
cout << loadTextString << endl;
}
LoadText.close();
}
break;[/code]

Any ideas?

Thanks in advance,
CheesyBeefy

Last edited on
Is loadTextString of type string?

It should get the whole line.
Yes, loadTextString is a string variable.

However it only saves the first word.

For example, if I put: "Save this text.", it would save this: "Save".

Any other advise?




Also, this is the code that saves the text:


[code=cpp]case 1: //SAVE TEXT -- SaveText
cout << "Enter some text to save: ";
SaveText.open("SAVEDATA.txt");
cin >> saveTextString
SaveText << saveTextString;
SaveText.close();
break;[/code]

Last edited on
Try using a cin.getline on saveTextString
I am now, but I get this error:
1
2
3
4
5
6
7
1>c:\documents and settings\brad\my documents\visual studio 2008\projects\fstreamconsole\fstreamconsole\fstreamconsole.cpp(101) :
 error C2661: 'std::basic_istream<_Elem,_Traits>::getline' : no overloaded function takes 1 arguments
1>        with
1>        [
1>            _Elem=char,
1>            _Traits=std::char_traits<char>
1>        ]


Here is the save code:
[code=cpp]case 1: //SAVE TEXT -- SaveText
cout << "Enter some text to save: ";
SaveText.open("SAVEDATA.txt");
cin.getline( saveTextString );
SaveText << saveTextString;
SaveText.close();
break;[/code]


Did some testing and I was misstaken, you should have used getline(cin, string)
Heres some code that worked for me:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main(int argc, char *argv[])
{
    string saveTextString;
    ofstream saveTextFile;
    cout << "Enter some text: ";
    getline(cin, saveTextString);
    saveTextFile.open("saveTextFile.txt");
    saveTextFile << saveTextString;
    saveTextFile.close();
    system("PAUSE");
    return EXIT_SUCCESS;
}
Topic archived. No new replies allowed.