By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,134 Members | 1,942 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,134 IT Pros & Developers. It's quick & easy.

reading from a file into an array

P: n/a
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
As*****@blackfoot.net

Jul 19 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Hi,

"Allen Seelye" <As*****@blackfoot.net> wrote in message
news:bo*********@enews3.newsguy.com...
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
As*****@blackfoot.net

Jul 19 '05 #2

P: n/a
Sorry forgot

#include <fstream>

Regards,
"Moonlit" <al******@jupiter.universe> wrote in message
news:3f***********************@news.xs4all.nl...
Hi,

"Allen Seelye" <As*****@blackfoot.net> wrote in message
news:bo*********@enews3.newsguy.com...
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
As*****@blackfoot.net


Jul 19 '05 #3

P: n/a
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";
}

Jul 19 '05 #4

P: n/a
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
As*****@blackfoot.net

Jul 19 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.