-3

I've been working on saving to files and this was the result. The only problem is that anything after a space is ignored, (if you typed "john smith") it would print ("the last person to use this file was: john") I am using codeblocks with the GNU GCC compiler. Here is the code:

    #include <iostream>
    #include <cstdlib>
    #include <fstream>

    using namespace std;

    int main()
    {
        string name;
        ofstream saveData;
        ifstream Data;
        Data.open("Info.data", ios::binary);
        Data >> name;
        Data.close();
        cout << "The last person to use the file was " << name << endl;
        cout << "What is your name?" << endl;
        cin >> name;
       saveData.open("Info.data", ios::binary);
       saveData << name;
       cout << name << endl;
       system("PAUSE");
       saveData.close();
       return 0;
   }

thanks

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190

1 Answers1

0

For strings objects of ifstream (including cin) use input from beginning till the first space, and space is SPACE, TAB, and NELINE. So, you should use getline instead cin >>

Try this:

Data.open("Info.data");
getline(Data, name);
Data.close();

and

cout << "What is your name?" << endl;
//cin >> name;
getline(cin, name);

UPDATE:

By the way, in your code after

   Data.open("Info.data", ios::binary);

you use

   Data >> name;

So, stream opened in binary mode is read by >> - it is not so good.

VolAnd
  • 6,367
  • 3
  • 25
  • 43
  • recommend a swap of istream for ifstream and whitespace for space. – user4581301 Aug 06 '16 at 06:16
  • what #include file is getline in? – Jake Wickham Aug 06 '16 at 06:18
  • getline(name); didn't work with #include – Jake Wickham Aug 06 '16 at 06:21
  • am i using getline() wrong? – Jake Wickham Aug 06 '16 at 06:21
  • MSDN says it is in - https://msdn.microsoft.com/query/dev12.query?appId=Dev12IDEF1&l=EN-US&k=k(%22string%2Fstd%3A%3Agetline%22);k(%22std%3A%3Agetline%22);k(getline);k(DevLang-C%2B%2B);k(TargetOS-Windows)&rd=true – VolAnd Aug 06 '16 at 06:23
  • 1
    @JakeWickham take care mixing >> and getline. >> stops at whitespace, but doesn't extract it from the stream. Stackoverflow is littered with questions from people confused by getline returning empty strings because an end of line was left in the stream after the user typed input and hit enter. – user4581301 Aug 06 '16 at 06:24
  • 1
    The usual advice is not to mix `getline()` and operator `>>` on the same stream AT ALL. – Peter Aug 06 '16 at 06:27
  • is getline() better than cin? – Jake Wickham Aug 06 '16 at 06:31
  • Better, no. It just performs a different job. A common trick is to read lines from the user with getline, and then write the line into a stringstream (`#include `), and parse the stringstream with >> or more getlines, but getlines using a different delimiter than end of line. Read more here: http://en.cppreference.com/w/cpp/string/basic_string/getline and here: http://stackoverflow.com/questions/7868936/read-file-line-by-line – user4581301 Aug 06 '16 at 06:38