Re: Reading a file

Discussion in 'C++' started by Karl Heinz Buchegger, Jul 3, 2003.

  1. Allan Bruce wrote:
    >
    > Hi there,
    > I am slowly learning c++ after having studied c for some time. My question
    > is, how do I read a file in c++? My (crappy) book does not cover any file
    > i/o. I know how to do it in c, but not in c++. I wish to read the entire
    > contents of a file into a char array. How would I do this?


    By not using a char array :)

    Seriously: You can do the very same thing you do in C.
    But a C++ solution would use the stream classes and of course
    std::string and probably std::vector or some other container:

    std::ifstream in_file( ... );
    std::string in_line;
    std::vector< std::string > content;

    while( getline( in_file, in_line ) )
    content.push_back( in_line );


    What book are you using that does not cover reading from streams?

    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Jul 3, 2003
    #1
    1. Advertising

  2. Karl Heinz Buchegger

    Allan Bruce Guest

    "Karl Heinz Buchegger" <> wrote in message
    news:...
    >
    >
    > Allan Bruce wrote:
    > >
    > > Hi there,
    > > I am slowly learning c++ after having studied c for some time. My

    question
    > > is, how do I read a file in c++? My (crappy) book does not cover any

    file
    > > i/o. I know how to do it in c, but not in c++. I wish to read the

    entire
    > > contents of a file into a char array. How would I do this?

    >
    > By not using a char array :)
    >
    > Seriously: You can do the very same thing you do in C.
    > But a C++ solution would use the stream classes and of course
    > std::string and probably std::vector or some other container:
    >
    > std::ifstream in_file( ... );
    > std::string in_line;
    > std::vector< std::string > content;
    >
    > while( getline( in_file, in_line ) )
    > content.push_back( in_line );
    >
    >
    > What book are you using that does not cover reading from streams?


    er, (emabarassingly) "Teach yourself c++ in 24 hours"
     
    Allan Bruce, Jul 3, 2003
    #2
    1. Advertising

  3. Karl Heinz Buchegger

    Chris Theis Guest

    "Allan Bruce" <> wrote in message
    news:be169c$vq0$2surf.net...
    >

    [SNIP]
    >
    > >
    > > What book are you using that does not cover reading from streams?

    >
    > er, (emabarassingly) "Teach yourself c++ in 24 hours"
    >


    Do yourself a favor and get a decent textbook. I'd recommen "Accelerated
    C++" by A. Koenig and B. Moo or "Thinking C++" from Bruce Eckel ( which you
    can check out on the web http://www.bruceeckel.com/).

    Regards
    Chris
     
    Chris Theis, Jul 3, 2003
    #3
    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. Darrel
    Replies:
    3
    Views:
    688
    Kevin Spencer
    Nov 11, 2004
  2. Luis Esteban Valencia Muñoz
    Replies:
    3
    Views:
    1,421
    Scott Allen
    Jun 4, 2005
  3. Replies:
    0
    Views:
    798
  4. Karim Ali

    Reading a file and resuming reading.

    Karim Ali, May 25, 2007, in forum: Python
    Replies:
    2
    Views:
    381
    Hrvoje Niksic
    May 25, 2007
  5. =?ISO-8859-2?Q?Miros=B3aw?= Makowiecki

    Reading of file by next of map file and by next of file descriptor.

    =?ISO-8859-2?Q?Miros=B3aw?= Makowiecki, Jul 10, 2007, in forum: C++
    Replies:
    1
    Views:
    809
    Alf P. Steinbach
    Jul 10, 2007
Loading...

Share This Page