如何清除从文件中读取的垃圾值?
How to remove the garbage value read from the file?
#include <stdio.h>
#include <math.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
#define N 50
struct Visitor
{
char name[20];
int y;
char pass[20];
int age;
int oku;
float price;
};
main()
{
struct Visitor Data[N];
FILE *fdata;
int i = 1;
fdata = fopen("data.txt", "r"); // read mode
if (fdata == NULL)
{
perror("Error while opening the file.\n");
exit(EXIT_FAILURE);
}
else
{
while (EOF != fscanf(fdata, "%s\t,%d\t,%s\t,%d\t,%d\t,%.2f\n", Data[i].name, &Data[i].y, Data[i].pass, &Data[i].age, &Data[i].oku, &Data[i].price))
{
printf("%s\t,%d\t,%s\t,%d\t,%d\t,%.2f\n", Data[i].name, Data[i].y, Data[i].pass, Data[i].age, Data[i].oku, Data[i].price);
i++;
}
}
fclose(fdata);
return 0;
}
事实证明,我的文件的所有内容都显示在屏幕的左侧,然后每一行都有一些垃圾值。我上面的编码有问题吗?我应该如何消除垃圾值?请帮忙..
附件是我的示例输出。 (左边是我的文件内容)
data.txt
#include<stdio.h>
#include<math.h>
#include<conio.h>
#include<string.h>
#include <stdlib.h>
# define N 50
struct Visitor{
char name[20];
int y;
char pass[20];
int age;
int oku;
float price;
};
main(){
struct Visitor Data[N];
FILE *fdata;
int i=1;
fdata = fopen("data.txt","r"); // read mode
if( fdata == NULL )
{
perror("Error while opening the file.\n");
exit(EXIT_FAILURE);
}
else{
while (EOF!=fscanf(fdata,"%19s%d%19s%d%d%f", &Data[i].name, &Data[i].y, &Data[i].pass, &Data[i].age, &Data[i].oku, &Data[i].price )){
printf("%s\t,%d\t,%s\t,%d\t,%d\t,RM%.2f\n", Data[i].name, Data[i].y, Data[i].pass, Data[i].age, Data[i].oku, Data[i].price);
i++;
}
}
fclose(fdata);
return 0;
}
#include <stdio.h>
#include <math.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
#define N 50
struct Visitor
{
char name[20];
int y;
char pass[20];
int age;
int oku;
float price;
};
main()
{
struct Visitor Data[N];
FILE *fdata;
int i = 1;
fdata = fopen("data.txt", "r"); // read mode
if (fdata == NULL)
{
perror("Error while opening the file.\n");
exit(EXIT_FAILURE);
}
else
{
while (EOF != fscanf(fdata, "%s\t,%d\t,%s\t,%d\t,%d\t,%.2f\n", Data[i].name, &Data[i].y, Data[i].pass, &Data[i].age, &Data[i].oku, &Data[i].price))
{
printf("%s\t,%d\t,%s\t,%d\t,%d\t,%.2f\n", Data[i].name, Data[i].y, Data[i].pass, Data[i].age, Data[i].oku, Data[i].price);
i++;
}
}
fclose(fdata);
return 0;
}
事实证明,我的文件的所有内容都显示在屏幕的左侧,然后每一行都有一些垃圾值。我上面的编码有问题吗?我应该如何消除垃圾值?请帮忙..
附件是我的示例输出。 (左边是我的文件内容)
data.txt
#include<stdio.h>
#include<math.h>
#include<conio.h>
#include<string.h>
#include <stdlib.h>
# define N 50
struct Visitor{
char name[20];
int y;
char pass[20];
int age;
int oku;
float price;
};
main(){
struct Visitor Data[N];
FILE *fdata;
int i=1;
fdata = fopen("data.txt","r"); // read mode
if( fdata == NULL )
{
perror("Error while opening the file.\n");
exit(EXIT_FAILURE);
}
else{
while (EOF!=fscanf(fdata,"%19s%d%19s%d%d%f", &Data[i].name, &Data[i].y, &Data[i].pass, &Data[i].age, &Data[i].oku, &Data[i].price )){
printf("%s\t,%d\t,%s\t,%d\t,%d\t,RM%.2f\n", Data[i].name, Data[i].y, Data[i].pass, Data[i].age, Data[i].oku, Data[i].price);
i++;
}
}
fclose(fdata);
return 0;
}