读入.txt中两列数据

afjafjafj2008 2011-06-15 02:15:10
data.txt

360 0xED,0x33
3DM 0x33,0x44
3DR 0x48,0x61
3DXML 0x3C,0x3F
ACI 0x52,0x6F
ACP 0xD4,0xC3
ADM 0x43,0x4C
ADR 0x4F,0x70
KSUDOKU 0x3C,0x21
KW3 0x52,0x00

中间是tab键,怎么读放两列数据?(数组,map都可以)
...全文
102 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
afjafjafj2008 2011-06-15
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <string>
#include <iostream>
#include <map>

using namespace std;

int main(void)
{
FILE *oldfile, *newfile1, *newfile2;
char oldname[50] = "c:\\trid020.txt";
char newname1[50] = "c:\\test1.txt";
char newname2[50] = "c:\\test2.txt";
char a[20];
char b[20];
map<string, string> maptype;
map<string, string>::iterator iter;

if((oldfile = fopen(oldname, "r+")) == NULL)
perror("Open error!");

if((newfile1 = fopen(newname1, "w+")) == NULL)
perror("Open error!");

if((newfile2 = fopen(newname2, "w+")) == NULL)
perror("Open error!");

while(fscanf(oldfile, "%s%s", &a, &b) == 2)
{
fprintf(newfile1, "%s\n", a);
fprintf(newfile2, "%s\n", b);
maptype.insert(pair<string, string>(a, b));
memset(a, 0, 10);
memset(b, 0, 10);
}

for (iter=maptype.begin(); iter!=maptype.end(); iter++)
{
cout<<iter->first<<"--"<<iter->second<<endl;
}

fclose(oldfile);
fclose(newfile1);
fclose(newfile2);
return 0;
}


好了!
luciferisnotsatan 2011-06-15
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 luciferisnotsatan 的回复:]

读放哪两列数据?360 和 0xED,0x33?
用fscanf();格式化字符串用 "%s %s" 就行了
[/Quote]
前提要保证后面那列0xED,0x33中不会有空白符
luciferisnotsatan 2011-06-15
  • 打赏
  • 举报
回复
读放哪两列数据?360 和 0xED,0x33?
用fscanf();格式化字符串用 "%s %s" 就行了
bdmh 2011-06-15
  • 打赏
  • 举报
回复

FILE* f=fopen("c:\\1.txt","r");
char a[10],b[10];
while(!feof(f))
{
fscanf(f,"%s %s",a,b);
}
c_losed 2011-06-15
  • 打赏
  • 举报
回复
思路:

一次读取一行
然后查找\t
最后分开存入

64,439

社区成员

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

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