I tried to change for loop to do while loop so that I no need to control the value of i in the future. But seems my program crashes.
#include<stdio.h>
#include<stdlib.h>
struct stock
{
char code[10];
char name[10];
float amt;
int held;
float price;
struct stock *next;
};
int main(void)
{
FILE* fpData;
fpData=fopen("1123.txt","r");
struct stock *head, *current;
int i;
head=(struct stock*) malloc(sizeof(stock));
current=head;
do
{
fscanf(fpData,"%s %s %f %d %f",current->code, current->name, ¤t->amt, ¤t->held, ¤t->price);
current->next=(struct stock *) malloc(sizeof(stock));
current=current->next;
}
while((fscanf(fpData,"%s %s %f %d %f",current->code, current->name, ¤t->amt, ¤t->held, ¤t->price))==5);
current=head;
for(i=0;i<5;i++)
{
printf("%s\n%s\n%.2f\n%d\n%.2f\n",current->code, current->name, current->amt, current->held, current->price);
current=current->next;
}
return 0;
}