reading from a file into an array

Discussion in 'C++' started by Allen Seelye, Nov 7, 2003.

  1. Allen Seelye

    Allen Seelye Guest

    Newbie here. I'm having a hell of a time with this. It takes input from
    the console into an array and then dumps it into a file. that all is
    pretty straight forward and works just fine. The output file looks like
    this:

    1,John Smith, 5551234, 1515 Some St
    2,Jane Smith, 5555678, 1515 Someother St

    ....and so on. I've had no success getting this read back into an array.
    I've got three books on C++ and none of them has any information on how
    to accomplish this. Even if someone could show me how to read each line
    in as a single string I could probably figure it out from there on my own.

    Any help would be greatly appreciated.

    --
    Allen Seelye
    Allen Seelye, Nov 7, 2003
    #1
    1. Advertising

  2. Allen Seelye

    Moonlit Guest

    Hi,

    "Allen Seelye" <> wrote in message
    news:...
    > Newbie here. I'm having a hell of a time with this. It takes input from
    > the console into an array and then dumps it into a file. that all is
    > pretty straight forward and works just fine. The output file looks like
    > this:
    >
    > 1,John Smith, 5551234, 1515 Some St
    > 2,Jane Smith, 5555678, 1515 Someother St


    #include <string>
    #include <algorithm>
    using namespace std;

    ifstream Input( Filename.c_str() );

    if( !Input.is_open() )
    {
    // Error handling
    }
    else
    {
    string Line;
    while( getline( Input, Line ) )
    {

    }
    }

    Regards, Ron AF Greve.
    >
    > ...and so on. I've had no success getting this read back into an array.
    > I've got three books on C++ and none of them has any information on how
    > to accomplish this. Even if someone could show me how to read each line
    > in as a single string I could probably figure it out from there on my own.
    >
    > Any help would be greatly appreciated.
    >
    > --
    > Allen Seelye
    >
    >
    Moonlit, Nov 7, 2003
    #2
    1. Advertising

  3. Allen Seelye

    Moonlit Guest

    Sorry forgot

    #include <fstream>

    Regards,
    "Moonlit" <> wrote in message
    news:3fabe927$0$58700$4all.nl...
    > Hi,
    >
    > "Allen Seelye" <> wrote in message
    > news:...
    > > Newbie here. I'm having a hell of a time with this. It takes input from
    > > the console into an array and then dumps it into a file. that all is
    > > pretty straight forward and works just fine. The output file looks like
    > > this:
    > >
    > > 1,John Smith, 5551234, 1515 Some St
    > > 2,Jane Smith, 5555678, 1515 Someother St

    >
    > #include <string>
    > #include <algorithm>
    > using namespace std;
    >
    > ifstream Input( Filename.c_str() );
    >
    > if( !Input.is_open() )
    > {
    > // Error handling
    > }
    > else
    > {
    > string Line;
    > while( getline( Input, Line ) )
    > {
    >
    > }
    > }
    >
    > Regards, Ron AF Greve.
    > >
    > > ...and so on. I've had no success getting this read back into an array.
    > > I've got three books on C++ and none of them has any information on how
    > > to accomplish this. Even if someone could show me how to read each line
    > > in as a single string I could probably figure it out from there on my

    own.
    > >
    > > Any help would be greatly appreciated.
    > >
    > > --
    > > Allen Seelye
    > >
    > >

    >
    >
    Moonlit, Nov 7, 2003
    #3
  4. Allen Seelye

    Allen Seelye Guest

    Thanks! I think I can go from there just fine.
    You're right about the books. I'm ready to toss them all. I can't find
    one that explains things very well. In all 3 books, the sections on file
    I/O are less than a page and the information that is there is lousy. All
    the online tutorials I've found have been just as bad.

    Anyway, thanks again.


    --
    Allen Seelye
    Allen Seelye, Nov 7, 2003
    #4
  5. Allen Seelye

    Rolf Magnus Guest

    Allen Seelye wrote:

    > Newbie here. I'm having a hell of a time with this. It takes input
    > from the console into an array and then dumps it into a file. that all
    > is pretty straight forward and works just fine. The output file looks
    > like this:
    >
    > 1,John Smith, 5551234, 1515 Some St
    > 2,Jane Smith, 5555678, 1515 Someother St
    >
    > ...and so on. I've had no success getting this read back into an
    > array. I've got three books on C++ and none of them has any
    > information on how to accomplish this.


    Then throw them all away. This is pretty basic stuff.

    > Even if someone could show me how to read each line in as a single
    > string I could probably figure it out from there on my own.


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

    int main()
    {
    std::ifstream file("thefile.txt");
    std::string s;

    while (std::getline(file, s))
    std::cout << "Got a line: " << s << '\n';

    if (!file.eof())
    std::cout << "Error reading file\n";
    }
    Rolf Magnus, Nov 7, 2003
    #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. Row
    Replies:
    0
    Views:
    468
  2. Foxy Kav
    Replies:
    6
    Views:
    7,783
    tom_usenet
    Apr 28, 2004
  3. Replies:
    1
    Views:
    11,304
    Dan Andrews
    Sep 29, 2006
  4. Replies:
    7
    Views:
    417
  5. yourmycaffiene

    reading data from a file into a 2d array

    yourmycaffiene, Feb 23, 2006, in forum: C Programming
    Replies:
    29
    Views:
    1,202
    Ben Pfaff
    Mar 6, 2006
Loading...

Share This Page