I am trying to open a file and parse a file with separate functions.
when i parse the file, i want to read each line in my input file and i want to ignore any punctuation and make everything lowercase so that i can then print it out to a separate file into strings with no spaces or punctuation.
i have tried to implement it but i cannot figure out what i am doing wrong. I am not getting an error but my output is not correct.
this is my code:
#include <iostream>
#include <fstream>
#include <ostream>
#include <sstream>
#include <string>
using namespace std;
void processFile();
void parseFile(ifstream&, ofstream&);
int main()
{
//call function to open file and process
cout << "processing file" << endl;
processFile();
return 0;
}
void processFile()
{
string newString;
ifstream inFile;
ofstream outFile;
cout << "opening files" << endl;
// open files
inFile.open("inFile.txt");
outFile.open("outFile.txt");
cout << "parsing file" << endl;
//parse file for capitalization & punctuation
parseFile(inFile, outFile);
//close files
outFile.close();
inFile.close();
}
void parseFile(ifstream &inFile, ofstream &outFile)
{
//create and initialize variables
string newString;;
int i = 0;
if(!inFile)
{
cout << "ERROR!!! Cannot read file.";
}
else
{
do
{
//read each line in the input file until EOF
getline(inFile, newString, '\n');
//parse each string for punctuation
while(newString[i])
{
if(isalpha(newString[i])) //check each char in each
//string for punctuation
{
if(isupper(newString[i])) //check each string for
//capitalization
{
newString[i] = tolower(newString[i]); //convert
//string to
//lower case
}
outFile << newString[i]; //output each line to file
cout << newString[i];
}
i++;
if(newString[i] == '\n')
break;
}
} while(!inFile.eof());
}
}
this is my input:
racecar
RACEcar
rotator
rotor
civic
Red rum, sir, is murder!
Rats live on no evil star.
Neil, a trap! Sid is part alien!
Step on no pets.
Dammit, I’m mad!
my expected output is everything in the input printed in lowercase, without punctuation and spaces
this is my actual output:
racecarsirismurderraliena
You need to reinitialize i after each call to getline(), otherwise it will continue at the offset where the previous string ended. getline() overwrites newString every time it's called.
do
{
//read each line in the input file until EOF
getline(inFile, newString, '\n');
i = 0;
//parse each string for punctuation
If you compare your input with your output it makes sense.
The first string "racecar" is parsed without problem. Now i is 7.
The next 4 calls to getline() get strings that are shorter than 7 so you get no new output in outFile.txt.
The call to getline() after that gets "Red rum, sir, is murder!", starting from 7 you get "sirismurder" and i is now 24.
The next call to getline() gets "Rats live on no evil star.", starting from 24 you get "r" and i is is now 26
The last call to getline() gets "Neil, a trap! Sid is part alien!", starting from 26 you get "alien".