LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 03-14-2003, 02:16 AM   #1
GtkUser
Member
 
Registered: Sep 2002
Location: Canada
Distribution: Redhat 9.0
Posts: 637

Rep: Reputation: 30
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.
 
Old 03-14-2003, 08:54 AM   #2
Palin
Member
 
Registered: Feb 2003
Location: A Meatlocker, well feels like one
Distribution: Gentoo
Posts: 292

Rep: Reputation: 30
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.
 
Old 03-14-2003, 10:40 AM   #3
GtkUser
Member
 
Registered: Sep 2002
Location: Canada
Distribution: Redhat 9.0
Posts: 637

Original Poster
Rep: Reputation: 30
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;
}
 
Old 03-14-2003, 07:03 PM   #4
PTBmilo
Member
 
Registered: Jan 2003
Posts: 167

Rep: Reputation: 30
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;
}
 
Old 03-16-2003, 03:47 PM   #5
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
If you are looking for something like
object serialization you might want
to give qt, especially the class
QDataStream
...

Cheers,
Tink
 
Old 04-12-2003, 11:45 PM   #6
ludwig
Member
 
Registered: Jun 2002
Location: Orange County, CA
Distribution: Debian (squeeze), kernel 2.6.30-2-amd64
Posts: 32

Rep: Reputation: 15
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
c++ fstream:: question javakid Programming 2 01-21-2005 01:03 PM
Lilo Question: Windows from a non-standard partition failure_man Slackware 13 12-12-2004 11:38 AM
Telnet to non standard port question MadPenguin Linux - Networking 2 04-21-2004 10:17 AM
Mount windows share - not standard question hopefully! Flibble Linux - General 4 10-17-2002 10:22 AM
standard vs Expert install? Standard flubs up! Frost Linux - Software 1 03-27-2002 07:55 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:44 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration