0

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);
}
David G
  • 94,763
  • 41
  • 167
  • 253
Tyler
  • 1,933
  • 5
  • 18
  • 23
  • Not too good with C++, could really use the help! Thanks guys – Tyler Oct 15 '13 at 02:00
  • So what is your problem? – Brandon Buck Oct 15 '13 at 02:02
  • I can't get my process_word function to work so it output's the words in the file! – Tyler Oct 15 '13 at 02:03
  • Your `process_word` function reads the entire contents of a file. I don't see any attempt to process the words. Are you asking us to do that part for you? – Brandon Buck Oct 15 '13 at 02:08
  • How should i go about processing the words then? – Tyler Oct 15 '13 at 02:08
  • Your goal is to tokenize the file in the form of words, there are a lot of examples that do that. http://stackoverflow.com/questions/275355/c-reading-file-tokens is one example of how to such a thing. It's best practice to ask questions here about an issue or error you are getting, not so much about "How do I do X?" Part of the requirements for a question is a basic understanding of the problem being solved. Give Google a shot before posting a question. If you have trouble implementing an example you've found, ask why you're getting such and such error and we'll be happy to help. – Brandon Buck Oct 15 '13 at 02:12
  • Ok thank you, sorry for making it such an obscure question. I've scoured google from answers but I don't understand still. I will keep looking, sorry again! – Tyler Oct 15 '13 at 02:17
  • The link I gave you demonstrates the exact result you wish to receive. Read over the answers provided. And as an aside I'd also like to mention that you should name your variables meaningfully (i.e. something that alludes to their purpose). I remark about `emtpy_str` which at first glance appears to be used to store and empty `string` type but it actually goes on to whole the contents of the file. Maybe something like `file_contents` would be a better name. Good code comments itself! – Brandon Buck Oct 15 '13 at 02:20

1 Answers1

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});
}
David G
  • 94,763
  • 41
  • 167
  • 253