For Programmers: Free Programming Magazines  


Home > Archive > Unix Programming > September 2005 > cant figure out where the [segmentation] fault lies ??









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author cant figure out where the [segmentation] fault lies ??
ebrahimbandookwala@gmail.com

2005-09-24, 3:56 am

Hi Im making this fairly simple c prog which reads and writes structs
to a file in binary . I get this stupid error when I try doing the
following ( in deleteentry () )

if(remove (filename))
{
if(rename (tempfile , filename))
{
if(!remove (tempfile))
printf("\nFailed to remove %s" , tempfile);
}
else
printf("\nFailed to rename %s" , tempfile);
}
else
printf("\nFailed to remove %s" , filename );

which fails at either the first level or the second. Cant figure out y
?? For some reason it refuses to delete the file (filename) . I also
tried plain and simple rename(tempfile -> filename) but that too
refuses to work ?? Maybe im overlooking something really stupid !! ..











#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/stat.h>
#include <string.h>
#include <errno.h>

#define FLIGHT_NAME_MAX 60
#define TAIL_NUMBER_DIGITS 6
#define REC_SIZE 84

typedef struct flight_s {
char flight[FLIGHT_NAME_MAX];
char tail_number[TAIL_NUMBER_DIGITS];
float price;
int seats;
int first , business;
}flight_t;

char * filename = "records.dat"; // The record file.
typedef enum field_e { flight , tail_number , price , seats , first ,
business }; // should go in the header.


int add_flight(flight_t * );
int add_flights (int num , flight_t * f , char * file );
void del_flight(int , void *);

int sizeoffile();
void getStructure( int s_no , flight_t * );

int findbyflight( void * data);
int findbytail( void * data);
int findbyprice( void * data);
int findbyseats( void * data);
int findbyfirst( void * data);
int findbybusiness( void * data);

void deleteentry ( int entry_no );


int main(void)
{
flight_t t;
int choice;
int i,j;

while(choice!=4)
{
printf("\n\n\t\tFlight Inventory...\n");
printf("\n\n\tChoose from Menu\n1.Add Flight\n2.Del Flight\n3.View
Flights\n4.Exit\n\n");
scanf("%d",&choice);

switch(choice)
{
case 1:
printf("\nEnter flight :");
scanf("%s",t.flight);
printf("\nEnter flight number :");
scanf("%s",t.tail_number);
t.price = 200;
t.seats=260;
t.first=20;
t.business=50;
add_flight(&t);
break;

case 2:
printf("\nEntry to delete : ");
scanf("%d",&i);
deleteentry(i);
break;

case 3:
j = sizeoffile ();
for(i =0 ; i < (j/REC_SIZE) ; i++ )
{
getStructure(i, &t);
printf("\n%d. Flt %s\t Num %s\t\n",i+1,t.flight,t.tail_number);
}
break;
}
}
//return add_flight (&t);
//return del_flight( flight , &t.seats );
return 0;
}

int add_flight ( flight_t * f )
{
int fd = 0 , exit_flag = 0, written; // 0 exit flag is normal , 1
means err.

if((fd = open (filename , O_WRONLY ))<0)
{
if ( ( fd = creat ( filename , 0777 ) ) < 0 ){
printf("\nFile could not be created!!\n" ); // file was not created
..
return 1;
}
} // file was created.


ls(fd , 0 , SEEK_END); // s the end of the file. =>Append mode

written = write ( fd , f , sizeof(*f) );
if(written < sizeof(*f) )
{
printf("The complete record was not written on file.");
exit_flag = 1;
}
close(fd);
return exit_flag;
}

int add_flights (int num , flight_t * f , char * file )
{
int i;
int fd = 0 , exit_flag = 0, written; // 0 exit flag is normal , 1
means err.

if((fd = open (file , O_WRONLY))<0)
{
if ( ( fd = creat ( file , 0777 ) ) < 0 ){
printf("\nFile could not be created!!\n" ); // file was not created
..
return 1;
}
} // file was openned


ls(fd , 0 , SEEK_END); // s the end of the file. =>Append mode

for(i=0;i<num;i++)
{
written = write ( fd , f , sizeof(*f) );
if(written < sizeof(*f) )
{
printf("Some record was not written on file.");
exit_flag = 1;
}
f++;
}

close(fd);
perror(errno);
return exit_flag;
}


void del_flight(int field , void * data)
{
int n;
if(field == flight)
{
if((n = findbyflight(data)) >= 0 )
{
deleteentry(n); // deletes the nth entry in the file
}
else
printf("The entry for flight %s was not found " , (char *) data );
}
else if (field == tail_number )
{
if((n = findbytail(data)) >= 0 )
{
deleteentry(n); // deletes the nth entry in the file
}
else
printf("The entry for tail %s was not found " , (char *) data );
}
else if (field == price )
{
if((n = findbyprice(data)) >= 0 )
{
deleteentry(n); // deletes the nth entry in the file
}
else
printf("The entry for price %d was not found " , *((double *) data
));
}
else if (field == seats )
{
if((n = findbyseats(data)) >= 0 )
{
deleteentry(n); // deletes the nth entry in the file
}
else
printf("The entry for seats %d was not found " , *((int *) data ));
}
else if (field == first )
{
if((n = findbyfirst(data)) >= 0 )
{
deleteentry(n); // deletes the nth entry in the file
}
else
printf("The entry for first %d was not found " , *((int *) data ));
}
else
{
if((n = findbybusiness(data)) >= 0 )
{
deleteentry(n); // deletes the nth entry in the file
}
else
printf("The entry for business %d was not found " , *((int *) data
));
}
}

int sizeoffile()
{
struct stat sts ;
stat(filename , &sts); // read the stat structure for the records file
and get the size of the file.
return sts.st_size;
}

void getStructure( int s_no , flight_t * t)
{
int fd;
if(sizeoffile() > 0 && ( (fd = open(filename , O_RDONLY)) > 0 ))
{

//int fd = open ( filename , O_RDONLY ) ;
ls ( fd , REC_SIZE * s_no , SEEK_CUR );
read ( fd , t , REC_SIZE );
close(fd);
}
}

int findbyflight(void * data)
{
int i , cursize;
flight_t t;
cursize = sizeoffile ();
for(i =0 ; i < (cursize/REC_SIZE) ; i++ )
{
getStructure(i, &t);
if(strcmp(t.flight ,(char *) data ) )
{
return i;
}
}
return -1;
}

int findbytail( void * data)
{
int i , cursize;
flight_t t;
cursize = sizeoffile ();
for(i =0 ; i < (cursize/REC_SIZE) ; i++ )
{
getStructure(i, &t);
if(strcmp(t.tail_number , data ) )
{
return i;
}
}
return -1;
}

int findbyprice( void * data)
{
int i , cursize;
flight_t t;
float * price;
cursize = sizeoffile ();
for(i =0 ; i < (cursize/REC_SIZE) ; i++ )
{
getStructure(i, &t);
price = data;
if(t.price == *price)
{
return i;
}
}
return -1;
}


int findbyseats( void * data)
{
int i , cursize;
flight_t t;
cursize = sizeoffile ();
for(i =0 ; i < (cursize/REC_SIZE) ; i++ )
{
getStructure(i, &t);
if(t.seats == *((int *)data))
{
return i;
}
}
return -1;
}

int findbyfirst( void * data)
{
int i , cursize;
flight_t t;
int * first;
cursize = sizeoffile ();
for(i =0 ; i < (cursize/REC_SIZE) ; i++ )
{
getStructure(i, &t);
first = data;
if(t.first == *first )
{
return i;;
}
}
return -1;
}

int findbybusiness( void * data)
{
int i , cursize;
flight_t t;
int * business;
cursize = sizeoffile ();
for(i =0 ; i < (cursize/REC_SIZE) ; i++ )
{
getStructure(i, &t);
business = data;
if(t.business== *business )
{
return i;
}
}
return -1;
}

void deleteentry ( int entry_no )
{
int i,arr_counter=0;
char * tempfile = "_temp";

//flight_t temp [(sizeoffile()/REC_SIZE)];// should not be used for v
large buffer purposes .
flight_t temp2 [100]; // holds only a 100 structs = 8400 bytes = 8.2
Kb
for(i=0;i<(sizeoffile()/REC_SIZE);i++)
{

if(arr_counter > 99)
{
arr_counter = 0;
printf("\nStatus of write %d" ,add_flights(100 , temp2 , tempfile));
// dump records to file.
}

if(i==entry_no)
continue;
else
{
getStructure(i, &temp2[arr_counter]);
arr_counter++;
}
}

if(arr_counter>0)
{
printf("\nStatus of write %d" ,
add_flights(arr_counter,temp2,tempfile))
;
}

if(remove (filename))
{
if(rename (tempfile , filename))
{
if(!remove (tempfile))
printf("\nFailed to remove %s" , tempfile);
}
else
printf("\nFailed to rename %s" , tempfile);
}
else
printf("\nFailed to remove %s" , filename );


/*
if(!rename(tempfile,filename))
{
printf("\nFile write error you %d",errno);
perror(errno);
}
*/
}

Pascal Bourguignon

2005-09-24, 3:56 am

ebrahimbandookwala@gmail.com writes:

> Hi Im making this fairly simple c prog which reads and writes structs
> to a file in binary . I get this stupid error when I try doing the
> following ( in deleteentry () )
>
> if(remove (filename))
> {
> if(rename (tempfile , filename))
> {
> if(!remove (tempfile))
> printf("\nFailed to remove %s" , tempfile);
> }
> else
> printf("\nFailed to rename %s" , tempfile);
> }
> else
> printf("\nFailed to remove %s" , filename );
>
> which fails at either the first level or the second. Cant figure out y
> ?? For some reason it refuses to delete the file (filename) . I also
> tried plain and simple rename(tempfile -> filename) but that too
> refuses to work ?? Maybe im overlooking something really stupid !! ..


You've got some other problems too:

[pjb@thalassa tmp]$ ./fl


Flight Inventory...


Choose from Menu
1.Add Flight
2.Del Flight
3.View Flights
4.Exit

3

1. Flt 1 Num 1

2. Flt LA-LLMC Num 2


Flight Inventory...


Choose from Menu
1.Add Flight
2.Del Flight
3.View Flights
4.Exit

2

Entry to delete : 1
close: No such file or directory

Status of write 0
Failed to remove records.dat

Flight Inventory...


Choose from Menu
1.Add Flight
2.Del Flight
3.View Flights
4.Exit

3

1. Flt LA-LLMC Num 2

2. Flt LA-LLMC Num 2

3. Flt LA-LLMC Num 2

4. Flt LA-LLMC Num 2

5. Flt LA-LLMC Num 2

6. Flt LA-LLMC Num 2

7. Flt LA-LLMC Num 2

8. Flt LA-LLMC Num 2

9. Flt LA-LLMC Num 2

10. Flt LA-LLMC Num 2

.... (an infinite loop) ...

[pjb@thalassa tmp]$ rm -f records.dat _temp
rm -f records.dat _temp
[pjb@thalassa tmp]$ ./fl
../fl


Flight Inventory...


Choose from Menu
1.Add Flight
2.Del Flight
3.View Flights
4.Exit

3
3

1. Flt Num À@`@

2. Flt Num À@`@

3. Flt Num À@`@

4. Flt Num À@`@

5. Flt Num À@`@

6. Flt Num À@`@

7. Flt Num À@`@

.... (an infinite loop) ...


Otherwise, I cannot reach the remove/rename statements. You should
revise your invariants!

--
__Pascal_Bourguignon__ _ Software patents are endangering
() ASCII ribbon against html email (o_ the computer industry all around
/\ 1962:DO20I=1.100 //\ the world http://lpf.ai.mit.edu/
2001:my($f)=`fortune`; V_/ http://petition.eurolinux.org/
Barry Margolin

2005-09-24, 3:56 am

In article <1127540245.240039.71820@g14g2000cwa.googlegroups.com>,
ebrahimbandookwala@gmail.com wrote:

> Hi Im making this fairly simple c prog which reads and writes structs
> to a file in binary . I get this stupid error when I try doing the
> following ( in deleteentry () )
>
> if(remove (filename))
> {
> if(rename (tempfile , filename))
> {
> if(!remove (tempfile))
> printf("\nFailed to remove %s" , tempfile);
> }
> else
> printf("\nFailed to rename %s" , tempfile);
> }
> else
> printf("\nFailed to remove %s" , filename );
>
> which fails at either the first level or the second. Cant figure out y
> ?? For some reason it refuses to delete the file (filename) . I also
> tried plain and simple rename(tempfile -> filename) but that too
> refuses to work ?? Maybe im overlooking something really stupid !! ..


remove() and rename() return 0 when they're successful. 0 is false when
used in an if(). So I think the sense of your tests are backward. You
want something like

if(remove(filename) == 0)

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com