Thread: Trouble with reading data with istringstream

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    13

    Trouble with reading data with istringstream

    Well, this stinks....I'm able to user ostringstream to wreite data to a file but when i read it back, Dev C++ gives me these errors:

    Code:
    Compiler: Default compiler
    Building Makefile: "F:\Projects\Makefile.win"
    Executing  make...
    make.exe -f "F:\Projects\Makefile.win" all
    g++.exe -c User.cpp -o User.o -I"F:/Dev-Cpp/lib/gcc/mingw32/3.4.2/include"  -I"F:/Dev-Cpp/include/c++/3.4.2/backward"  -I"F:/Dev-Cpp/include/c++/3.4.2/mingw32"  -I"F:/Dev-Cpp/include/c++/3.4.2"  -I"F:/Dev-Cpp/include"   
    
    User.cpp: In member function `bool User::loadUserFile(const char*)':
    User.cpp:54: error: ambiguous overload for 'operator>>' in '((User*)this)->User::UserDataIn >> ((std::string*)this)->std::basic_string<_CharT, _Traits, _Alloc>::c_str [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]()'
    F:/Dev-Cpp/include/c++/3.4.2/istream:687: note: candidates are: std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, unsigned char*) [with _Traits = std::char_traits<char>] <near match>
    F:/Dev-Cpp/include/c++/3.4.2/istream:692: note:                 std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, signed char*) [with _Traits = std::char_traits<char>] <near match>
    
    User.cpp:55: error: ambiguous overload for 'operator>>' in '((User*)this)->User::UserDataIn >> (((std::string*)((User*)this)) + 4u)->std::basic_string<_CharT, _Traits, _Alloc>::c_str [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]()'
    F:/Dev-Cpp/include/c++/3.4.2/istream:687: note: candidates are: std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, unsigned char*) [with _Traits = std::char_traits<char>] <near match>
    F:/Dev-Cpp/include/c++/3.4.2/istream:692: note:                 std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, signed char*) [with _Traits = std::char_traits<char>] <near match>
    
    make.exe: *** [User.o] Error 1
    
    Execution terminated

    this is how i'm reading the data:

    Code:
    
    bool User::loadUserFile(const char* filename)
    {
         char UBuff[256];
         
         sprintf(UBuff,"%s.user",filename);
         
         UserFileIn.open(UBuff,ios_base::binary );
         
         if(UserFileIn.is_open() )
         {
               UserDataIn >> UserName.c_str();
               UserDataIn >> Password.c_str();
               
               UserFileIn.read((char*)&UserDataIn,sizeof(UserDataIn));
               
               if(UserFileIn.is_open() )
               {
                                       UserFileIn.close();
               }
               return true;
         }
         else
         {
             return false;
         }
         return true;
    }
    Also how would i make it so when u typed the password u'd get this: "*"?

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You don't want to call c_str() when reading into a string. Just read directly into it:
    Code:
               UserDataIn >> UserName;
               UserDataIn >> Password;
    There might be other issues there (like where/how are those variables declared and are you mixing binary and text based input?), but I think that's what this specific problem is. Note that if you get errors with that version, make sure you #include <string>.

    >> Also how would i make it so when u typed the password u'd get this: "*"?
    You have to use non-standard input functions to do that. I think kbhit might help but I don't remember for sure. It's a common question if you'd like to search the board for better answers.
    Last edited by Daved; 08-19-2010 at 04:55 PM.

  3. #3
    Registered User
    Join Date
    Aug 2010
    Posts
    13
    Doing this gives me these errors:

    Code:
    
    Compiler: Default compiler
    Building Makefile: "F:\Projects\Makefile.win"
    Executing  make...
    make.exe -f "F:\Projects\Makefile.win" all
    g++.exe -c main.cpp -o main.o -I"F:/Dev-Cpp/lib/gcc/mingw32/3.4.2/include"  -I"F:/Dev-Cpp/include/c++/3.4.2/backward"  -I"F:/Dev-Cpp/include/c++/3.4.2/mingw32"  -I"F:/Dev-Cpp/include/c++/3.4.2"  -I"F:/Dev-Cpp/include"   
    
    g++.exe -c User.cpp -o User.o -I"F:/Dev-Cpp/lib/gcc/mingw32/3.4.2/include"  -I"F:/Dev-Cpp/include/c++/3.4.2/backward"  -I"F:/Dev-Cpp/include/c++/3.4.2/mingw32"  -I"F:/Dev-Cpp/include/c++/3.4.2"  -I"F:/Dev-Cpp/include"   
    
    User.cpp: In member function `bool User::loadUserFile(const char*)':
    User.cpp:54: error: ambiguous overload for 'operator>>' in '((User*)this)->User::UserDataIn >> "User Name: "'
    F:/Dev-Cpp/include/c++/3.4.2/istream:687: note: candidates are: std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, unsigned char*) [with _Traits = std::char_traits<char>] <near match>
    F:/Dev-Cpp/include/c++/3.4.2/istream:692: note:                 std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, signed char*) [with _Traits = std::char_traits<char>] <near match>
    User.cpp:55: error: ambiguous overload for 'operator>>' in '((User*)this)->User::UserDataIn >> "Password: "'
    F:/Dev-Cpp/include/c++/3.4.2/istream:687: note: candidates are: std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, unsigned char*) [with _Traits = std::char_traits<char>] <near match>
    F:/Dev-Cpp/include/c++/3.4.2/istream:692: note:                 std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, signed char*) [with _Traits = std::char_traits<char>] <near match>
    
    make.exe: *** [User.o] Error 1
    
    Execution terminated
    this is what i'm doing:


    Code:
    UserDataIn >> "User Name: " >> UserName;
               UserDataIn >> "Password: " >>  Password;
    doesw it matter if i don;t use the user name/password strings? the ones in the "" would that still load the data from the file? (Just trying to get this, sorry if u said this and i didn't understand.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I didn't quite understand your question. You don't want to add the "User Name: " to the code that reads in the strings. You're just reading from the file or from the user. If you want to prompt a user to type in their username, you might do something like this:
    Code:
    cout << "User Name: ";
    cin >> UserName;
    But if you really want the password to be covered with '*', then you'll have to use something different than input streams anyway because they only look at the input after it has been typed by the user and the user hits <enter>. By then it is too late the letters are already on the screen.

  5. #5
    Registered User
    Join Date
    Aug 2010
    Posts
    13
    Heres a working example, pseudo code:

    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    void readfile ( const char * filepath )
    {
      ifstream file ( filepath);
    
      while ( !file.eof() )
      {
          string line;
          
          getline(&line, 256);
    
          cout << line.c_str() << endl;
      }
    }

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Maybe I'm being hypercritical but that whole post is quite misleading. As a working example getline is written entirely wrong. The inexperienced might later ask: How does it know to read from the file you opened? Why is there a magic number? Why is line passed by pointer? That is also exactly why it fails as pseudo code; it leads to unnecessary confusion about what to do.Pseudo code that has nothing to do with the OP's problem, and doesn't explain the algorithm you wanted to show is bad pseudo code.

    Never write pseudo code again. :|

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    CppProgrammer33, why not show us the code that you use to write to file, and/or tell us what is the file format? This will make it clear if your motivation for writing:
    Code:
    UserDataIn >> "User Name: " >> UserName;
    UserDataIn >> "Password: " >>  Password;
    is that you want to skip the "User Name: " portion of the file, or if it is merely an error made in desperation.

    Quote Originally Posted by iPromise
    Heres a working example, pseudo code:
    That looks like it is intended to be compilable C++, not pseudo code. As such, I must point out that it is wrong: you should not use eof() to control a loop like that, you should not be taking the address of line, and there is no need to use c_str().
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Aug 2010
    Posts
    13
    Sorry guys, I coded that on the spot, here is a better example:

    Code:
    #include <string>
    #include <iostream>
    #include <ifstream>
    
    using namespace std;
    
    int main ()
    {
       ifstream File("...");
    
       while ( !File.eof() )
       {
           string line;
           
           File >> line;
    
           cout << line << endl;
       }
    
       File.close();
    
       return 0;
    }
    This works fine for me, I use eof() because I am reading until the end of the file, I don't see how thats a problem.

  9. #9
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by iPromise View Post
    Sorry guys, I coded that on the spot, here is a better example:

    Code:
    #include <string>
    #include <iostream>
    #include <ifstream>
    
    using namespace std;
    
    int main ()
    {
       ifstream File("...");
    
       while ( !File.eof() )
       {
           string line;
           
           File >> line;
    
           cout << line << endl;
       }
    
       File.close();
    
       return 0;
    }
    This works fine for me, I use eof() because I am reading until the end of the file, I don't see how thats a problem.
    Sorry, but it's still wrong, see here (it's more C focused but the concept still applies to C++). What you need to do is test the result of the read operation instead of testing for EOF. If the read fails then there was nothing more to read and you shouldn't enter the loop at all. Testing for EOF and using it to control the loop means you go through the loop one more time than you should resulting in unexpected behavior (for newbs at least) of the program.

    Your example should simply be:
    Code:
    string line;
    while ( File >> line )
    {
        cout << line << endl;
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    @iPromise: You really don't need to manually close files. The destructor will take care of it for you. Good to remember.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading data file efficiently
    By Korhedron in forum C++ Programming
    Replies: 3
    Last Post: 08-07-2010, 03:09 PM
  2. reading in file data
    By sickofc in forum C Programming
    Replies: 2
    Last Post: 03-01-2006, 05:16 PM
  3. Please Help Reading Data From A File
    By NickHolmes in forum C Programming
    Replies: 5
    Last Post: 05-29-2005, 11:24 PM
  4. Reading encrypted data from file
    By cctoombs in forum C Programming
    Replies: 2
    Last Post: 02-13-2005, 02:59 AM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM