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, &current->amt, &current->held, &current->price);
		current->next=(struct stock *) malloc(sizeof(stock));
		current=current->next;
	}
	while((fscanf(fpData,"%s %s %f %d %f",current->code, current->name, &current->amt, &current->held, &current->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;

}

Tried compiling your program but....

prg1.c: In function ‘main’:
prg1.c:20: warning: ISO C90 forbids mixed declarations and code
prg1.c:23: error: ‘stock’ undeclared (first use in this function)
prg1.c:23: error: (Each undeclared identifier is reported only once
prg1.c:23: error: for each function it appears in.)

This

head=(struct stock*) malloc(sizeof(stock));

should be

head=(struct stock*) malloc(sizeof(struct stock));

and you have another error just like this on line 29

Your do-while loop reads two lines from the file in each iteration: one in the body, and one in the condition. The list gets only three nodes instead of five, and the for-loop then crashes as it tries to read alien memory the third node points to.

prg1.c: In function ‘main’:
prg1.c:20: warning: ISO C90 forbids mixed declarations and code
prg1.c:23: error: ‘stock’ undeclared (first use in this function)
prg1.c:23: error: (Each undeclared identifier is reported only once
prg1.c:23: error: for each function it appears in.)

ISO C90 wants declarations to be the first thing in a block. Rearrange this:

FILE* fpData;
fpData=fopen("1123.txt","r");
struct stock *head, *current;
int i;

to this:

FILE* fpData;
struct stock *head, *current;
int i;
fpData=fopen("1123.txt","r");

I'm not really good in link list. How am I suppose to increase the amount of node?

TQ for the struct part. Corrected it.

The flow of the program as it is now is like this:

create node A
fscanf data to node A
create node B
fscanf data to node B
fscanf data to node B (losing data that the previous fsanf read)
create node C
fscanf data to node C
fscanf data to node C (losing data that the previous fsanf read)
create node D
try to fscanf data but fail because we got to the end of the file
the loop is done

In other words, for each new node creation, you read from the file twice. Find a way to remove one fscanf from the loop. Would it be easier with while-loop?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.