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

read line

P: n/a
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...
Jul 23 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
long double ld=50;
printf("%Lg",ld);

and display show -6.8065E+038
no 50
Jul 23 '05 #2

P: n/a
kak3012 wrote:
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.

--
Later,
Jerry.

The universe is a figment of its own imagination.

Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.