reading from a file into an array

A

Allen Seelye

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.
 
M

Moonlit

Hi,

Allen Seelye said:
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.
 
M

Moonlit

Sorry forgot

#include <fstream>

Regards,
Moonlit said:
Hi,

Allen Seelye said:
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.
 
A

Allen Seelye

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.
 
R

Rolf Magnus

Allen said:
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";
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,477
Members
44,898
Latest member
BlairH7607

Latest Threads

Top