ProgrammingThis 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.
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.
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;
}
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.