The program takes in a command for e.g. "a australia" which adds an ASCII image file with the australian flag into the node. If I type i, it gives an index of all the files added, and if I press p, it prints the flag onto the screen.
Now these 3 commands work fine to an extent. But if I press "i" to list the files and THEN press "p" it gives me a segfault, however if I don't list the files, i can add and print as many times without any problems. I'm guessing the memory allocation isn't right? But it could be the code.
Code:
typedef struct country Country;
struct country {
char name[LENGTH];
int dimen;
int num;
QTnode *flag; // another structure defined in a nother file
Country *next;
};
char cpy( char s[MAXLENGTH], char strEd[MAXLENGTH] );
Country *makeNode(char p[MAXLENGTH]);
// Main function
int main( void )
{
char command[MAXLENGTH];
char c;
Country *node;
Country *pnode; //previous node
Country *curr; //the current node i.e. the most recent file added
Country *tail = NULL;
int dimen;
int i = 1; //This is the image number
int j; //This is a control number for the while loop
printPrompt();
while( fgets( command, MAXLENGTH, stdin ) != NULL ) {
int imgNum;
char *p;
if(( p=strrchr( command, '\n')) != NULL ) {
*p = '\0'; // remove '\n' at end of line
}
// find the first non-space character in the command
p = command;
while(isspace(*p)) {
p++;
}
c = *p;
if( isdigit(c)) {
if( sscanf( command, "%d", &imgNum ) == 1 ) {
// INSERT CODE FOR <k> COMMAND
}
}
else switch( c ) {
case 'h': // help
printf(" A - Add image\n" );
printf(" I - Index\n" );
printf(" P - Print image\n" );
printf(" F - Forward\n" );
printf(" B - Back\n" );
printf("<k>- make image number k the current image\n");
printf(" D - Delete image\n" );
printf(" L - Look for image\n" );
printf(" R - Rotate image counterclockwise\n" );
printf(" M - Mirror image (reflect vertically)\n" );
printf("NE - zoom into North East corner\n" );
printf("NW - zoom into North West corner\n" );
printf("SW - zoom into South West corner\n" );
printf("SE - zoom into South East corner\n" );
printf(" O - zoom Out\n" );
printf(" U - Undo\n" );
printf(" H - Help\n" );
printf(" Q - Quit\n" );
break;
// INSERT CODE FOR OTHER COMMANDS
case 'a':
j = 1;
while( j > 0 ){ //This loop is actually to allow the image number to increase everytime this case a executes.
p++;
while(isspace(*p)) {
p++;
}
node = makeNode(p); //Create a new node
curr = node;
strcpy(node->name, p); // Copy from the user input to the member 'name' of the struct called node
node->flag = getImage(node->name, &node->dimen);
node->num = i;
if((node->dimen > 0) && (node->dimen < 128)) {
printf("*%d [%2d] %s\n", node->num, node->dimen, node->name); //Print details after adding each image
}
if( tail == NULL ) {
tail = node;
pnode = node;
}
else {
pnode->next = node;
pnode = pnode->next;
}
pnode->next = NULL;
j = 0;
i++;
}
break;
case 'i':
node = tail;
while (node != NULL){
if(node == curr){
printf("*%d [%2d] %s\n", node->num, node->dimen, node->name);
}
else{
printf(" %d [%2d] %s\n", node->num, node->dimen, node->name);
}
node = node->next;
}
break;
case 'p': //print the image
dimen = node->dimen;
printImage(node->flag, dimen); //accessing the inner node
break;
case 'q': // quit program
printf("Bye!\n");
return 0;
break;
default:
printf("Unrecognized command: %s\n", command );
break;
}
printPrompt();
}
return 0;
}
Country *makeNode(char p[MAXLENGTH]){
Country *node;
node = (Country*)malloc(sizeof(Country));
node->next = NULL;
return (node);
}