dole_4
09-27-2002, 07:07 AM
hi,
I have some code that compiles 100% error/warning free, but whenever i execute the program its spits back segment faults.
The aim of the code is to search a 2d array (size rowXcol) for a specific character (in the first case 'W') and print to a file a string ("wumpus") plus the x/y co ordinates of where it occurs. Each time it occurs it should print the same string and different x/y co ords on a newline (FILE * passed down 1 line).
Then it should return the FILE * to the calling function where it can search for a new char , then print another string/x/y into the file and so on...
here is the code:
somewhere in int main() {
...
FILE* outFilePtr;
outFilePtr = insert_data( outFilePtr, 'W' , "wumpus", map, data );
outFilePtr = insert_data(outFilePtr, 'G' , "gold", map, data);
outFilePtr = insert_data(outFilePtr, 'P' , "pit", map, data);
outFilePtr = insert_data(outFilePtr, 'E' , "exit", map, data);
... }
FILE * insert_data( FILE * filePtr, char toFind, char word[], char ** map, int * data)
{
int c=0, d=0;
char line[25];
int row = data[5];
int col = data[6];
while( c < col ) {
while ( d < row ) {
if( map[c][d] == toFind ) {
sprintf( line,"%s %d %d\n", word , d, c);
fputs(line, filePtr);
}
d++;
}
d=0;
c++;
}
return filePtr;
}
Now i know the segment error occurs the first time it calls insert_data, but i can't find where. Possible its some error where a < is put in place of a > or something similar, but whatever it is, any help would be really appreciated.
Thanks.
I have some code that compiles 100% error/warning free, but whenever i execute the program its spits back segment faults.
The aim of the code is to search a 2d array (size rowXcol) for a specific character (in the first case 'W') and print to a file a string ("wumpus") plus the x/y co ordinates of where it occurs. Each time it occurs it should print the same string and different x/y co ords on a newline (FILE * passed down 1 line).
Then it should return the FILE * to the calling function where it can search for a new char , then print another string/x/y into the file and so on...
here is the code:
somewhere in int main() {
...
FILE* outFilePtr;
outFilePtr = insert_data( outFilePtr, 'W' , "wumpus", map, data );
outFilePtr = insert_data(outFilePtr, 'G' , "gold", map, data);
outFilePtr = insert_data(outFilePtr, 'P' , "pit", map, data);
outFilePtr = insert_data(outFilePtr, 'E' , "exit", map, data);
... }
FILE * insert_data( FILE * filePtr, char toFind, char word[], char ** map, int * data)
{
int c=0, d=0;
char line[25];
int row = data[5];
int col = data[6];
while( c < col ) {
while ( d < row ) {
if( map[c][d] == toFind ) {
sprintf( line,"%s %d %d\n", word , d, c);
fputs(line, filePtr);
}
d++;
}
d=0;
c++;
}
return filePtr;
}
Now i know the segment error occurs the first time it calls insert_data, but i can't find where. Possible its some error where a < is put in place of a > or something similar, but whatever it is, any help would be really appreciated.
Thanks.