 |
 |
Issues with fstream
|
 |
|
 |
|
Senior User
Join Date: Jun 1999
Location: San Jose, CA
Status:
Offline
|
|
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
|
|
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
|
|
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
|
|
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'."
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|