Как перевести все первые буквы предложений в верхний регистр после указанного разделителя в текстовом файле?

Как следует из названия, у меня есть текстовый файл, в котором практически нет заглавных букв, поэтому все предложения не выглядят правильно, если первая буква не заглавная. Вот мой код:

    //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. Я пока не знаю, где его установить.


person Sam Peterson    schedule 08.10.2014    source источник
comment
Почему вы ищете пустую строку: input = str.find('.');?   -  person Thomas Matthews    schedule 08.10.2014
comment
Попробуйте это: while (getline(datafile, input, '.')) Вы не можете поместить более одного символа между одинарными кавычками, используйте двойные кавычки.   -  person Thomas Matthews    schedule 08.10.2014
comment
То есть точка и пробел считаются двумя символами?   -  person Sam Peterson    schedule 08.10.2014
comment
Да, точка - это символ. Пробел — это символ. Вместе они два персонажа.   -  person Thomas Matthews    schedule 08.10.2014
comment
Все становится еще сложнее, если вы принимаете во внимание символы, отличные от ASCII. Для полноценного решения Unicode потребуется хорошая библиотека Unicode.   -  person dalle    schedule 08.10.2014


Ответы (1)


Вместо этого

    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 для таких проблем.

person Ravi Shenoy    schedule 08.10.2014