[英]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
我想做的是
pull a line from the input file 从输入文件中拉出一行
init an istreamstream with the line 用该行初始化一个istreamstream
pull each word from the istringstream 从istringstream中拉出每个单词
i. 一世。 process the word
处理单词
ii. ii。 write it to the output file
将其写入输出文件
I'm not sure how to go about doing 1-3 can anyone help with my functions? 我不确定如何进行1-3操作,有人可以帮助我完成我的功能吗? 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});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.