文件的操作

xumesang 2014-12-12 12:08:10
我有两个文件,a.txt,和b.txt,a.txt的内容如下:
-1 1:0.642104853998493 2:0.286303260307055
-1 1:0.406633346777957 2:0.254545454545455
-1 1:0.00123721678553904 2:0.277777777777778
......
b.txt的内容如下:
-1
-1
1
......
现在需要统计a.txt每行的第一个数字是否和b.txt对应行的内容相同;
给出相同的个数。用C++实现,只要详细代码,谢谢
...全文
149 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
忘世麒麟 2014-12-12
  • 打赏
  • 举报
回复
打开文件 //文件读(将文件的内容读出) ifstream finA("D:\\A.txt"); ifstream finB("D:\\B.txt"); 从文件中读取一行 string strA,strB; finA.getline(strA); finB.getline(strB); 比较两个数是否相等 if(strA[0] == strB[0]) { ....... } 关闭文件: finA.close() finB.close() ------------------------- 关键步骤 int count = 0;; while(finA.getline(strA)&&finB.getline(strB)) { if(strA[0] == strB[0]) count++; //统计出来的行数 }
xumesang 2014-12-12
  • 打赏
  • 举报
回复
引用 1 楼 sinservice 的回复:
不做这道题也没关系,大不了不及格。
这有不是考试,只是我遇到的一个问题待解决
xumesang 2014-12-12
  • 打赏
  • 举报
回复
引用 2 楼 baijiaheizhiganmao 的回复:
这么简单......
帮帮忙啊,我是菜鸟
忘世麒麟 2014-12-12
  • 打赏
  • 举报
回复
这么简单......
「已注销」 2014-12-12
  • 打赏
  • 举报
回复
不做这道题也没关系,大不了不及格。
忘世麒麟 2014-12-12
  • 打赏
  • 举报
回复
引用 10 楼 xumesang 的回复:
[quote=引用 5 楼 baijiaheizhiganmao 的回复:] 打开文件 //文件读(将文件的内容读出) ifstream finA("D:\\A.txt"); ifstream finB("D:\\B.txt"); 从文件中读取一行 string strA,strB; finA.getline(strA); finB.getline(strB); 比较两个数是否相等 if(strA[0] == strB[0]) { ....... } 关闭文件: finA.close() finB.close() ------------------------- 关键步骤 int count = 0;; while(finA.getline(strA)&&finB.getline(strB)) { if(strA[0] == strB[0]) count++; //统计出来的行数 }
请问这个strA[0]指的是“-1 1:0.642104853998493 2:0.286303260307055”里的“-1”还是“-”?[/quote] 是-1,这种情况你在处理一下。 假如按照你的数据的格式的话,应该是从strA中截取出第一个字符到第一个空格的部分(string有专门的方法)然后直接和strB比较,不用带下标了。
xumesang 2014-12-12
  • 打赏
  • 举报
回复
引用 5 楼 baijiaheizhiganmao 的回复:
打开文件 //文件读(将文件的内容读出) ifstream finA("D:\\A.txt"); ifstream finB("D:\\B.txt"); 从文件中读取一行 string strA,strB; finA.getline(strA); finB.getline(strB); 比较两个数是否相等 if(strA[0] == strB[0]) { ....... } 关闭文件: finA.close() finB.close() ------------------------- 关键步骤 int count = 0;; while(finA.getline(strA)&&finB.getline(strB)) { if(strA[0] == strB[0]) count++; //统计出来的行数 }
请问这个strA[0]指的是“-1 1:0.642104853998493 2:0.286303260307055”里的“-1”还是“-”?
忘世麒麟 2014-12-12
  • 打赏
  • 举报
回复
引用 6 楼 xumesang 的回复:
[quote=引用 5 楼 baijiaheizhiganmao 的回复:] 打开文件 //文件读(将文件的内容读出) ifstream finA("D:\\A.txt"); ifstream finB("D:\\B.txt"); 从文件中读取一行 string strA,strB; finA.getline(strA); finB.getline(strB); 比较两个数是否相等 if(strA[0] == strB[0]) { ....... } 关闭文件: finA.close() finB.close() ------------------------- 关键步骤 int count = 0;; while(finA.getline(strA)&&finB.getline(strB)) { if(strA[0] == strB[0]) count++; //统计出来的行数 }
请先看懂题目,我只比较a.txt中每一行的第一个string,不是正行比较[/quote] “需要统计a.txt每行的第一个数字是否和b.txt对应行的内容相同”妈蛋我不管了
xumesang 2014-12-12
  • 打赏
  • 举报
回复
好吧,是我没看懂,我错了
xumesang 2014-12-12
  • 打赏
  • 举报
回复
是第一个word,不是一整行
xumesang 2014-12-12
  • 打赏
  • 举报
回复
引用 5 楼 baijiaheizhiganmao 的回复:
打开文件 //文件读(将文件的内容读出) ifstream finA("D:\\A.txt"); ifstream finB("D:\\B.txt"); 从文件中读取一行 string strA,strB; finA.getline(strA); finB.getline(strB); 比较两个数是否相等 if(strA[0] == strB[0]) { ....... } 关闭文件: finA.close() finB.close() ------------------------- 关键步骤 int count = 0;; while(finA.getline(strA)&&finB.getline(strB)) { if(strA[0] == strB[0]) count++; //统计出来的行数 }
请先看懂题目,我只比较a.txt中每一行的第一个string,不是正行比较

64,690

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧