Re: Structure Linked Lists: Searching for Words.
A lack of a free most likely won't cause an infinite loop, just a memory leak. Here's what I did to read in an input file named "in.txt" I went to the program arguments screen and typed in the name and clicked the check box next to "This target provides the project's main executable". Make sure the file is in the same directory as the project file as well.
Re: Structure Linked Lists: Searching for Words.
yup finally got it read in. Now I am learning how messed up things are :/ apparently my debbugger gets a segmentation fault at my strcmp line in my find function. Not sure whats up
Code:
struct artist *find(struct artist *sPtr, char *name)
{ //NEW
while (strcmp(name, sPtr->name )!=0)
{
sPtr = sPtr->nextArtist_p;
if (sPtr == NULL)
break;
}
return sPtr;
}
Re: Structure Linked Lists: Searching for Words.
Set a break point at the line and look at the values you are passing in to strcmp. Odds are one of your pointers is NULL and you are dereferencing it. Or it points to some memory you don't actually own.
Re: Structure Linked Lists: Searching for Words.
setting the break point didnt do anything it would just lock up anyway and say segmented fault. uhhggggggggggg
Re: Structure Linked Lists: Searching for Words.
Then set the breakpoint earlier....that's all I can really tell you. Debugging is an important skill to develop.
1 Attachment(s)
Re: Structure Linked Lists: Searching for Words.
my nodes arent saving when i read in my text file :(. Thats why my strcmp isnt working. I am not sure what to do to make them save, thought for sure I set it up corretly, and then created a print function to check, but that returns empty. halp! If you guys wanna try recreating it i attached the txt file.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LINEBUFFERSIZE 256
/* Definitions for data structure nodes (artist, disc, track) */
struct song
{
char *songName_p;
int trackNumber;
struct song *nextSong_p;
};
struct disc
{
char *discName_p;
int year;
struct song *song_p;
struct disc *nextDisc_p;
};
struct artist
{
char name[20];
char *artistName_p;
struct disc *disc_p;
struct artist *nextArtist_p;
};
struct artist *end = (struct artist *) NULL; //NEW
struct artist *startPtr = (struct artist *) NULL; //NEW
struct artist *find(struct artist *, char * );//NEW
//NEW
typedef struct artist artist_t;
typedef struct disc disc_t;
typedef struct song song_t;
typedef struct artist *artistNodePtr;
typedef struct disc *discNodePtr;
typedef struct song *songNodePtr;
/* function prototypes */
void InsertArtist(struct artist *New);
artistNodePtr initializenode(char *name);
artistNodePtr findOrInsertArtist(artistNodePtr *sPtr, char *name);
discNodePtr findOrInsertDisc(discNodePtr *sPtr, char *discID, int releaseYear);
void findOrInsertSong(songNodePtr *sPtr, char *songID, int trackID);
void getNextLine(char buffer[], int bufferSize, FILE *fptr);
void printlist( struct artist *ptr );
void printnode(struct artist *ptr);
int main(int argc, char *argv[])
{
char name[20];
struct artist *newNodePointer;
char lineBuffer[LINEBUFFERSIZE];
artistNodePtr startPtr = NULL; /* initially the artist list is empty */
FILE *musicFile;
char *artistTemp, *discTemp, *yearTemp, *trackTemp, *songTemp;
int year, track, menu = 1;
artistNodePtr theArtist;
// discNodePtr theDisc;
if (argc==1)
{
printf(" Must supply a file name as command line argument/n");
return 0;
}
if ((musicFile = fopen(argv[1], "r")) == NULL)
{
printf ("Error opening music file. Program terminated/n");
return 0;
}
getNextLine(lineBuffer, LINEBUFFERSIZE, musicFile);
while (!feof(musicFile))
{
artistTemp = strtok(lineBuffer,";");
if (artistTemp == NULL)
{
printf("Error parsing input file; Program is terminated\n");
return 0;
}
discTemp = strtok(NULL ,";");
if (discTemp == NULL)
{
printf("Error parsing input file; Program is terminated\n");
return 0;
}
yearTemp = strtok(NULL ,";");
if (yearTemp == NULL)
{
printf("Error parsing input file; Program is terminated\n");
return 0;
}
trackTemp = strtok(NULL ,";");
if (trackTemp == NULL)
{
printf("Error parsing input file; Program is terminated\n");
return 0;
}
songTemp = strtok(NULL ,"\n");
if (songTemp == NULL)
{
printf("Error parsing input file; Program is terminated\n");
return 0;
}
year = atoi(yearTemp);
track = atoi(trackTemp);
theArtist = findOrInsertArtist(&startPtr, artistTemp);
// theDisc = findOrInsertDisc(&(theArtist->disc_p), discTemp, year);
// findOrInsertSong(&(theDisc->song_p), songTemp, track);
getNextLine(lineBuffer, LINEBUFFERSIZE, musicFile);
} /* end of while loop */
while (menu != 0)
{
printf("1 TEST \n");
printf("2 to display alls songs by a given artist\n");
printf("3 to display all songs on a given disc\n");
printf("0 to exit the library\n ");
scanf("%d", &menu);
switch (menu){
case 1: printlist(startPtr);
break;
case 2:
printf("enter an artist name");
scanf("%s", name );
newNodePointer = find(startPtr, name );
if (newNodePointer==NULL)
{
newNodePointer = initializenode(name );
InsertArtist(newNodePointer);
}
}
} /* end main */
}
artistNodePtr findOrInsertArtist(artistNodePtr *sPtr, char *name )
{
sPtr = initializenode(name);
InsertArtist(sPtr);
if(!sPtr)
sPtr = find(startPtr, name);
return sPtr;
}
void printlist( struct artist *ptr ){
while(ptr!= NULL){
printnode(ptr);
ptr = ptr->nextArtist_p;
}
}
void printnode(struct artist *ptr)
{
printf("Name %s\n", ptr->name);
}
artistNodePtr initializenode(char *name)
{
struct artist *newNodePtr = (artistNodePtr)malloc(sizeof(artist_t));
if (newNodePtr != NULL)
{
newNodePtr->artistName_p = (char*)malloc((strlen(name)+1)*sizeof(char));
if (newNodePtr->artistName_p != NULL)
{
strcpy(newNodePtr->artistName_p, name);
newNodePtr->nextArtist_p = NULL;
newNodePtr->disc_p = NULL;
return newNodePtr;
}
}
}
void InsertArtist(struct artist *New)
{
//NEW
struct artist *temp, *prev;
if(startPtr == NULL)
{
startPtr = New;
end = New;
startPtr->nextArtist_p = NULL;
return;
}
temp = startPtr;
while(strcmp(temp->name, New->name) < 0)
{
temp = temp->nextArtist_p;
if(temp == NULL)
break;
}
if(temp == startPtr)
{
New->nextArtist_p = startPtr;
startPtr = New;
}
else
{
prev = startPtr;
while(prev->nextArtist_p != temp)
{
prev = prev->nextArtist_p;
}
prev->nextArtist_p = New;
New-> nextArtist_p = temp;
if(end == prev)
end = New;
}
}
struct artist *find(struct artist *newNodePointer, char *name)
{
//NEW
while (strcmp(name, newNodePointer->name )!=0)
{
newNodePointer = newNodePointer->nextArtist_p;
if (newNodePointer == NULL)
break;
}
return newNodePointer;
}
/*discNodePtr findOrInsertDisc( discNodePtr *sPtr, char *discID, int releaseYear)
{
}*/
/*void findOrInsertSong(songNodePtr *sPtr, char *songID, int trackID)
{
}*/
void getNextLine(char buffer[], int bufferSize, FILE *fptr)
{
char temp;
int i = 0;
buffer[0] = fgetc(fptr);
while ( (!feof(fptr)) && (buffer[i] != '\n') && i<(bufferSize-1))
{
i = i +1;
buffer[i]=fgetc(fptr);
}
if ((i == (bufferSize-1)) && (buffer[i] != '\n'))
{
temp = fgetc(fptr);
while (temp != '\n')
{
temp = fgetc(fptr);
}
}
buffer[i] = '\0';
}
Re: Structure Linked Lists: Searching for Words.
4 errors compiling for me.
This is why I made the comment about the typedefs before. You're getting artists and artist pointers mixed up because you're using the typedefs.