0

I'm working on a program which is suppose to read information from a text file. With the help of this question I was able to make it read 1 line at the time. Now I've written a loop which runs trough the file and reads all of it:

int main(void)
{
    FILE * fp;
    fp = open_file("test.txt");
    while(1){
        if(feof(fp))
            break;
        product_t * product = read_line(fp);
        print_product(product);
    }
    fclose(fp);
    return(0);
}

Here is the whole program I'm using to read from the text file: https://pastebin.com/pe1XZj7c

The file I'm trying to read from looks like this:

H235 Sportello_dx N246 15:20:43 15:27:55
K542 Sportello_sx N247 10:03:10 10:15:30
A356 Maniglia_fronte G102 18:40:11 18:52:23

The program reads the whole file and processes the data correctly but then I get a segmentation fault. Here is the output of the whole program:

product_t code_product: H235 
product_t name: Sportello_dx 
product_t code_piece: N246 
product_t enter: 15:20:43 
product_t exit: 15:27:55 
product_t code_product: K542 
product_t name: Sportello_sx 
product_t code_piece: N247 
product_t enter: 10:3:10 
product_t exit: 10:15:30 
product_t code_product: A356 
product_t name: Maniglia_fronte 
product_t code_piece: G102 
product_t enter: 18:40:11 
product_t exit: 18:52:23 
Segmentation fault

I used GDB to try to figure out what is causing the segmentation fault. Here is the output it gave me including the backtrace:

Starting program: /home/trie/Desktop/Progett Asd/a.out 
product_t code_product: H235 
product_t name: Sportello_dx 
product_t code_piece: N246 
product_t enter: 15:20:43 
product_t exit: 15:27:55 
product_t code_product: K542 
product_t name: Sportello_sx 
product_t code_piece: N247 
product_t enter: 10:3:10 
product_t exit: 10:15:30 
product_t code_product: A356 
product_t name: Maniglia_fronte 
product_t code_piece: G102 
product_t enter: 18:40:11 
product_t exit: 18:52:23 

Program received signal SIGSEGV, Segmentation fault.
__GI_____strtol_l_internal (nptr=0x0, endptr=endptr@entry=0x0, 
    base=base@entry=10, group=group@entry=0, 
    loc=0x7ffff7dd4400 <_nl_global_locale>) at ../stdlib/strtol_l.c:293
293 ../stdlib/strtol_l.c: No such file or directory.
(gdb) backtrace
#0  __GI_____strtol_l_internal (nptr=0x0, endptr=endptr@entry=0x0, 
    base=base@entry=10, group=group@entry=0, 
    loc=0x7ffff7dd4400 <_nl_global_locale>) at ../stdlib/strtol_l.c:293
#1  0x00007ffff7a70c82 in __strtol (nptr=<optimized out>, 
    endptr=endptr@entry=0x0, base=base@entry=10) at ../stdlib/strtol.c:106
#2  0x00007ffff7a6e290 in atoi (nptr=<optimized out>) at atoi.c:27
#3  0x0000555555554b5b in timestring_to_time (timestring=0x5555557589ea "18")
    at assembly_line_manager.c:106
#4  0x0000555555554a95 in read_line (fp=0x555555757010)
    at assembly_line_manager.c:82
#5  0x0000555555554924 in main () at assembly_line_manager.c:38

It seems like the program is trying to call the atoi function with invalid data after everything has bean read from the file. why is it doing that? how can I prevent it?

BRHSM
  • 854
  • 3
  • 13
  • 48

0 Answers0