c/c++语言开发共享读取CSV文件存入map中(C++)

自己平时操作文件用的不多,今天小伙伴让帮忙写一下这个,顺便记一下。实现功能:从"翻译.csv"文件中读取出字符串,以","作为分隔符,将每一行对应存入map中。 代码: 运行结果: …

  自己平时操作文件用的不多,今天小伙伴让帮忙写一下这个,顺便记一下。实现功能:从”翻译.csv”文件中读取出字符串,以”,”作为分隔符,将每一行对应存入map中。

代码:

#include <iostream>  #include <fstream>  #include <string>  #include <vector>  #include <map>    using namespace std;    int main() {      ifstream infile("翻译.csv", ios::in);      if (!infile.is_open())      {          cerr << "can't open the file" << endl;      }      string linestr;      map<string, string> translation;      while (getline(infile,linestr))      {          // 分割字符串          int index = linestr.find(",");          string english = linestr.substr(0, index);          string chinese = linestr.substr(index+1, linestr.size()-1);          // 存入map          translation[english] = chinese;      }        //输出      for (map<string, string>::iterator iter = translation.begin(); iter!=translation.end();iter++)      {          cout << iter->first << iter->second << endl;      }      system("pause");      return 0;  }

运行结果:

读取CSV文件存入map中(C++)

 

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/c-cdevelopment/603418.html

(0)
上一篇 2021年5月11日
下一篇 2021年5月11日

精彩推荐