read line

K

kak3012

Hi,

I have a text file I will read it and write out binary. The file includes
256 coloums.

I use

while (infile.good())
{
infile.getline (buffer,2200);
cout<<buffer<<endl<<endl;
exit(0);
}

so I can see the file.

Each field is seperated by 1 TAB or 2 TABS I mean \t.

Is there a way to read this file at once into an array, line by line. They
are all numbers.

Regards...
 
J

Jerry Coffin

kak3012 said:
Hi,

I have a text file I will read it and write out binary. The file includes
256 coloums.

I use

while (infile.good())
{
infile.getline (buffer,2200);
cout<<buffer<<endl<<endl;
exit(0);
}

so I can see the file.

Each field is seperated by 1 TAB or 2 TABS I mean \t.

Is there a way to read this file at once into an array, line by line. They
are all numbers.

It's not entirely clear whether you want an array of numbers, or an
array of strings, each representing a single line from the file. For
an array (or technically, a vector, but you rarely want an array
anyway) of numbers, you could do something like this:

typedef double type; // use type appropriate to your numbers.

std::vector<type> numbers;

std::copy(std::istream_iterator<type>(infile),
std::istream_iterator<type>(),
std::back_inserter(numbers));

If you want the numbers kept as strings, but still want each as an item
in the vector, you can just change 'type' from 'double' to
'std::string'.

Oddly enough, if you want each LINE as a string in an array, it's a
little more difficult (though not much). You can do it a little like
above by creating a class that acts like a string, except that
operator>> reads a whole line instead of a single token. It's usually
easier, however, to do the job explicitly, with something like:

std::vector<std::string> numbers;
std::string temp;

while(infile.good()) {
std::getline(infile, temp);
numbers.push_back(temp);
}

A few notes about C++ in general: as mentioned above, especially to
start with, you probably won't encounter many reasons to use arrays --
you'll usually want to use vectors instead (or std::string instead of
arrays of char). Going along with that, you rarely want to use
istream::getline -- you'll almost always want to use std::getline
instead, because you'll be reading into a string instead of an array of
char. The first bit of code I have above probably looks rather foreign
to you right now, since you've probably never seen or used those parts
of the standard library yet -- but studying them is an effort that will
be well worthwhile, at least IMO. You can certainly read doubles (for
example) into a vector with code much like the second piece above --
but I think it's worth the work to learn how to do it the first way
I've given above instead.
 

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
474,201
Messages
2,571,049
Members
47,655
Latest member
eizareri

Latest Threads

Top