#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;
}
Turns out, all the contents of my file were shown on the left part of the screen, followed by some garbage values at each row. Is there any problem with my coding above? How should I eliminate the garbage values? Please help..
Attached is my sample output. (The one on the left part is my file content)
#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;
}