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