I have a program that will fetch some particular lines and store it in a buffer for further operations.The code which is given below works but with some errors.I couldn't trace out the error.Can anybody help on this plz??

#include <stdio.h>
#include <stdlib.h>
#include<string.h>

#define LINESIZE 256

void failif (int condition, char * message)
{
    if (condition)
    {
         printf("%s\n", message);
         exit(EXIT_FAILURE);
    }
}

void read_lines (char * * lines_array, FILE * fp, int start, int end)
{
    char line[LINESIZE];
    int i = 0, line_counter = 1;

    while(fgets(line, sizeof(line), fp) != NULL)
    {
        //Once line_counter matches up with start, "start recording" till end.
        if((line_counter >= start) && (line_counter <= end))
        {
            lines_array[i++] = strdup(line);
        }
        line_counter++;
    }
}

int main (int argc, char const *argv[])
{
    FILE *fp;
    int start,end,i;
    char * * lines_array;
    
    failif (argc != 4, "this program requieres three arguments");

    fp = fopen(argv[1], "rb");
    failif (!fp, "Could Not Open File");
    
    // The line range I want
    start   = atoi(argv[2]);
    end     = atoi(argv[3]);
    
    // allocate storage for the lines:
    lines_array = (char **) malloc((end - start) * sizeof(char *));
    failif (!lines_array, "memory allocation failed");

    read_lines (lines_array, fp, start, end);

    for(i = 1; i <( end - start)+1; ++i)
    {
        printf("\n%d.%s",i, lines_array[i]);
    }

     new code: free memory used by the allocated storage
    for (i = 0; i < end - start; i++)
        free (lines_array[i]);
    free (lines_array);

    return 0;
}

this is the output i get

[root@wer ~]# ../a.out "ug2.io" "8" "15"

1.wintmyd3 - D3 sponsor car

2.davidchoeart - David Choe sponsor car

3.tunejapantuning - Japantuning sponsor car

4.gimmechingy - Chingy Navigator sponsor car

5.needmybestbuy - Best Buy vinyl in Career mode

6.goforoldspice - OldSpice vinyl in Career mode

7.gottahavebk - BurgerKing vinyl in Career mode
*** glibc detected *** ./a.out: free(): invalid next size (fast): 0x0878c170 ***
======= Backtrace: =========
/lib/i686/nosegneg/libc.so.6[0xbfd651]
/lib/i686/nosegneg/libc.so.6(cfree+0x90)[0xc00cd0]
./a.out[0x8048722]
/lib/i686/nosegneg/libc.so.6(__libc_start_main+0xe0)[0xba9390]
./a.out[0x80484b1]
======= Memory map: ========
003a0000-003ab000 r-xp 00000000 08:07 7662226 /lib/libgcc_s-4.1.2-20070925.so.1
003ab000-003ac000 rwxp 0000a000 08:07 7662226 /lib/libgcc_s-4.1.2-20070925.so.1
00407000-00408000 r-xp 00407000 00:00 0 [vdso]
00b74000-00b8f000 r-xp 00000000 08:07 7662217 /lib/ld-2.7.so
00b8f000-00b90000 r-xp 0001a000 08:07 7662217 /lib/ld-2.7.so
00b90000-00b91000 rwxp 0001b000 08:07 7662217 /lib/ld-2.7.so
00b93000-00ce9000 r-xp 00000000 08:07 7662218 /lib/i686/nosegneg/libc-2.7.so
00ce9000-00ceb000 r-xp 00156000 08:07 7662218 /lib/i686/nosegneg/libc-2.7.so
00ceb000-00cec000 rwxp 00158000 08:07 7662218 /lib/i686/nosegneg/libc-2.7.so
00cec000-00cef000 rwxp 00cec000 00:00 0
08048000-08049000 r-xp 00000000 08:07 4816575 /root/a.out
08049000-0804a000 rw-p 00000000 08:07 4816575 /root/a.out
0878c000-087ad000 rw-p 0878c000 00:00 0
b7e00000-b7e21000 rw-p b7e00000 00:00 0
b7e21000-b7f00000 ---p b7e21000 00:00 0
b7f9f000-b7fa0000 rw-p b7f9f000 00:00 0
b7fbc000-b7fbf000 rw-p b7fbc000 00:00 0
bff49000-bff5f000 rw-p bff49000 00:00 0 [stack]
Aborted

this is the ug2.10 file

NOTE: These are the main Need for Speed: Underground 2 cheats that are usually applied by pressing down the right key combination. Please find the instructions on how to use these cheats below.

Enter one of the following codes before pressing [Enter] to get into the main menu to activate the corresponding cheat function:

opendoors - The Doors sponsor car
yodogg - Snoop Dogg sponsor car
wannacapone - Capone sponsor car
shinestreetbright - ShinestStreet sponsor car
wintmyd3 - D3 sponsor car
davidchoeart - David Choe sponsor car
tunejapantuning - Japantuning sponsor car
gimmechingy - Chingy Navigator sponsor car
needmybestbuy - Best Buy vinyl in Career mode
goforoldspice - OldSpice vinyl in Career mode
gottahavebk - BurgerKing vinyl in Career mode
gotmycingular - Cingular vinyl in Career mode
gottaedge - Edge vinyl in Career mode
needperformance1 - Unlock performance parts level 1
needperformance2 - Unlock performance parts level 2
gimmevisual1 - Unlock visuals level 1
gimmevisual2 - Unlock visuals level 2
ordermebaby - $1,000 in Career mode, RX-8 and Skyline in quick race mode
regmybank - $200 in Career mode
regmebaby - $20,000 in Career mode

Lines to be read: end - start + 1
Pointers allocated: end - start

Did you understand?

Use code tag with the language specifier!!!
[code=c] sources

[/code]

Always check program arguments: argc == 4 then start > 0 and start <= end.

Comments
Say yes to "fencepost errors"
This article has been dead for over six months. Start a new discussion instead.