您的位置:程序门 -> vc/mfc -> 基础类



求救!用mfc如何把文本文件读成数组?//


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


求救!用mfc如何把文本文件读成数组?//[已结贴,结贴人:cailei0596]
发表于:2007-07-06 09:22:01 楼主
文本文件中全是数字,如
232   32323   5,222
3212   ,323232   323232,434
4343,11


分隔符又有逗号又有空格,怎么搞定?
发表于:2007-07-06 09:31:331楼 得分:0
template_info   a;
cfile   file;
int   k;

if(file.open(strpath,cfile::moderead,null))
{
cstring   tmpstr;
tchar*   tbuf=tmpstr.getbuffer(file.getlength());
file.read(tbuf,file.getlength());
tmpstr=tbuf;
int   l;
int   k=0;

for(int   j=0;j <file.getlength();j++)  
{      
if(*(tbuf+j)==48)
{
float   d=atof(tmpstr.mid(j,8));
a.center[l]=d;
l++;
j=j+9;
if(l==64)
k=1;
if(l> 64)
{
afxmessagebox(_t( "聚类中心数据读取错误! "));
return;
}
if(classfytemplate.getsize()> 20)
{
afxmessagebox(_t( "聚类种数读取错误! "));
return;
}
}
else
{
if(k!=0)
{
classfytemplate.add(a);
k=0;
}
hanzi_info   m;
m.name=tmpstr.mid(j,2);
a.template1.push_back(m);
l=0;
j++;
}
if(classfytemplate.getsize()==19   &&   k!=0)
{
classfytemplate.add(a);
k=0;
}
}  
}

我以前用过的哈,你看看吧
发表于:2007-07-06 09:39:402楼 得分:0
你这段码有什么用啊?
发表于:2007-07-06 10:31:303楼 得分:0
1楼的码看不明白,还有没有高手来接分啊?
发表于:2007-07-06 10:39:084楼 得分:0
我的思路是先查找所有的 ", ",然后把逗号替换为空格.不知道这办法行不行?
发表于:2007-07-06 10:43:295楼 得分:10
cfile   fl;
...
cstring   wholefile;
char   *pbuf=wholefile.getbuffer(fl.getlength());
fl.read(pbuf,fl.getlength());

char   seps[]= ",   \t\n ";
char   *token=strtok(pbuf,seps);
float   *parray=new   float[fl.getlength()/2];
for(int   i=0;token!=null;token=strtok(null,seps);i++)
{
*(parray+i)=atof(token);

}
发表于:2007-07-06 10:44:176楼 得分:0
for(int   i=0;token!=null;token=strtok(null,seps),i++)
{
*(parray+i)=atof(token);

}
发表于:2007-07-06 10:45:527楼 得分:10
msdn里的:

example

/*   strtok.c:   in   this   program,   a   loop   uses   strtok
  *   to   print   all   the   tokens   (separated   by   commas
  *   or   blanks)   in   the   string   named   "string ".
  */

#include   <string.h>
#include   <stdio.h>

char   string[]   =   "a   string\tof   ,,tokens\nand   some     more   tokens ";
char   seps[]       =   "   ,\t\n ";
char   *token;

void   main(   void   )
{
      printf(   "%s\n\ntokens:\n ",   string   );
      /*   establish   string   and   get   the   first   token:   */
      token   =   strtok(   string,   seps   );
      while(   token   !=   null   )
      {
            /*   while   there   are   tokens   in   "string "   */
            printf(   "   %s\n ",   token   );
            /*   get   next   token:   */
            token   =   strtok(   null,   seps   );
      }
}


output

a   string       of   ,,tokens
and   some     more   tokens

tokens:
  a
  string
  of
  tokens
  and
  some
  more
  tokens

发表于:2007-07-06 10:53:518楼 得分:0
分解串函数strtok  
这个在vc++6.0中可以运行吗?????
发表于:2007-07-06 10:58:329楼 得分:0
7楼的才是正解哦,马上给分


快速检索

最新资讯
热门点击