Thread: string manipulation

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    142

    string manipulation

    How do i get the contents of a file into a buffer so that i can use some <string> functions on it?

    Cheers
    Last edited by wart101; 12-11-2006 at 01:54 AM.
    WhAtHA hell Is GoInG ON

  2. #2
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476
    Dunno if this is what you wanted:

    http://www.arachnoid.com/cpptutor/student2.html

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    142
    Quote Originally Posted by g4j31a5
    Dunno if this is what you wanted:

    http://www.arachnoid.com/cpptutor/student2.html
    Sorry i wasn't to clear, i know how to ge a file into a buffer but i want to convert it from (const * char) to (string) but i don't know how to.

    I hope that makes sence.
    WhAtHA hell Is GoInG ON

  4. #4
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476
    I think the fstream's getline() function can have a string as the output parameter (CMIIW). Or why don't you use the copy constructor of the string? E.g.

    Code:
     char str1[]="Hello World!";
     std::string str2=str1;

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    If you want to read the entire contents of a file into a string object you can use a stringstream as an intermediate object.
    Code:
    ifstream file("foo.txt");
    stringstream sstr;
    string info;
    
    if( file.is_open() )
    {
        sstr << file.rdbuf();  // Read entire file into stringstream
        info = sstr.str();     // Convert stringstream into string
        // Do whatever you want with the string "info".
    }
    "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

  6. #6
    Registered User
    Join Date
    Sep 2005
    Posts
    142
    Quote Originally Posted by hk_mp5kpdw
    If you want to read the entire contents of a file into a string object you can use a stringstream as an intermediate object.
    Code:
    ifstream file("foo.txt");
    stringstream sstr;
    string info;
    
    if( file.is_open() )
    {
        sstr << file.rdbuf();  // Read entire file into stringstream
        info = sstr.str();     // Convert stringstream into string
        // Do whatever you want with the string "info".
    }
    Thanks mate that is exactly what i needed, cheers.
    WhAtHA hell Is GoInG ON

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  3. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  5. string manipulation
    By SPEKTRUM in forum Linux Programming
    Replies: 3
    Last Post: 01-26-2002, 11:41 AM