LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-17-2009, 12:13 PM   #1
PMR
LQ Newbie
 
Registered: May 2009
Posts: 5

Rep: Reputation: 0
Error while populating data from text files to a 2d linked lists


Hi everybody.I am new to this community hence not very much aware of how things work around here.

But i have seen that this site seems to be very helpful so planned on joining it.

Ok here is how my problem goes..
I have to develop a code that can take data from .txt files and then save them in a 2d linked list in a sorted manner. I have tried the coding but since am new to the C language I am finding it a little hard to understand the errors completely. I would be really glad if someone could show my mistakes in the following code.

The file formats are as follows -
[FareCategoryID]|[FareType]|[FareName]|[CategoryDescription]

[ItemID]|[FareCategoryID]|[ItemName]|[Price]|[ItemDescription]
 
Old 05-17-2009, 12:15 PM   #2
PMR
LQ Newbie
 
Registered: May 2009
Posts: 5

Original Poster
Rep: Reputation: 0
Code:

int loadData(FMSType* fms, char* fareFile, char* subfareFile)
{
CategoryTypePtr temp=NULL, startCategory=NULL, category=NULL, cur=NULL, prev=NULL;
ItemTypePtr tempItem=NULL, startItem=NULL, item=NULL, curItem=NULL, prevItem=NULL;


char line[LINE_LENGTH];
char subline[LINE_LENGTH];
char *catID;
char *catName;
char *catType;
char *catDesc;
char *itemID;
char *itemName;
char *itemDesc;
char *fareID;
char *token;
unsigned dollar,cent;

FILE *fp1,*fp2;
startCategory=(CategoryTypePtr )malloc(sizeof(CategoryType)); /* Dynamically creating category structure variable */
startCategory = NULL;


fp1=fopen(fareFile,"r"); /* opening fare.dat file in read mode */
fp2=fopen(subfareFile,"r");

if(fp1 == NULL || fp2 == NULL)
{
if(fp1 == NULL)
{
printf("%s does not exist!!\nPlease provide the correct file. \n",fareFile);

}
else if(fp2 == NULL)
{
printf("%s does not exist!!\nPlease provide the correct file.\n",subfareFile);
}
else if(fp1 == NULL && fp2 == NULL)
{
printf("%s and %s does not exist!!\n Please provide bothe files.\n",fareFile,subfareFile);
}
return SUCCESS;
}
else
{
while((fgets(line,LINE_LENGTH,fp1)) != NULL)
{
category=(CategoryTypePtr )malloc(sizeof(CategoryType));

catID=strtok(line,DELIM);
strcpy(category->categoryID,catID);

catType=strtok(NULL,DELIM);
strcpy(category->fareType,catType);

catName=strtok(NULL,DELIM);
strcpy(category->categoryName,catName);

catDesc=strtok(NULL,DELIM);
strcpy(category->categoryDescription,catDesc);

category->nextCategory=NULL;
category->headItem=NULL;

/* To create the linked list for the fare.dat file */

if(startCategory == NULL) /* To check whether the list already exists or not */
{
startCategory = category;
}
else /* If its not the first node created then do this else part */
{
temp = (CategoryTypePtr )malloc(sizeof(CategoryType));
temp = startCategory;

while(temp->nextCategory != NULL)
{
temp = temp->nextCategory;
}
temp->nextCategory = category;
}
} /* End of while(EOF) */
} /* End of else */

temp = startCategory;
if(temp == NULL)
{
printf("The list is empty\n");
}
else
{
printf("The list contents are:\n");
while(temp != NULL)
{
printf("Fare CategoryID: %s\nFare Type: %c\nFare Name: %s\nCategory Description: %s\n", temp->categoryID,temp->fareType,temp->categoryName,temp->categoryDescription);
temp= temp->nextCategory;
}
}

/* List for subfare.dat file */

while((fgets(subline,LINE_LENGTH, fp2)) != NULL)
{
item = (ItemTypePtr )malloc(sizeof(ItemType));

itemID=strtok(subline,DELIM);
strcpy(item->itemID,itemID);

fareID=strtok(NULL,DELIM);

itemName=strtok(NULL,DELIM);
strcpy(item->itemName,itemName);

token = strtok(NULL,".");
dollar = atoi(token);

token = strtok(NULL,"|");
cent = atoi(token);
item->prices[0].dollars = dollar;
item->prices[0].cents = cent;

itemDesc=strtok(NULL,DELIM);
strcpy(item->itemDescription,itemDesc);

item->nextItem=NULL;

temp=startCategory;

while(temp->nextCategory != NULL)
{
if(strcmp(fareID,temp->categoryID) == 0) /* Checking whether the fareIDs are same*/
{
if(temp->headItem == NULL)
{
temp->headItem=item;
}
else
{
tempItem=temp->headItem;
while(tempItem->nextItem != NULL)
{
tempItem=tempItem->nextItem;
}
tempItem->nextItem=item;
}
}
temp=temp->nextCategory;
}
printf("This is out of list loop\n");
} /* End of while(EOF) */
} /* End of else */


/*printf("The contents of the complete list are:\n");*/
/*temp=startCategory;*/
if(temp == NULL)
{
printf("The list is empty\n");
}
else
{
printf("The list contents are:\n");
while(temp != NULL)
{
printf("\nFare CategoryID: %s\nFare Type: %c\nFare Name: %s\nCategory Description: %s", temp->categoryID,temp->fareType,temp->categoryName,temp->categoryDescription);
tempItem=temp->headItem;
if(tempItem == NULL)
{
printf("The subfare list is empty for this fareID\n");
}
else
{
while(tempItem != NULL)
{
printf("ItemId: %s\nItem Name: %s\nDollar:%d\n Cents:%d\nItem description:%s",tempItem->itemID,tempItem->itemName,tempItem->prices[0].dollars,prices[0].cents,tempItem->itemDescription);
}
temp= temp->nextCategory;
}
/*temp=temp->nextCategory;*/
}
}
if(fclose(fp1)!=0 || fclose(fp2)!=0)
{
printf("Error in closing files\n");
}
return EXIT_SUCCESS;

}
 
Old 05-17-2009, 12:19 PM   #3
PMR
LQ Newbie
 
Registered: May 2009
Posts: 5

Original Poster
Rep: Reputation: 0
Errors

These are the errors that i get. I get the "bus error" so i am sure am referencing the memory wrongly.

fms_utility.c:202: error: syntax error before ‘if’
fms_utility.c:212: warning: type defaults to ‘int’ in declaration of ‘tempItem’
fms_utility.c:212: error: ‘temp’ undeclared here (not in a function)
fms_utility.c:212: warning: data definition has no type or storage class
fms_utility.c:213: error: syntax error before ‘if’
fms_utility.c:223: warning: type defaults to ‘int’ in declaration of ‘temp’
fms_utility.c:223: error: invalid type argument of ‘->’
fms_utility.c:223: warning: data definition has no type or storage class
fms_utility.c:224: error: syntax error before ‘}’ token
 
Old 05-17-2009, 07:42 PM   #4
norobro
Member
 
Registered: Feb 2006
Distribution: Debian Sid
Posts: 694

Rep: Reputation: 273Reputation: 273Reputation: 273
In the future please use the code tags when posting this much code. Makes it much easier to read.

You need to take a look at your syntax. The closing brace for your loadData function is this one:
Code:
} /* End of else */

/*printf("The contents of the complete list are:\n");*/
/*temp=startCategory;*/
if(temp == NULL)
I copied your code into an editor and only got 180 lines, but I suspect your line 202 is the above if statement.

Last edited by norobro; 05-17-2009 at 08:42 PM. Reason: changed "line 203" to "line 202"
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
linked lists Natasl Programming 5 08-04-2006 01:02 PM
Linked Lists leonidg Programming 7 03-10-2005 03:07 AM
queue and linked lists Palamides Programming 2 03-09-2005 09:08 PM
Linked Lists - What and Why? scuzzman Programming 9 12-31-2004 11:51 AM
c++ linked lists jclark00001 Programming 10 02-23-2003 03:40 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:28 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration