I am needing to parse through a text file and output the vowels within this textfile line by line into a separate file. This all needs to be done using one filestream at a time.
I open the input stream using fstream ios:: in & ios::binary, read in one line using getline, clear & close that stream then open my output fstream using ios::out & ios::app, find the vowels and output them one by one as they are found. Then close output stream and open the input stream. Done using seekg to put the file at the end of the prior line in hopes that getline will read in the consecutive line.
fstream a(fileName, ios::in | ios::binary | ios::ate);
string OF = "vowels_" + fileName;
while (count < size && count != -1) {
count = a.tellg();
a.close();
fstream b(OF, ios::out| ios::app);
for (int i = 0; i < line.length(); i++) {
if (line.at(i) == '\n'){b << '\n';}
if (line.at(i) == 'a' || line.at(i) == 'e' || line.at(i) == 'i' || line.at(i) == 'o' || line.at(i) == 'u') {
b << line.at(i); vowels+= line.at(i);
}
}
if(vowels.empty()){b << endl;}
if (line.empty()){b << endl;}
b.close();
fstream a(fileName, ios::in | ios::binary | ios::ate);
}
fstream a(fileName, ios::in | ios::binary | ios::ate);at the end of the loop does not re-initializea, it creates a newfstream.When you get to the start of the next iteration,
ais closed.You are overcomplicating this.
Work line by line in text mode, keep track of how many lines you've read, and skip over that many lines on each iteration.
Something like this: