Hi everybody,
I'm trying to make the following code running properly, but I can't get
rid of the "SEGMENTATION FAULT" error message when executing.
Reading some messages posted earlier, I understood that a segmentation
fault can occur whenever I declare a pointer and I leave it
un-initialized.
So I thought the problem here is with the (const char *)s in the stuct
flightData (please note that I get the same fault declaring as char *
the members of struct flightData), but I can't figure out why.
Please also note that I've declared struct flightData members as char *
instead of char vectors, because I can't assume a value for the maximum
lenght of town names in flights.dat.
Could someone kindly explain the reason for I get the segmentation
fault, and suggest a way to make the program run properly?
Many thanks
Fra
The source code was compiled with GCC 3.3.5 (both with and without
-ansi option: the result is always the same) under Linux Knoppix 3.7
Live (kernel 2.6.9)
*** Here is the content of flights.dat:
Madrid Roma AA
Paris London UA
Milano Roma AZ
Milano New_York AZ
Berlin Milano LA
Roma Paris AZ
*** Here is the source code of fly3.c:
#include <stdio.h>
#include <string.h>
#define DEBUG
struct flightData {
const char * takeoffTown;
const char * landingTown;
const char * code;
};
/* ...omitted function prototypes... */
int main( int argc, const char *argv[] )
{
struct flightData flight;
FILE *flightsFPtr, *takeoffsFPtr, *landingsFPtr, *companiesFPtr;
if ( argc != 3 ) {
printf( "Usage: fly3 flights.dat Town - Aborting program fly3\n"
);
return 0;
}
if ( (flightsFPtr = fopen( argv[1], "r" )) == NULL ) {
printf( "Cannot open file %s - Aborting program fly3\n" );
return 0;
}
#ifdef DEBUG
printf( "Opening output files...\n" );
#endif
takeoffsFPtr = fopen( "takeoffs.dat", "w" );
landingsFPtr = fopen( "landings.dat", "w" );
companiesFPtr = fopen( "companies.dat", "w" );
#ifdef DEBUG
printf( "Scanning file %s...\n", argv[1] );
#endif
while ( !feof(flightsFPtr) ) {
#ifdef DEBUG
printf( "Reading %s...\n", argv[1] );
#endif
fscanf( flightsFPtr, "%s%s%s", &flight.takeoffTown,
&flight.takeoffTown, &flight.code );
#ifdef DEBUG
/* NEXT THREE LINES ARE FOR DEBUGGING PURPOSES */
printf( "I've read file %s...\n", argv[1] );
printf( "Town entered by user: %s\n", argv[2] );
printf( "I've read takeoff from: %s\n", flight.takeoffTown );
/* HERE I GET SEGMENTATION FAULT IF DEBUGGING*/
#endif
/* testing if the town name entered by user matches the name
stored in flight.takeoffTown */
if ( strcmp(flight.takeoffTown, argv[2]) == 0 ) { /* HERE I GET
SEGMENTATION FAULT IF NOT DEBUGGING */
#ifdef DEBUG /* COULDN'T EVER EXECUTE FROM THIS LINE ON... =(
*/
printf( "Writing on takeoffs file...\n" );
#endif
writeData( takeoffsFPtr, flight );
if ( stringPresence( companiesFPtr, flight.code ) == 0 ) {
#ifdef DEBUG
printf( "Writing on companies file...\n" );
#endif
fprintf( companiesFPtr, "%s\n", flight.code );
}
/* *** ...omitted the remaining code... *** */
*** and finally the output running the executable:
$ fly3 flights.dat Milano
Opening output files...
Scanning file flights.dat...
Reading flights.dat...
I've read file flights.dat...
Town entered by user: Milano
Segmentation fault
$