首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

怎么读取用tab分隔开的文本文件内容

2012-03-09 
如何读取用tab分隔开的文本文件内容请问各位高手, 我现在有一个文本文件 里面的内容是大体 这样的 :IDNAME

如何读取用tab分隔开的文本文件内容
请问各位高手, 我现在有一个文本文件 里面的内容是 大体 这样的 :
  ID NAME AGE SCORE
  100 张三 18 100
  101 李四 19 100
  102 小丽 19 98
  **************************
  在文本里面他们都是用 TAB 隔开的,大概有几十万条记录 我用 CStdioFile.ReadString(CString); 读取文本之后 应该如何 把 每一条记录里面的各字段内容放在我设定的结构体里面?  

  struct node  
  {  
  int ID;
  char name[20];
  int age;
  bool sex;
  .........
  }  




[解决办法]

C/C++ code
FILE *stream = fopen( "data.txt", "r+" );fscanf(stream,"%d%s%d%d",&st[i].ID,st[i].name,&st[i].age,&st[i].score);//读入一列用循环读
[解决办法]
最简单的办法就是把流中的字符一个个的读,判断是不是制表符或换行,不是的话写到缓冲,是制表符的话从缓冲区中读字符串,是换行的话就写一个新的结构,循环这个过程。
[解决办法]
你看一下这个:http://topic.csdn.net/u/20120116/17/b747a1b8-4f2f-46be-8834-aa6df33705ec.html
昨天我解决的,很类似,连数据都很类似
[解决办法]
C/C++ code
#include <iostream>#include <sstream>#include <string>#include <fstream>using namespace std;void Load(istream &file){    while(!file.eof())    {        char line[256];        file.getline(line, 255);        stringstream ss(line);        int ID;        char name[20];        int age;        bool sex;        ss >> ID >> name >> age >> sex;        cout << ID << name << age << sex << endl;    }}        int main(){        ifstream file("test.txt");    if(!file)    {        cout << "Cannot open file:" << "test.txt" << endl;        return -1;    }    Load(file);        return 0;}
[解决办法]
C/C++ code
string replace(string &src, string find, string replace){    string reply;    int i = 0;    int len = find.length();    while(1)    {        int start = i;        i = src.find_first_of(find, i);        if(i < 0)        {                reply += src.substr(start);            break;                }        reply += src.substr(start, i - start);        reply += replacement;        i += len;    }    return reply;} 

热点排行