一个文件输入数据 终端打印显示乱码
#include<stdio.h>#include<stdlib.h>
#define SET 20
#define LENGTH 13
void Init(double (*w)[LENGTH]);
void Input_file(double (*w)[LENGTH]);
void Write_file(double (*w)[LENGTH]);
void Print(double (*w)[LENGTH]);
FILE *fp;
int main()
{
double Weight[SET][LENGTH];
double Weight1[SET][LENGTH];
Init(Weight);
printf("orginal info as follow...\n");
Print(Weight);
Write_file(Weight);
Input_file(Weight1);
printf("Renew show in file !\n");
Print(Weight1);
return 0;
}
void Input_file(double (*w)[LENGTH])
{
int i,j;
if( (fp=fopen("king.txt","r"))==NULL )
{
printf("cannot open file !\n");
return ;
}
for(i=0;i<SET;i++)
{
for(j=0;j<LENGTH;j++)
{
fscanf(fp,"%f",&(w[i][j]));
}
}
fclose(fp);
}
void Write_file(double (*w)[LENGTH])
{
int i,j;
if( (fp=fopen("king.txt","w"))==NULL )
{
printf("cannot open file !\n");
return ;
}
for(i=0;i<SET;i++)
{
for(j=0;j<LENGTH;j++)
{
fprintf(fp,"%6.2f ",w[i][j]);
}
// fprintf(fp,"\n");
}
fclose(fp);
}
void Init(double (*w)[LENGTH])
{
int i,j;
for(i=0;i<SET;i++)
{
for(j=0;j<LENGTH;j++)
{
w[i][j]=2.0;
}
}
}
void Print(double (*w)[LENGTH])
{
int i,j;
for(i=0;i<SET;i++)
{
for(j=0;j<LENGTH;j++)
{
printf("%8.2f",w[i][j]);
}
printf("\n");
}
printf("\n");
}