VC++中调用文本文件数据
我想把一个格式为.txt里的数据读到数组中去,但是运行时不报错也不显示数据,不知道哪里有问题,哪位大侠知道的话请指点一下,万分感激!程序如下所示:voidCWSView:...
我想把一个格式为.txt里的数据读到数组中去,但是运行时不报错也不显示数据,不知道哪里有问题,哪位大侠知道的话请指点一下,万分感激!
程序如下所示:
void CWSView::OnTXTdata()
{
// TODO: Add your command handler code here
/*
#include "stdio.h"
#include "iostream.h"
#include "string.h"
#include "stdlib.h"
*/
#define N 8
FILE *f;
int Index[N],i=0,j;
double Property[N][10];
char str[8],*pNext;
f=fopen(" C:\\11.txt","r");//打开文件
if(f==NULL) {cout<<"Error, file don't open!"<<endl; return;}//如果文件没有打开就退出报错
while(!feof(f))//检测是否读到了文件结尾
{
fgets(str,8,f);//以文本形式从文件中读出一行
Index[i]=atoi(str);//将类别标识转换为整型数,赋值
pNext=str;
for(j=0;j<10;j++)
{
pNext=strchr(pNext+1,',');//搜索数据间的分隔符
Property[i][j]=atof(pNext+1);//将属性值转化为为浮点型数,赋值
}
i++;
}
fclose(f);//关闭文件。
CString aaa; //检测数据是否已经进入数组
aaa .Format( " %f ",Property[0][1]);
MessageBox( aaa);
}
记事本里的数据如下:(我是存在桌面上的)
1, 1.165058,488435903,12.624524,1.227569,-0.241418,0.313309,29.671624,0.295257,0.834603,0.51633
2, 44.958586,164.426042,2.532960,9.781250,-0.071961,21.048896,94.933568,2.425042,2.322002,0.110473
3, 6.156897,49.916181,6.137582,4.293264,-1.096729,4.220809,20.711.87,1.631536,2.745403,0.650255
4, 1.313779,253.824236,12.909426,2.066736,-0.508483,0.951059,85.677853,0.766792,1.641812,0.162697
5, 62.565949,143.977014,-4.261669,10.970069,3.717741,36.071925,49.566582,3.751988,3.527789,1.101520
6, 1.125404,411.115208,12.009650,1.527292,7.461904,0.174898,23.010923,0.317719,0.515347,1.091824
7, 2.685612,439.701389,10.480918,1.775278,-0.595395,1.370035,111.324478,0.400682,0.754117,0.298095
8, 1.028165,456,1.3056,12.769018,1.058819,-0.728003,0.228638,27.983478,0.106284,0.585423,0.390469 展开
程序如下所示:
void CWSView::OnTXTdata()
{
// TODO: Add your command handler code here
/*
#include "stdio.h"
#include "iostream.h"
#include "string.h"
#include "stdlib.h"
*/
#define N 8
FILE *f;
int Index[N],i=0,j;
double Property[N][10];
char str[8],*pNext;
f=fopen(" C:\\11.txt","r");//打开文件
if(f==NULL) {cout<<"Error, file don't open!"<<endl; return;}//如果文件没有打开就退出报错
while(!feof(f))//检测是否读到了文件结尾
{
fgets(str,8,f);//以文本形式从文件中读出一行
Index[i]=atoi(str);//将类别标识转换为整型数,赋值
pNext=str;
for(j=0;j<10;j++)
{
pNext=strchr(pNext+1,',');//搜索数据间的分隔符
Property[i][j]=atof(pNext+1);//将属性值转化为为浮点型数,赋值
}
i++;
}
fclose(f);//关闭文件。
CString aaa; //检测数据是否已经进入数组
aaa .Format( " %f ",Property[0][1]);
MessageBox( aaa);
}
记事本里的数据如下:(我是存在桌面上的)
1, 1.165058,488435903,12.624524,1.227569,-0.241418,0.313309,29.671624,0.295257,0.834603,0.51633
2, 44.958586,164.426042,2.532960,9.781250,-0.071961,21.048896,94.933568,2.425042,2.322002,0.110473
3, 6.156897,49.916181,6.137582,4.293264,-1.096729,4.220809,20.711.87,1.631536,2.745403,0.650255
4, 1.313779,253.824236,12.909426,2.066736,-0.508483,0.951059,85.677853,0.766792,1.641812,0.162697
5, 62.565949,143.977014,-4.261669,10.970069,3.717741,36.071925,49.566582,3.751988,3.527789,1.101520
6, 1.125404,411.115208,12.009650,1.527292,7.461904,0.174898,23.010923,0.317719,0.515347,1.091824
7, 2.685612,439.701389,10.480918,1.775278,-0.595395,1.370035,111.324478,0.400682,0.754117,0.298095
8, 1.028165,456,1.3056,12.769018,1.058819,-0.728003,0.228638,27.983478,0.106284,0.585423,0.390469 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询