ifstream and strings

This is a discussion on ifstream and strings within the C++ Programming forums, part of the General Programming Boards category; I take in a line of text at a time from a file, do my parsing and organizing, append it ...

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    162

    ifstream and strings

    I take in a line of text at a time from a file, do my parsing and organizing, append it all to my big finale' string and do the funky chicken, then my data is ready to be written to a file.

    Everything is dandy for me up until I get to actually writting the string. I created a simplified variation of my sample code which still produces the error, then I cut it down to this to show who ever may help me. Here it is:

    Code:
    string name, pass, final; // all strings
    
    [ ... blabla .. ]
    
    final.append("INSERT INTO `data` VALUES (1, '");
    final.append(name);
    final.append("', '");
    final.append(pass);
    final.append("');");
    
    fout.write(final.c_str()); // <---- Fails Here
    fout.put('\n');
    Essentially it doesn't want a const char* and it's getting one... so how exactly do I go about getting my string from this state in to my file?

    I attempted to creat a char* which pointed to the string.c_str() and casted which failed, I then realized I was guessing and who know where I would go after that so someone please help.

    Thanks!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,303
    Perhaps you wish to write:
    Code:
    fout << final << "\n";
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jul 2006
    Posts
    162
    No kidding? Oh my, I spent some time on this... and if that's the sollution then damn....

    update: Thank you. : ]

    another: I think this was one of those 'very late night stupid things' for me, now i'm seeing it everywhere, had i only looked at the << operator instructions on the websites. -sigh-
    Last edited by simpleid; 11-19-2006 at 05:37 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem with string class, ifstream, and getline
    By deathbob in forum C++ Programming
    Replies: 9
    Last Post: 09-18-2005, 12:20 PM
  2. Ofstream, Ifstream, Searching files for strings?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 04-04-2005, 04:45 PM
  3. Quick question: Ifstream and Strings
    By Junior89 in forum C++ Programming
    Replies: 11
    Last Post: 12-22-2004, 03:22 AM
  4. What actually happens when we use strings
    By Zahl in forum C++ Programming
    Replies: 0
    Last Post: 04-26-2003, 03:52 PM
  5. am I declaring my strings wrong?
    By sunburnbyRA in forum C++ Programming
    Replies: 7
    Last Post: 03-06-2003, 05:42 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21