0

正如标题所说,我的文本文件中几乎没有大写字母,所以如果没有首字母大写,所有句子看起来都不正确。到目前为止,这是我的代码:

    //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 关键字。我还不知道在哪里设置它。

4

1 回答 1

0

而不是这个

    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:我更喜欢使用 RegEx 来解决这类问题。

于 2014-10-08T07:53:17.377 回答