Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
03-14-2003, 02:16 AM
|
#1
|
Member
Registered: Sep 2002
Location: Canada
Distribution: Redhat 9.0
Posts: 637
Rep:
|
Standard C++ fstream question
Say I have a text file called 'data.txt' and it just contains a bunch of integer values. How do I read one integer into a record using a Standard C++ file steam in the called function, and return the stream conditon to the caller. I want to keep calling the GetRecord function, until I have all of the integers from the file, and than I want the loop to terminate. This means that I'll have to change GetRecords return type, but I'm not sure what the type should be, because I have not used C++ file streams in the past.
A trivial example:
Code:
#include<fstream>
//A simple abstact record type
struct rec_t {
int inum;
};
int main() {
//...
struct rec_t record;
while ( GetRecord(record) != 0 ) {
//do stuff with each record
}
//...
return 0;
}
//The function definiton
void GetRecord( struct rec_t& rec ) {
std::ifstream infile("data.txt");
infile >> rec.inum;
}
Last edited by GtkUser; 03-14-2003 at 02:19 AM.
|
|
|
03-14-2003, 08:54 AM
|
#2
|
Member
Registered: Feb 2003
Location: A Meatlocker, well feels like one
Distribution: Gentoo
Posts: 292
Rep:
|
Basically you just need to test the file for EOF
if( !infile ){
}
you will be inside the loop when you reach the end of the file. Also you should move the declaration of your file handler to the main loop and pass it to the function. If you leave it where it is you will always return the same value.
|
|
|
03-14-2003, 10:40 AM
|
#3
|
Member
Registered: Sep 2002
Location: Canada
Distribution: Redhat 9.0
Posts: 637
Original Poster
Rep:
|
Thanks. I was able to do this and it worked. Not sure if this is the best way, but for now it's okay:
Code:
#include<iostream>
#include<fstream>
struct rec_t {
int inum;
};
std::istream& GetRecord( std::istream& infile, struct rec_t& rec ) {
if( infile )
infile >> rec.inum;
return infile;
}
int main() {
struct rec_t record;
std::ifstream infile("data.txt");
while ( GetRecord( infile, record ) ) {
std::cout << record.inum << std::endl;
}
return 0;
}
|
|
|
03-14-2003, 07:03 PM
|
#4
|
Member
Registered: Jan 2003
Posts: 167
Rep:
|
If you don't need to modularise it, I'm pretty sure you can do this:
Code:
int main() {
struct rec_t record;
std::ifstream infile("data.txt");
while ( infile >> record.inum ) {
std::cout << record.inum << std::endl;
}
return 0;
}
|
|
|
03-16-2003, 03:47 PM
|
#5
|
Moderator
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
|
If you are looking for something like
object serialization you might want
to give qt, especially the class
QDataStream
...
Cheers,
Tink
|
|
|
04-12-2003, 11:45 PM
|
#6
|
Member
Registered: Jun 2002
Location: Orange County, CA
Distribution: Debian (squeeze), kernel 2.6.30-2-amd64
Posts: 32
Rep:
|
PTBmilo has the right idea. If you're reading non-character data, you should test for successful extraction instead of EOF by using the boolean nature of the extraction operator. In other words, compare these two situations:
// Loop A:
while (false == cin.eof())
{
cin >> myInt;
sum += myInt;
}
// Loop B:
while (cin >> myInt)
{
sum += myInt;
}
If there is trailing whitespace after the last integer on the input stream, loop "A" will produce an incorrect result. After reading the last integer, the EOF marker will not have been read yet because extraction stops as soon as the first non-integer character is encountered. This leaves the read pointer of the input stream positioned on the whitespace character. Since EOF hasn't been seen yet, the loop iterates one more time; the extraction inside the loop will fail since there's only whitespace left. The "myInt" variable will contain its previous value (since the extraction failed), and "sum" will get an extra "myInt" value added to it. The last (failed) extraction will have advanced the read pointer to the EOF marker, so the loop will finally terminate.
But loop "B" is controlled by testing for successful extraction. Even if there is trailing whitespace after the last integer value, using the extraction operator as the condition expression for the loop means the loop body will only be entered if another integer is successfully read from the input stream.
The essential point is that testing for EOF really only makes sense if you're performing character I/O, otherwise you should test for successful extraction.
|
|
|
All times are GMT -5. The time now is 12:02 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|