Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Issues with fstream

Issues with fstream
Thread Tools
Senior User
Join Date: Jun 1999
Location: San Jose, CA
Status: Offline
Reply With Quote
Mar 13, 2004, 10:44 PM
 
I am having some issues with fstream. I am using ProjectBuilder currently. My problem is that I want to open a few files, and do some processing on them. First time through, things go fine. However every further attempt goes nowhere, as the "cursor position" is always at the end of the file. I thought that closing and opening would reset teh cursor position, but no. So i tried forcing the cursor position (using seekp), but it doesn't do anything. My code is below, so any help with getting the cursor back at the top so I can run through a new file is much appriciated.

Code:
fstream fileToScan; .... while ( currentFile < numOfFiles ) { fileToScan.open( fileNames[currentFile], ifstream::in ); fileToScan.seekp(ios::beg); while ( !fileToScan.eof() ) { fileToScan >> currentWord; i = 0; while ( i < currentWord.length() ) { currentWord[i] = tolower(currentWord[i]); i++; } if ( queryWord == currentWord ) { currentWordCount++; anyFound = TRUE; } } if ( currentWordCount != 0 ) { reportFile << fileNames[currentFile]; reportFile << " (" << currentWordCount << ")\n"; } fileToScan.close(); currentWordCount = 0; currentFile++; }
     
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
Mar 14, 2004, 12:33 AM
 
Well, seekp() is an ostream function. If you're doing input, you'd want to use istream's seekg(). Also, I'm not sure streams are reusable. I don't have a whole lot of C++ experience, but I've never heard of anyone reusing them before.
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
Senior User
Join Date: Jun 1999
Location: San Jose, CA
Status: Offline
Reply With Quote
Mar 14, 2004, 01:09 AM
 
Originally posted by Chuckit:
Well, seekp() is an ostream function. If you're doing input, you'd want to use istream's seekg(). Also, I'm not sure streams are reusable. I don't have a whole lot of C++ experience, but I've never heard of anyone reusing them before.
I tried seekg as well, but no luck there either.

As far as reusability, if that is the case, how would you recommend I open multiple files? The thing is, I don't know how many files will be read in right now (they are specified on the command line). So if I can't reuse the same stream what would be a better way to read in multiple files, and take action on them?
     
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
Mar 14, 2004, 01:19 AM
 
Originally posted by kupan787:
As far as reusability, if that is the case, how would you recommend I open multiple files? The thing is, I don't know how many files will be read in right now (they are specified on the command line). So if I can't reuse the same stream what would be a better way to read in multiple files, and take action on them?
Create the stream inside your file-reading loop, so a stream is created for each file. Like so:
Code:
for (int i = 0; i < numberOfFiles; ++i) { ifstream reader(fileNames[i]); // Do your file parsing }
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
   
Thread Tools
Forum Links
Forum 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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 09:02 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2