【发布时间】:2014-01-14 18:35:03
【问题描述】:
我正在尝试使用矢量来操作文件 (.txt) 的元素 - 例如 push_back、accessing [] 等。我想对从文件中检索到的元素执行简单的编码,但我部分成功了最终的结果不是我所期望的。
这是文件内容(示例):
datum
-6353
argo
我想在这里执行的是读取向量的每个元素(有点二维)并用它们各自的ASCII 代码对它们进行编码。
这是我的代码(到目前为止):
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <iterator>
using namespace std;
int main ()
{
std::string word;
//std::vector<std::vector <std::string> > file;
std::vector<std::string> file;
std::ifstream in( "test.txt" );
while (in >> word)
{
file.push_back(word);
}
for (size_t i=0; i<file.size(); i++)
{
for (int j=0;j<file[i].size();j++)
{
//cout<<i<<","<< j<<endl;
cout<<((int)file[i][j])<<",";
}
}
in.close();
return 0;
}
当前输出(对于上述代码):
100,97,116,117,109,45,54,51,53,51,97,114,103,111,
RUN FINISHED; exit value 0; real time: 0ms; user: 0ms; system: 0ms
预期输出:
100,97,116,117,109
45,54,51,53,51
97,114,103,111
请指导我实现这一目标。
【问题讨论】: