Thread: String help

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    28

    String help

    How do you create a string from a character array?

    Better yet, how do you read from a line from a file and then assign that line to a string? The getline method from the ifstream class requires a *char as it is...

  2. #2
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    Code:
    char *line;
    //manipulate line
    
    std::string s = line;
    //...
    std::string s= ...;
    s = line;
    //or
    s += line;
    //or
    s.assign(line);

  3. #3
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Use the getline method that works with strings:
    Code:
    #include <iostream>
    #include <string>
    
    int main()
    {
        std::string input;
        std::getline(std::cin, input);
        std::cout << input << std::endl;
    }
    Best part - you don't have to worry about the length of the input.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. compare structures
    By lazyme in forum C++ Programming
    Replies: 15
    Last Post: 05-28-2009, 02:40 AM
  2. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM