简体   繁体   中英

C++: File I/O having difficulty with opening and working with it

I'm having difficulty opening files and processing what is inside of them. What i want to do is

  1. pull a line from the input file

  2. init an istreamstream with the line

  3. pull each word from the istringstream

    i. process the word

    • do my specific function i've created

    ii. write it to the output file

I'm not sure how to go about doing 1-3 can anyone help with my functions? This is what i have so far...

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);
}
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});
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM