C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

read line

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
kak3012
Guest





PostPosted: Thu Feb 24, 2005 9:13 pm    Post subject: read line Reply with quote



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


Back to top
saper
Guest





PostPosted: Fri Feb 25, 2005 8:11 am    Post subject: Re: read line Reply with quote



long double ld=50;
printf("%Lg",ld);

and display show -6.8065E+038
no 50


Back to top
Jerry Coffin
Guest





PostPosted: Fri Feb 25, 2005 4:28 pm    Post subject: Re: read line Reply with quote



kak3012 wrote:
Quote:
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< 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
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.


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.