Thread: ifstream and strings

  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
    28,413
    Perhaps you wish to write:
    Code:
    fout << final << "\n";
    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

  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, 11:20 AM
  2. Ofstream, Ifstream, Searching files for strings?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 04-04-2005, 03: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, 02: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