LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 11-07-2003, 08:01 PM   #1
dilberim82
Member
 
Registered: Apr 2001
Location: NY
Distribution: used to be Redhat, now Debian Sarge
Posts: 291

Rep: Reputation: 30
C++ question


Hi guys,
I have a C++ project and i ran into a problem. I looked in ten different books and i cannot find an answer. I need to read a file and figure out how many numbers are in the file, then read the numbers to an array. The problem is, after i count how many numbers are in the file, i cannot read it again. I tried:

fin.close()

then open the file again but it does not work. Any ideas how to get around this? Thanks
 
Old 11-07-2003, 08:31 PM   #2
dakensta
Member
 
Registered: Jun 2003
Location: SEUK
Distribution: Debian & OS X
Posts: 194

Rep: Reputation: 35
Is it a binary or text file?

And if binary, do you know the type of numbers (bytes per value) and do you have any assurance about what format the numbers are in?

You have a few options, depending on the file structure, the most simple being just to keep reading until the file ends. Stick each number as it arrives in one of the standard containers (maybe a list), as this will expand to your needs, using its push_back() function.

Code:
using namespace std;

ifstream ifs( "yourfile" );
int temp;
list<int> data;
while( ifs >> temp )
   data.push_back( data );

OR

ifstream ifs( "yourfile", ifstream::in | ifstream::binary );
int temp;
list<int> data;
while( ifs.rdbuf()->sgetn( reinterpret_cast<char*>(&temp), sizeof( temp ) ) )
   list.push_back( temp );

MAYBE

// I am not sure just how reliable this is... so I would check in one of your books (sorry, bit tired to do it myself)
// I think there is a more reliable way but I can't find the code atm
  ifs.seekg ( 0, ios::end );
  int length = is.tellg();
  ifs.seekg ( 0, ios::beg );

Last edited by dakensta; 11-07-2003 at 08:43 PM.
 
Old 11-07-2003, 08:45 PM   #3
dilberim82
Member
 
Registered: Apr 2001
Location: NY
Distribution: used to be Redhat, now Debian Sarge
Posts: 291

Original Poster
Rep: Reputation: 30
Here is my main method... File contains integers. We are learning dynamic arrays and i have to figure out how many numbers are in the file so i can set the array size. When i compile the program with the rest of the stuff it runs, but it cannot open the file for the second time. And i am not sure if i can count how many numbers are in the file while i read the numbers in the array.

int main()
{
int arraySize = 1, next;
char filename[30];
cout << "Enter the name of the file: ";
cin >> filename;

// Opens the file to count the arraysize
ifstream fin;
fin.open(filename);
if(fin.fail())
{
cout << "Input file could not be opened 1\n";
exit(1);
}

fin >> next;
//Counts how many numbers are in the file
while (!fin.eof())
{
arraySize++;
fin >> next;
}
fin.close();
cout << "there are" << arraySize << "numbers in file";

myArray a;
a = new int[arraySize];

getArray(a, arraySize);

delete []a;

return 0;
}

Last edited by dilberim82; 11-07-2003 at 08:51 PM.
 
Old 11-07-2003, 09:34 PM   #4
dakensta
Member
 
Registered: Jun 2003
Location: SEUK
Distribution: Debian & OS X
Posts: 194

Rep: Reputation: 35
The ifstream, fin, has it's fail bit set as it has reached the end of the file - reopening the file does not clear this - try: if( fin ) cout << "OK\n"; after getting arraySize

After getting the arraySize value either call fin.clear() and then fin.open( "your file" ) or use another ifstream: ifstream fin2( "yourfile" ) to reopen the file.

Also - change this line : while( !fin.eof() ) to while( fin >> next )

the reason for this is that the eof() might not return true until the value AFTER the end of the file has tried to be read so you end up with an extra iteration.

You will hopefully learn about standard containers in the not too distant future ... after you have learnt all the basics things get a lot easier

Last edited by dakensta; 11-07-2003 at 09:36 PM.
 
Old 11-07-2003, 09:53 PM   #5
dilberim82
Member
 
Registered: Apr 2001
Location: NY
Distribution: used to be Redhat, now Debian Sarge
Posts: 291

Original Poster
Rep: Reputation: 30
thx alot

I did not know i could call fin.clear(). i tried opening the file again with fin2 but it did not work... Thanks alot though . I am going to try fin.clear() now
 
Old 11-08-2003, 09:08 AM   #6
dakensta
Member
 
Registered: Jun 2003
Location: SEUK
Distribution: Debian & OS X
Posts: 194

Rep: Reputation: 35
Quote:
i tried opening the file again with fin2 but it did not work...
Can you post this code?

I am hesitant to post a complete solution as the "We are learning dynamic arrays" suggests homework and it would be a shame to compromise your education
 
Old 11-08-2003, 11:02 AM   #7
h/w
Senior Member
 
Registered: Mar 2003
Location: New York, NY
Distribution: Debian Testing
Posts: 1,286

Rep: Reputation: 46
why would you want to open the file twice in the first place?
open it once for reading, as and when it encounters a number, push it into the array, and close.
cant you use vectors for ur dyn array?
(or maybe even do a malloc everytime you need to push a number into the array, and then use a pointer if needed - im talking C here - havent done C++ for a while now.)
 
Old 11-09-2003, 11:33 AM   #8
dilberim82
Member
 
Registered: Apr 2001
Location: NY
Distribution: used to be Redhat, now Debian Sarge
Posts: 291

Original Poster
Rep: Reputation: 30
We did not learn vectors yet and with the information i have i dont think i can do because i am going to get out of the loop then create the array. I am not sure but after i quit the loop and create an array i think it'd destroy the data. I'll give it a shot monday though just to learn.

BTW: Thanks alot dakensta. not only you solved my problem, but my program works better now. I used to read -8(million?) for the spaces i have at the end of the file and even my teacher did not know how to fix it.
 
Old 11-09-2003, 01:07 PM   #9
dakensta
Member
 
Registered: Jun 2003
Location: SEUK
Distribution: Debian & OS X
Posts: 194

Rep: Reputation: 35
Glad it all works now.

Just to follow from h/w's comment, you don't have to open the file twice - if you call fin.seekg( 0, ifstream::beg ) you will be taken back to the start of the file.

This version (it is overloaded) of seekg works like this : seekg( offset, offset_from ) where offset_from can be beginning, current position or end.
 
  


Reply



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
Question, Apples Contribution to Open Source + MacOs file structure question Higgy3k Other *NIX 5 07-25-2005 04:23 AM
Not your regular GRUB question - just a short question for a fried MBR!! ziphem Linux - General 3 01-31-2005 01:51 PM
2 part question: Speeding up MDK9.1/GNOME question wardialer Linux - Newbie 6 10-14-2004 03:16 PM
login prompt question & kde scheme question JustinCoyan Slackware 2 06-09-2004 02:02 PM
RE: Suse 8.0 hardware question {newbie question, pls help} Radiouk Linux - Distributions 2 06-04-2002 12:53 PM

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

All times are GMT -5. The time now is 04:48 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