istream and string

Discussion in 'C++' started by nnandini8@gmail.com, Apr 20, 2006.

  1. Guest

    Hello guys,
    I have a small doubt in istream.
    I have a file calles infile.txt
    infile.txt contains Hello World
    now I wanted to add these contents to a string.
    ie i need to have hello world in a string
    Note there is a space between Hello and WOrld

    Any help will be greatly appreciated
    Thank You
    Nandini
     
    , Apr 20, 2006
    #1
    1. Advertising

  2. Mike Wahler Guest

    <> wrote in message
    news:...
    > Hello guys,
    > I have a small doubt in istream.
    > I have a file calles infile.txt
    > infile.txt contains Hello World
    > now I wanted to add these contents to a string.
    > ie i need to have hello world in a string
    > Note there is a space between Hello and WOrld
    >
    > Any help will be greatly appreciated


    // (error checking omitted)
    #include <fstream>
    #include <iostream>
    #include <string>

    int main()
    {
    std::ifstream ifs("infile.txt");
    std::getline(ifs, line);
    std::cout << "string contains: " << line << '\n';
    return 0;
    }

    -Mike
     
    Mike Wahler, Apr 20, 2006
    #2
    1. Advertising

  3. Guest

    Thanks for the reply Mike.
    It worked but what do I need to do if I have like
    hello
    world
    in 2 seperate lines
    Thanks in advance

    Nandini

    wrote:
    > Hello guys,
    > I have a small doubt in istream.
    > I have a file calles infile.txt
    > infile.txt contains Hello World
    > now I wanted to add these contents to a string.
    > ie i need to have hello world in a string
    > Note there is a space between Hello and WOrld
    >
    > Any help will be greatly appreciated
    > Thank You
    > Nandini
     
    , Apr 20, 2006
    #3
  4. benben Guest

    wrote:
    > Thanks for the reply Mike.
    > It worked but what do I need to do if I have like
    > hello
    > world
    > in 2 seperate lines
    > Thanks in advance


    Use another getline to get the next line.

    Regards,
    Ben
     
    benben, Apr 21, 2006
    #4
  5. wrote:

    > Thanks for the reply Mike.
    > It worked but what do I need to do if I have like
    > hello
    > world
    > in 2 seperate lines


    Here's an alternative solution:

    #include <fstream>
    #include <sstream>
    #include <string>
    #include <iostream>

    int main()
    {
    using std::ifstream;
    using std::stringstream;
    using std::string;
    using std::cout;

    ifstream file("Test.txt");
    stringstream buffer;
    buffer << file.rdbuf();
    string contents = buffer.str();

    cout << "Contents: " << contents << '\n';
    }

    Regards,

    --
    Ney André de Mello Zunino
     
    =?ISO-8859-1?Q?Ney_Andr=E9_de_Mello_Zunino?=, Apr 22, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. matthurne
    Replies:
    3
    Views:
    957
    matthurne
    Jul 22, 2004
  2. Gernot Frisch

    from std::string to std::istream?

    Gernot Frisch, Mar 17, 2005, in forum: C++
    Replies:
    4
    Views:
    14,457
    Victor Bazarov
    Mar 18, 2005
  3. Richard Clay
    Replies:
    2
    Views:
    326
    Malte Starostik
    Apr 5, 2005
  4. mos
    Replies:
    3
    Views:
    377
    Jim Langston
    Jun 26, 2006
  5. xmllmx
    Replies:
    5
    Views:
    597
    Jorgen Grahn
    Jun 15, 2010
Loading...

Share This Page