在 C++ 中打开和解析文件以检查标点符号并使用两个函数转换为小写
opening and parsing a file in c++ to check for punctuation and to convert to lower case using two functions
我正在尝试打开一个文件并用单独的函数解析一个文件。
当我解析文件时,我想读取输入文件中的每一行,我想忽略任何标点符号并将所有内容都设为小写,这样我就可以将它打印到一个单独的文件中,变成没有空格的字符串或标点符号。
我已经尝试实现它,但我无法弄清楚我做错了什么。我没有收到错误,但我的输出不正确。
这是我的代码:
#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());
}
}
这是我的输入:
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!
我的预期输出是以小写形式打印的输入中的所有内容,没有标点符号和空格
这是我的实际输出:
racecarsirismurderraliena
您需要在 getline()
之后初始化 i
。这是因为每次调用 getline()
都会导致从输入文件读取的每一行数据被覆盖(而不是附加)到 newString
.
do
{
//read each line in the input file until EOF
getline(inFile, newString, '\n');
i = 0; // <== initialize 'i' to zero
//parse each string for punctuation
while(newString[i])
{
您需要在每次调用getline() 后重新初始化i,否则它会在前一个字符串结束的偏移处继续。每次调用 getline() 时都会覆盖 newString。
do
{
//read each line in the input file until EOF
getline(inFile, newString, '\n');
i = 0;
//parse each string for punctuation
如果您将输入与输出进行比较,这是有道理的。
第一个字符串"racecar"解析没有问题。现在我7岁了。
接下来 4 次调用 getline() 得到的字符串少于 7 个,因此您在 outFile.txt 中没有得到新的输出。
之后对 getline() 的调用得到 "Red rum, sir, is murder!",从 7 开始得到 "sirismurder",我现在是 24。
下一次调用 getline() 得到 "Rats live on no evil star.",从 24 开始你得到 "r" 而我现在是 26
最后一次调用 getline() 得到 "Neil, a trap! Sid is part alien!",从 26 开始你得到 "alien"。
我正在尝试打开一个文件并用单独的函数解析一个文件。
当我解析文件时,我想读取输入文件中的每一行,我想忽略任何标点符号并将所有内容都设为小写,这样我就可以将它打印到一个单独的文件中,变成没有空格的字符串或标点符号。
我已经尝试实现它,但我无法弄清楚我做错了什么。我没有收到错误,但我的输出不正确。
这是我的代码:
#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());
}
}
这是我的输入:
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!
我的预期输出是以小写形式打印的输入中的所有内容,没有标点符号和空格
这是我的实际输出:
racecarsirismurderraliena
您需要在 getline()
之后初始化 i
。这是因为每次调用 getline()
都会导致从输入文件读取的每一行数据被覆盖(而不是附加)到 newString
.
do
{
//read each line in the input file until EOF
getline(inFile, newString, '\n');
i = 0; // <== initialize 'i' to zero
//parse each string for punctuation
while(newString[i])
{
您需要在每次调用getline() 后重新初始化i,否则它会在前一个字符串结束的偏移处继续。每次调用 getline() 时都会覆盖 newString。
do
{
//read each line in the input file until EOF
getline(inFile, newString, '\n');
i = 0;
//parse each string for punctuation
如果您将输入与输出进行比较,这是有道理的。
第一个字符串"racecar"解析没有问题。现在我7岁了。
接下来 4 次调用 getline() 得到的字符串少于 7 个,因此您在 outFile.txt 中没有得到新的输出。
之后对 getline() 的调用得到 "Red rum, sir, is murder!",从 7 开始得到 "sirismurder",我现在是 24。
下一次调用 getline() 得到 "Rats live on no evil star.",从 24 开始你得到 "r" 而我现在是 26
最后一次调用 getline() 得到 "Neil, a trap! Sid is part alien!",从 26 开始你得到 "alien"。