C++ 如何将文本文件中指定分隔符后的所有句子首字母字符大写?
正如标题所说,我的文本文件中几乎没有大写字母,因此如果没有首字母大写,所有句子看起来都不合适。以下是我目前的代码:C++ 如何将文本文件中指定分隔符后的所有句子首字母字符大写?,c++,string,toupper,C++,String,Toupper,正如标题所说,我的文本文件中几乎没有大写字母,因此如果没有首字母大写,所有句子看起来都不合适。以下是我目前的代码: //This program reads an article in a text file, and changes all of the //first-letter-of-sentence-characters to uppercase after a period and space. #include <iostream> #include <f
//This program reads an article in a text file, and changes all of the
//first-letter-of-sentence-characters to uppercase after a period and space.
#include <iostream>
#include <fstream>
#include <string>
#include <cctype>//for toupper
using namespace std;
int main()
{
//Variable needed to read file:
string str;
string input = str.find('.');
//Open the file:
fstream dataFile("eBook.txt", ios::in);
if (!dataFile)
{
cout << "Error opening file.\n";
return 0;
}
//Read lines terminated by '. ' sign, and then output:
getline(dataFile, input, '. ');//error: no instance of overloaded function "getline"
//matches the argument list
//argument types are:(std::fstream, std::string, int)
while (!dataFile.fail())
{
cout << input << endl;
getline(dataFile, input);
}
//Close the file:
dataFile.close();
return 0;
}
。
注意:我知道我的代码中还没有toupper关键字。我还不知道在哪里设置它。而不是这个
getline(dataFile, input, '. ');
while (!dataFile.fail())
{
cout << input << endl;
getline(dataFile, input);
}
你可以把它改成
while(getline(dataFile, line, '.'))
{
for(auto &i : line )
{
if(!isspace(i))
{
i = toupper(i);
break;
}
}
outFile<<line<<".";
}
PS:我更喜欢使用正则表达式来解决这类问题。为什么要搜索空字符串:input=str.find'.'?尝试以下操作:在getlinedatafile中输入“.”不能在单引号之间放置多个字符,请使用双引号。因此句点和空格被视为两个字符?是的,句点是一个字符。空格是一个字符。它们加在一起就是两个字符。如果考虑到非ASCII字符,事情会变得更加复杂。一个成熟的Unicode解决方案需要一个好的Unicode库。