Why can't i get info from the text file correctly?Plz help me ASAP:(
I'm working on my assignment, and my deadline is tomorrow, i have tried again and again so many times but the info gotten from the text file are all wrong, i can't find any problem with my code, please help me :(
Code:
#include<stdio.h>
FILE *f;
typedef struct prd
{
char name[30];
char code[30];
char idt[30];
float price;
}prd;
void print(int i)
{int ct=1;
char line[500];
f=fopen("product.txt","r+");
prd product[i];
printf("pr: %s\n",product[ct].name);
{
fscanf(f,"%s %s %s %f",product[ct].name,product[ct].code,product[ct].idt,&product[ct].price);}
{
printf("%s\t",product[ct].name);
printf("%s\t", product[ct].code);
printf("%s\t", product[ct].idt);
printf("%0.2f\n", product[ct].price);
}
getch();
}
int main()
{
int i,j;
char sp;
f=fopen("product.txt","w");
printf("Input number of products: ");scanf("%d",&i);
struct prd product[i];
printf("Please input information of these products");
for (j = 0;j<i;j++)
{
printf("\nProduct #%d:\n",j+1);
printf("Name:");
scanf("%s",&product[j].name);
printf("Code:");
scanf("%s",&product[j].code);
printf("Id:");
scanf("%s",&product[j].idt);
printf("Price:");
scanf("%f", &product[j].price);
sp=getchar();
}
for (j = 0;j<i;j++)
{
fprintf(f,"%s\t",product[j].name);
fprintf(f,"%s\t", product[j].code);
fprintf(f,"%s\t", product[j].idt);
fprintf(f,"%0.2f\n", product[j].price);
}
print(i);
}