hey, im having a problem getting file stored into linkedlists. the program crushes when start reading.

these are the structs:

typedef struct category
{
   char categoryID[5];
   char categoryName[25];
   char fareType;   
   char categoryDescription[250];
   unsigned numItems;

   ItemTypePtr headItem; //ptr to item struct
   CategoryTypePtr nextCategory;
} CategoryType;

typedef struct item
{
   char itemID[5];
   char itemName[25];
   char itemDescription[250];
   ItemTypePtr nextItem;
} ItemType;

typedef struct fms
{
   CategoryTypePtr headCategory;
   unsigned numCategories;
} FMSType;

the format of the file is like:
F0001|F|Daily|Allow unlimited train, tram and bus travel for a whole day within selected zones.
F0002|F|3 hour|Allow unlimited train, tram and bus travel for at least two hours within selected zones.
F0003|F|10 hour|Allow ten 2 hour trips in a single ticket at a discounted price, but can only be used by one person at a time.

each field splited by |.

code:

int loadData( FMSType* fms, char* fareFile, char* subfareFile)
{
	int data = 0;
	
	FILE *fp1, *fp2;
	
	if((fp1 = fopen(fareFile, "r"))==NULL && ((fp2 = fopen(subfareFile, "r"))==NULL)) {
		printf("Cannot open file.\n");
		return data = 1;
		exit(1);
  }

  fscanf(fp1, "%s%s%s%s", fms->headCategory->categoryID, &fms->headCategory->fareType,&fms->headCategory->categoryName, &fms->headCategory->categoryDescription);

  /*fscanf(fp2, "%s%s%s%s",fms->headCategory->headItem->itemID, fms->headCategory->categoryID, fms->headCategory->headItem->itemName, fms->headCategory->headItem->itemDescription);

  fclose(fp1);
  fclose(fp2);

return data = 1;

}

how can i get these stored into linkedlists. any help is much appreciated.

When you use "%s" specifier for fscanf it reads until a white space character is found. White space characters would be a space, newline character, or a tab.

You can check out details on fscanf here.
http://www.cplusplus.com/reference/clibrary/cstdio/fscanf/

I also took a quick look at your structs.

Since you're a linking items together you'll probably need to use pointers, so it might be a good idea to take a look at

ItemTypePtr headItem; //ptr to item struct
CategoryTypePtr nextCategory;

to make sure that you've properly declared those as pointers to the types you've specified.

Since the fields are separated with | I think it would be easier use fgets() to read the entire line then parse the string using strtok().

char line[255];
while( fgets( fp1, line, sizeof(line) ) != NULL)
{
     char* ptr = strtok(line, "|");
     strcpy(fms->headCategory->categoryID, line);
     ptr = strtok(NULL, "|");
     strcpy(fms->headCategory->fareType,, line);
    // repeat above two lines for each of the other fields

}

thx Ancient Dragon that really helped me, now im working on the rest of the stuff, only hoping not to get stuck again. but here:
take a look at this piece of code

char *sep="|";
char* ptr = strtok(buff, sep);
strcpy(fms->headCategory->categoryID, ptr);
ptr = strtok(NULL, sep);
fms->headCategory->fareType = *ptr;
ptr = strtok(NULL, sep);

im using pointer instead of string passing value, would that be better or basicly the same thing. ans also how exactly to make it a
linkedlist. thanks a lot.

A linked list is basically a chain of pointers that, when dereferenced, allow you to access data. For example:

typedef struct t_dataContainer {
	int ANumber;
	DataContainer * PtrNextContainer;
} DataContainer;

So that would be your data container. Next what you would want to do is make a method to store data in it and attach the pointers where appropriate.

DataContainer * GL_PtrDataList = NULL;

void AddData( int NumberToStore ) {
	
	DataContainer * PtrLastData;
	DataContainer * PtrNewData;
	
	/* allocate memory for new data */
	PtrNewData = (DataContainer *)malloc( sizeof(DataContainer) );
	
	/* initilize new data */
	memset( PtrNewData, 0x00, sizeof(DataContainer) );
	
	/* set your data */
	PtrNewData->Anumber = NumberToStore;
	
	/* check to see if there is data in the list */
	if ( GL_PtrDataList = NULL ) {
		/* add new data to the list */
		GL_PtrDataList = PtrNewData;
	}
	else {
		/* start at the head of the list */
		PtrLastData = GL_PtrDataList;
		/* loop to find the end of the list */
		while ( 1 ) {
			/* if the container we're pointing to's next value is NULL */
			/* we're at the end of the list                            */
			if ( PtrLastData->PtrNextContainer == NULL ) {
				/* break loop to add new data */
				break;
			}
			/* other wise proceed to the next container to check again */
			PtrLastData = PtrLastData->PtrNextContainer;
		}
		
		/* add new data to the end of the list */
		PtrLastData->PtrNextContainer = PtrNewData;
	}

}

So you might want to check your structs to make sure that they have pointer variables so that you can point to the data that you want.

You might also want to search for the different kinds of linked lists. A bi-directional would allow you to go forward and backwards through a list.

Here's a link with a little bit of information to help you along your way.

http://www.codeproject.com/KB/cpp/linked_list.aspx

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.