string parsing troubles...
here is my source...I keep getting a segmentation fault on the second call to stringparse! Can anybody tell me why?
#include
#include
#include
#define maxname 25
#define maxline 100
#define maxstudent 10
//User defined types and structures
//ID is a type that defines a student
typedef struct student_id
{
char id[8];
char name[25];
int age;
int quiz[5];
float ave;
float height;
}ID;
//function declarations
void openfile(FILE **);
void stringparse(char *, char *);
void line_to_struct(ID*, char*, char*);
int main()
{
//local declarations
FILE *file_ptr;
char array[maxline];
char *array_ptr;
char *firstspace_ptr;
ID studentarray[maxstudent];
ID student1;
ID *student1_ptr;
//main code
openfile(&file_ptr);//opens the file for reading
fgets(array,sizeof(array),file_ptr);//places one line of the file in an array
//pointer initializations
array_ptr=&array[0];
firstspace_ptr=&array[0];
student1_ptr=&student1
line_to_struct(student1_ptr,array_ptr,firstspace_ptr);
printf("%s\n",student1.id);
printf("%s\n",student1.name);
return(0);
}
/*---------------------------------------------------------------------------
Takes in a FILE pointer and asks for a filename. It then opens it for reading.
If the file does not exist it returns an Error and exits the program.
----------------------------------------------------------------------------*/
void openfile(FILE **infile)
{
//local declarations
char filename[maxname];
printf("Please enter the name of the file you would like to open: ");
scanf("%s",filename);
*infile=fopen(filename, "r");
if(*infile==NULL)
{
printf("The file you entered does not exist\n");
exit(8);
}
}
/*----------------------------------------------------------------------------
Takes an array and looks for spaces. It advances firstspace to the first
character that is not a space and leaves the array_ptr at the same place.
---------------------------------------------------------------------------*/
void stringparse(char *array_ptr, char *firstspace_ptr)
{
firstspace_ptr=strchr(array_ptr,'' '');
*firstspace_ptr=''\0'';
firstspace_ptr++;
while (*firstspace_ptr=='' '' && *firstspace_ptr!=''\0'')
{
firstspace_ptr++;
};
}
/*---------------------------------------------------------------------------
line_to_struct fills a struct called ID
----------------------------------------------------------------------------*/
void line_to_struct(ID *student_ptr, char *array_ptr, char *firstspace_ptr)
{
stringparse(array_ptr,firstspace_ptr);
strcpy((*student_ptr).id, array_ptr);
array_ptr=firstspace_ptr;
stringparse(array_ptr,firstspace_ptr); //-this call bombs the prog
//strcpy((*student_ptr).name,array_ptr);
//strcpy(array_ptr,firstspace_ptr);
// stringparse(array_ptr,firstspace_ptr);
// student.quiz.quiz1=array_ptr;
// array_ptr=firstspace_ptr;
// stringparse(array_ptr,firstspace_ptr);
// student.quiz.quiz2=array_ptr;
// array_ptr=firstspace_ptr;
// stringparse(array_ptr,firstspace_ptr);
// student.quiz.quiz3=array_ptr;
// array_ptr=firstspace_ptr;
// stringparse(array_ptr,firstspace_ptr);
// student.quiz.quiz4=array_ptr;
// array_ptr=firstspace_ptr;
// stringparse(array_ptr,firstspace_ptr);
// student.quiz.quiz5=array_ptr;
// array_ptr=firstspace_ptr;
// stringparse(array_ptr,firstspace_ptr);
// student.height=array_ptr;*/
}
ICQ 11133295
AIM shatterstar98
MSN shatter98@hotmail.com
!
You''re not advancing through the array_ptr string, your just adding one to a single byte value!
//You need to use a char** in order to return a char* as// a parameter for firstspace_ptr.void stringparse(char *array_ptr, char *firstspace_ptr){firstspace_ptr=strchr(array_ptr,'' '');//This points firstspace to NULL! Bad programmer, bad! set it to array_ptr*firstspace_ptr=''\0'';//This does not advance through the string, it just adds!firstspace_ptr++;//BOOM! You''re reading RAM at address 0x00000001while (*firstspace_ptr=='' '' && *firstspace_ptr!=''\0''){firstspace_ptr++;};}
You''re not advancing through the array_ptr string, your just adding one to a single byte value!
I don't agree with all Magmai Kai Holmlor said but he is right about that you have to use char** firstspace_ptr. Also, strchr returns NULL if it doesn't find what it's looking for, so you have to check for that. Here is how I would write it( I haven't been able to test it since I don't have a compiler on this machine). Btw, about your while loop: if firstspace_ptr == ' ' it is always != '\0' so you don't need that test.
I don't know why you want to do **firstspace_ptr = '\0' but I
suppose you have your reasons. However it doesn't set the pointer to point to NULL as Magmai claimed.
Edited by - lowlevel on October 19, 2001 4:46:22 AM
void stringparse(char *array_ptr, char **firstspace_ptr){ *firstspace_ptr = strchr(array_ptr,' '); if( *firstspace_ptr != NULL ) { **firstspace_ptr = '\0'; *firstspace_ptr++; while( **firstspace_ptr == ' ' ) { *firstspace_ptr++; } } else { *firstspace_ptr = array_ptr; }}
I don't know why you want to do **firstspace_ptr = '\0' but I
suppose you have your reasons. However it doesn't set the pointer to point to NULL as Magmai claimed.
Edited by - lowlevel on October 19, 2001 4:46:22 AM
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement