在C C++中比较和匹配单词/excel文件中的数字
Compare and match numbers from a word/excel file in C C++
我必须比较单词文件中的两列,这两列逐行填充数字。例如,如果有一个单词文件有以下两列:
1 56
2 57
3 59
4 63
然后,如果我给出输入2,那么在C C++中输出应该是57。什么逻辑有助于映射表并使用其值?请帮忙。到目前为止,我想不出任何办法。我正在使用这段代码逐行读取文件。
#include <stdio.h>
int main(int argc, char* argv[])
{
char const* const fileName = argv[1]; /* should check that argc > 1 */
FILE* file = fopen("C:\Users\parth\Downloads\state_model_files.txt", "r"); /* should check the result */
char line[256];
while (fgets(line, sizeof(line), file)) {
/* note that fgets don't strip the terminating n, checking its
presence would allow to handle lines longer that sizeof(line) */
printf("%s", line);
}
/* may check feof here to make a difference between eof and io failure -- network
timeout for instance */
fclose(file);
getchar();
return 0;
}
基本上,您想要做的是(1)打开文件,(2)从文件中获取一行,(3)将每列的值写入某个变量,(4)将它们存储到映射中。稍后,您可以进行类似您在帖子中建议的查询。请注意,下面的方法对您的输入数据进行了某些假设。
#include <iostream>
#include <sstream>
#include <fstream>
#include <map>
using namespace std;
int main(int argc, char *argv[]) {
ifstream in(argv[1]);
if(!in) {
cout << "Could not open file " << argv[1] << endl;
return -1;
}
map<int, int> m;
int c1, c2;
string fields;
while(getline(in, fields)) {
istringstream ss(fields);
ss >> c1 >> c2;
m[c1] = c2;
}
in.close();
return 0;
}
相关文章:
- 如何从excel文件中读取数据并存储到变量中?
- 错误写入Excel文件-C++
- C++使用 adodb 在 64 位 Windows 7 中读取 Excel 文件?
- 在C J2ME中读取Excel文件,并从ITQ获取数据
- 使用C 在Excel文件中阅读的建议
- 打开现有的 Excel 文件 libxl
- 使用 MFC 时导出到 Excel 文件
- C 如何通过同一MFC应用程序通过System Command打开Excel文件后关闭CMD EXE
- 使用visualstudio在c++中读取excel文件
- 如何用C++将数据写入Excel文件
- 用c++读取excel文件(通用方向)
- 如何在 C++ 上使用互操作创建 Excel 文件
- 如何创建-编辑-删除 excel 文件
- 在C C++中比较和匹配单词/excel文件中的数字
- C#程序,用于使用关闭前事件处理程序自动处理excel,而不保存编辑后的excel文件
- 用c++读写excel文件
- 使用OleContainer保存时损坏的Excel文件
- 在Windows应用程序中从rdl/rdlc文件中写入Excel文件
- 在mac上使用system()打开excel文件
- Libxl从excel文件中读取整数