C ++: файловый ввод / вывод с трудностями при открытии и работе с ним

0

Мне трудно открывать файлы и обрабатывать то, что внутри них. Что я хочу сделать, это

  1. вытащить строку из входного файла

  2. init поток istream с линией

  3. вытащить каждое слово из istringstream

    я. обрабатывать слово

    • выполните мою конкретную функцию, которую я создал

    II. записать его в выходной файл

Я не уверен, как сделать 1-3, может ли кто-нибудь помочь с моими функциями? Это то, что у меня есть до сих пор...

string process_word(ifstream &inFile){
    string line, empty_str = "";
    while (getline(inFile,line)){
        empty_str += line;
    }
    return empty_str;
}

int main(){
    string scrambled_msg = "", input, output, line, word, line1, cnt;
    cout << "input file: ";
    cin >> input;
    cout << "output file: ";
    cin >> output;

    ifstream inFile(input);
    ofstream outFile(output);

    cout << process_word(inFile);
}
Теги:
file
c++11
function
iostream

1 ответ

0
std::vector<std::string> process_word(std::ifstream& in)
{
    std::string line;
    std::vector<std::string> words;

    while (std::getline(in, line)) // 1
    {
        std::istringstream iss{line}; // 2

        std::move(std::istream_iterator<std::string>{in},
                  std::istream_iterator<std::string>{},
                  std::back_inserter(words));
    }

    return words;
}

int main()
{
    std::ifstream in(file);
    std::ofstream out(file);

    auto words = process_word(in);

    for (auto word : words)
        // 3 i.

    std::move(words.begin(), words.end(), // 3 ii.
              std::ostream_iterator<std::string>{out});
}

Ещё вопросы

Сообщество Overcoder
Наверх
Меню