★★★VC++读取文本文件里的数据到两个数组中★★
1,0.056183,0.015906,0.003513,0.000283,0.000003,0.000828,0.1118111,0.020147,0.005431,0...
1, 0.056183, 0.015906, 0.003513, 0.000283, 0.000003, 0.000828, 0.111811
1, 0.020147, 0.005431, 0.001509, 0.000059, 0.000003, 0.000797, 0.111808
1, 0.034398, 0.009768, 0.001874, 0.000130, 0.000003, 0.000805, 0.111808
1, 0.069287, 0.019784, 0.005178, 0.000442, 0.000003, 0.000851, 0.111788
2, 0.026044, 0.001199, 0.001091, 0.000081, 0.000003, 0.000794, 0.111806
2, 0.090909, 0.008505, 0.003210, 0.001535, 0.000004, 0.000909, 0.111562
2, 0.115152, 0.002632, 0.006299, 0.001627, 0.000004, 0.000825, 0.111374
2, 0.073055, 0.001612, 0.001854, 0.000210, 0.000003, 0.000798, 0.111816
3, 0.354464, 0.198575, 0.028486, 0.024844, 0.000663, 0.012116, 0.113298
3, 0.552498, 0.364998, 0.240247, 0.210239, 0.047230, 0.130700, 0.010317
3, 0.264865, 0.137833, 0.013999, 0.008875, 0.000101, 0.004152, 0.111612
3, 0.448321, 0.263610, 0.042394, 0.038381, 0.001551, 0.020586, 0.111953
3, 0.218182, 0.099159, 0.001695, 0.000309, 0.000003, 0.000875, 0.111797
3, 0.280098, 0.134837, 0.002879, 0.004435, 0.000018, 0.002441, 0.112025
3, 0.451433, 0.261783, 0.084898, 0.045366, 0.002778, 0.019255, 0.069580
4, 0.212613, 0.098557, 0.012569, 0.007624, 0.000077, 0.003238, 0.111885
4, 0.264865, 0.113646, 0.005411, 0.002057, 0.000009, 0.001334, 0.111674
4, 0.267486, 0.132535, 0.015797, 0.009446, 0.000117, 0.004269, 0.110818
4, 0.233743, 0.105773, 0.023915, 0.013395, 0.000242, 0.005248, 0.111885
4, 0.273055, 0.132115, 0.003436, 0.002413, 0.000009, 0.001539, 0.111798
~~~ ………………………………………………………………………………………………………………………………………………
类别 特征
标识
上面是数据文件的一部分,其中第一列是类别标识,每一行从第二个数据起是特征属性。
首先,你要用C++语言写一个函数,从文本文件中将特征数据读入一个二维数组中,将类别标识读入一个一维数组中。 展开
1, 0.020147, 0.005431, 0.001509, 0.000059, 0.000003, 0.000797, 0.111808
1, 0.034398, 0.009768, 0.001874, 0.000130, 0.000003, 0.000805, 0.111808
1, 0.069287, 0.019784, 0.005178, 0.000442, 0.000003, 0.000851, 0.111788
2, 0.026044, 0.001199, 0.001091, 0.000081, 0.000003, 0.000794, 0.111806
2, 0.090909, 0.008505, 0.003210, 0.001535, 0.000004, 0.000909, 0.111562
2, 0.115152, 0.002632, 0.006299, 0.001627, 0.000004, 0.000825, 0.111374
2, 0.073055, 0.001612, 0.001854, 0.000210, 0.000003, 0.000798, 0.111816
3, 0.354464, 0.198575, 0.028486, 0.024844, 0.000663, 0.012116, 0.113298
3, 0.552498, 0.364998, 0.240247, 0.210239, 0.047230, 0.130700, 0.010317
3, 0.264865, 0.137833, 0.013999, 0.008875, 0.000101, 0.004152, 0.111612
3, 0.448321, 0.263610, 0.042394, 0.038381, 0.001551, 0.020586, 0.111953
3, 0.218182, 0.099159, 0.001695, 0.000309, 0.000003, 0.000875, 0.111797
3, 0.280098, 0.134837, 0.002879, 0.004435, 0.000018, 0.002441, 0.112025
3, 0.451433, 0.261783, 0.084898, 0.045366, 0.002778, 0.019255, 0.069580
4, 0.212613, 0.098557, 0.012569, 0.007624, 0.000077, 0.003238, 0.111885
4, 0.264865, 0.113646, 0.005411, 0.002057, 0.000009, 0.001334, 0.111674
4, 0.267486, 0.132535, 0.015797, 0.009446, 0.000117, 0.004269, 0.110818
4, 0.233743, 0.105773, 0.023915, 0.013395, 0.000242, 0.005248, 0.111885
4, 0.273055, 0.132115, 0.003436, 0.002413, 0.000009, 0.001539, 0.111798
~~~ ………………………………………………………………………………………………………………………………………………
类别 特征
标识
上面是数据文件的一部分,其中第一列是类别标识,每一行从第二个数据起是特征属性。
首先,你要用C++语言写一个函数,从文本文件中将特征数据读入一个二维数组中,将类别标识读入一个一维数组中。 展开
3个回答
展开全部
其中Index[]就是你要的类别标识数组,Property[][3]就是特征属性数组
假设你的数据文件是C:\11.txt
#include "stdio.h"
#include "iostream.h"
#include "string.h"
#include "stdlib.h"
#define N 100
void main()
{
FILE *f;
int Index[N],i=0,j;
float Property[N][7];
char str[100],*pNext;
f=fopen("C:\\11.txt","r");//打开文件
if(f==NULL) {cout<<"Error, file don't open!"<<endl; return;}//如果文件没有打开就退出报错
while(!feof(f))//检测是否读到了文件结尾
{
fgets(str,100,f);//以文本形式从文件中读出一行
Index[i]=atoi(str);//将类别标识转换为整型数,赋值
pNext=str;
for(j=0;j<7;j++)
{
pNext=strchr(pNext+1,',');//搜索数据间的分隔符
Property[i][j]=atof(pNext+1);//将类别标识转换为浮点型数,赋值
}
i++;
}
fclose(f);//关闭文件。
}
假设你的数据文件是C:\11.txt
#include "stdio.h"
#include "iostream.h"
#include "string.h"
#include "stdlib.h"
#define N 100
void main()
{
FILE *f;
int Index[N],i=0,j;
float Property[N][7];
char str[100],*pNext;
f=fopen("C:\\11.txt","r");//打开文件
if(f==NULL) {cout<<"Error, file don't open!"<<endl; return;}//如果文件没有打开就退出报错
while(!feof(f))//检测是否读到了文件结尾
{
fgets(str,100,f);//以文本形式从文件中读出一行
Index[i]=atoi(str);//将类别标识转换为整型数,赋值
pNext=str;
for(j=0;j<7;j++)
{
pNext=strchr(pNext+1,',');//搜索数据间的分隔符
Property[i][j]=atof(pNext+1);//将类别标识转换为浮点型数,赋值
}
i++;
}
fclose(f);//关闭文件。
}
展开全部
遇见多维数组头都大了,你还搞这么多数组.还是自己研究吧
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
嗯,突然明白LZ的意思了
#include <stdio.h>
void main()
{
int a[100],i,j;
float b[100][100];
FILE *fp;
fp=fopen("data.txt","r");
//假设数据存于data.txt
if(fp!=NULL)
{
for(i=0;i<=4*4;i++)
{
fscanf(fp,"%d, %f, %f, %f, %f, %f, %f, %f",&a[i],&b[i][0],&b[i][1],&b[i][2],&b[i][3],&b[i][4],&b[i][5],&b[i][6]);
printf("%d,%f,%f,%f,%f,%f,%f,%f\n",a[i],b[i][0],b[i][1],b[i][2],b[i][3],b[i][4],b[i][5],b[i][6]);
}
}
else
{
printf("can not open the file data.txt\n");
}
}
#include <stdio.h>
void main()
{
int a[100],i,j;
float b[100][100];
FILE *fp;
fp=fopen("data.txt","r");
//假设数据存于data.txt
if(fp!=NULL)
{
for(i=0;i<=4*4;i++)
{
fscanf(fp,"%d, %f, %f, %f, %f, %f, %f, %f",&a[i],&b[i][0],&b[i][1],&b[i][2],&b[i][3],&b[i][4],&b[i][5],&b[i][6]);
printf("%d,%f,%f,%f,%f,%f,%f,%f\n",a[i],b[i][0],b[i][1],b[i][2],b[i][3],b[i][4],b[i][5],b[i][6]);
}
}
else
{
printf("can not open the file data.txt\n");
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询