C语言 txt文件 为什么读取不了
#include<stdio.h>#include<stdlib.h>#include<math.h>#include<ctype.h>#include<string.h...
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <string.h>
#define Row 150
FILE *fp1;
FILE *fp;
struct point
{
double x1;
double x2;
double x3;
double x4;
int cluster;
};
int i,j,k;
struct point p[Row];
void main()
{
fp1=fopen("K-mean data.txt","r+");
if(fp1==NULL)
{
printf("cannot open file\n");
exit(0);
}
for(i=0;i<Row;i++)
{
fscanf(fp1,"%f,%f,%f,%f\n",&p[i].x1,&p[i].x2,&p[i].x3,&p[i].x4);
}
fclose(fp1);
fp=fopen("result.txt","w");
for(i=0;i<Row;i++)
{
fprintf(fp,"%f,%f,%f,%f,%d\n",&p[i].x1,&p[i].x2,&p[i].x3,&p[i].x4,&p[i].cluster);
}
} 展开
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <string.h>
#define Row 150
FILE *fp1;
FILE *fp;
struct point
{
double x1;
double x2;
double x3;
double x4;
int cluster;
};
int i,j,k;
struct point p[Row];
void main()
{
fp1=fopen("K-mean data.txt","r+");
if(fp1==NULL)
{
printf("cannot open file\n");
exit(0);
}
for(i=0;i<Row;i++)
{
fscanf(fp1,"%f,%f,%f,%f\n",&p[i].x1,&p[i].x2,&p[i].x3,&p[i].x4);
}
fclose(fp1);
fp=fopen("result.txt","w");
for(i=0;i<Row;i++)
{
fprintf(fp,"%f,%f,%f,%f,%d\n",&p[i].x1,&p[i].x2,&p[i].x3,&p[i].x4,&p[i].cluster);
}
} 展开
3个回答
推荐于2016-03-26
展开全部
虽然不知道你文件数据的格式,不过你可以把
fscanf(fp1,"%f,%f,%f,%f\n",&p[i].x1,&p[i].x2,&p[i].x3,&p[i].x4);
改成这样fscanf(fp1,"%lf%lf%lf%lf\n",&p[i].x1,&p[i].x2,&p[i].x3,&p[i].x4);或者这样
fscanf(fp1,"%lf,%lf,%lf,%lf\n",&p[i].x1,&p[i].x2,&p[i].x3,&p[i].x4);试一试
fscanf(fp1,"%f,%f,%f,%f\n",&p[i].x1,&p[i].x2,&p[i].x3,&p[i].x4);
改成这样fscanf(fp1,"%lf%lf%lf%lf\n",&p[i].x1,&p[i].x2,&p[i].x3,&p[i].x4);或者这样
fscanf(fp1,"%lf,%lf,%lf,%lf\n",&p[i].x1,&p[i].x2,&p[i].x3,&p[i].x4);试一试
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询