Sponsored Content
Top Forums Programming Segmentation fault in fopen when in write mode. Post 302672355 by shoaibjameel123 on Monday 16th of July 2012 09:26:29 AM
Old 07-16-2012
Segmentation fault in fopen when in write mode.

Hi All,

Although this code is quite long but it does not require one to understand the logic of the code. I am trying to run this code on my Linux machine using gcc. But when this line is executed:
Code:
output_pointer = fopen ( file_name , "w" );

I get segmentation fault. I've been breaking my head Smilie for a long time on this but with no clues. Exact error retrieved using gdb

Code:
Program received signal SIGSEGV, Segmentation fault.
0x00000031f4a7873e in _int_malloc () from /lib64/libc.so.6

This is my code:

Code:
#define _GNU_SOURCE
#define _BSD_SOURCE

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

#define NUMBER_OF_FILES 300000
#define LENGTH_OF_FILE 7617230 //number of records in the file

unsigned long int number_of_lines ( FILE * );
void file_name_generator ( unsigned long int , char * , char * );
char * itoa ( int , char * );
char * reverse ( char [] );
void file_names ( unsigned int ,char * , char * );
char * chomp ( char * );

int32_t main ( int32_t argc , char ** argv )
{
    FILE *output_pointer = NULL;
    FILE *bigram_counts = NULL;
    FILE *input_file = NULL;
    FILE *bigram_words = NULL;

    unsigned long int i = 0;
    unsigned long int number_of_words = 0;
    char *file_name = NULL;
    char **words_from_webpage = NULL;
    unsigned long int j = 0;
    unsigned long int *store_values = NULL;
    char **bigram_words_array = NULL;
    unsigned long int *bigram_counts_array = NULL;
    unsigned long int array_index = 0;
    char *str = NULL;

    char *line = NULL;
    size_t len = 0;
    ssize_t read = 0;
    
    bigram_words_array = ( char ** ) malloc ( LENGTH_OF_FILE * sizeof ( char * ) );
    if ( bigram_words_array == NULL )
    {
      fprintf ( stderr , "malloc() memory allocation failure\n" );
    }
    
    for ( i = 0 ; i < LENGTH_OF_FILE ; i ++ )
    {
        bigram_words_array [ i ] = ( char * ) malloc ( 50 * sizeof ( char ) );
        if ( bigram_words_array  [ i ] == NULL )
        {
            fprintf ( stderr , "malloc() memory allocation failure\n" );
        }
    }


    bigram_counts_array = ( unsigned long int * ) malloc ( LENGTH_OF_FILE * sizeof ( unsigned long int ) );
    if ( bigram_counts_array == NULL )
    {
      fprintf ( stderr , "malloc() memory allocation failure\n" );
    }

    str = ( char * ) malloc ( 4 * sizeof ( char ) );
    if ( str == NULL )
    {
        fprintf ( stderr , "malloc() memory allocation failure\n" );
    }

    ENTRY e , *ep;

    bigram_counts = fopen ( "counts.dat" , "r" );
    if ( bigram_counts == NULL )
    {
        fprintf ( stderr , "file read error\n" );
    }
    
    bigram_words = fopen ( "bigrams.dat" , "r" );
    if ( bigram_words == NULL )
    {
      fprintf ( stderr , "file read error\n" );
    }

    file_name = ( char * ) malloc ( 300 * sizeof ( char ) );
    if ( file_name == NULL )
    {
        fprintf ( stderr , "malloc() memory allocation failure\n" );
    }
    
    while ( !feof ( bigram_words ) )
    {
        while ( ( read = getline ( &line , &len , bigram_words ) ) != -1 )
            {
                line = chomp ( line );//this removes newline character
                strcpy ( bigram_words_array [ j ] , line );
                if ( j <= LENGTH_OF_FILE )
                {
                    j ++;
                }
            }
    }
    
    j = 0;
    
    while ( !feof ( bigram_counts ) )
    {
      fscanf ( bigram_counts , "%ld\n" , &bigram_counts_array [ j ++ ] );
    }
    j = 0;


    if ( line )
    {
        free ( line );
    }
        
    hcreate ( LENGTH_OF_FILE ); //building the hash table
    for ( i = 0 ; i < LENGTH_OF_FILE ; i ++ )
    {
        e.key = bigram_words_array [ i ];
        e.data = ( void * ) i;
        ep = hsearch ( e , ENTER );
        if ( ep == NULL )
        {
            fprintf ( stderr , "hash table entry failed\n" );
        }
    }

    fclose ( bigram_counts );
    fclose ( bigram_words );

    for ( i = 0 ; i < NUMBER_OF_FILES ; i ++ ) //this will iterate through all the files in the directory
    {
        file_name_generator ( ( i + 1 ) , file_name , str ); //this generates the file name of the file to be read
        memset ( str , 0 , strlen ( str ) );
        input_file = fopen ( file_name , "r" );
        if ( input_file == NULL )
        {
            memset ( file_name , 0 , strlen ( file_name ) );
            fclose ( input_file );
            continue;
        }

        number_of_words = number_of_lines ( input_file ); //computes the number of words in the data file
        store_values = ( unsigned long int * ) malloc ( number_of_words * sizeof ( unsigned long int ) ); this array will store all the numerical values corresponding to the words
        if ( store_values == NULL )
        {
            fprintf ( stderr , "malloc() memory allocation failure\n" );
        }

        words_from_webpage = ( char ** ) malloc ( number_of_words * sizeof ( char * ) ); //this read the data file consising of words each word in newline
        if ( words_from_webpage == NULL )
        {
            fprintf ( stderr , "malloc() memory allocation failure in words_from_webpage\n" );
        }

        for ( j = 0 ; j < number_of_words ; j ++ )
        {
            words_from_webpage [ j ] = ( char * ) malloc ( 20 * sizeof ( char ) );
            if ( words_from_webpage [ j ] == NULL )
            {
                fprintf ( stderr , "malloc() memory allocation failure\n" );
            }
        }

        j = 0;
        rewind ( input_file );

        while ( !feof ( input_file ) )
        {
            while ( ( read = getline ( &line , &len , input_file ) ) != -1 )
            {
                line = chomp ( line );
                strcpy ( words_from_webpage [ j ] , line );
                if ( j <= number_of_words )
                {
                    j ++;
                }
            }
        }

        if ( line )
        {
            free ( line );
        }

        for ( j = 0 ; j < number_of_words ; j ++ )
        {
            e.key = words_from_webpage [ j ];
            ep = hsearch ( e , FIND ); //find the word in the hash table and get the index of the word from the word list file
            array_index = ( unsigned long int ) ( ep -> data );
             * ( store_values + j ) = bigram_counts_array [ array_index ];
        }
        
        memset ( file_name , 0 , strlen ( file_name ) );

        file_names ( ( i + 1 ) , file_name , str ); //get the file path of the file to be written.
        
        output_pointer = fopen ( file_name , "w" ); //THIS IS WHERE THE PROBLEM OCCURS
        if ( output_pointer == NULL )
        {
            fprintf ( stderr , "file write error\n" );
        }

        j = 0;
        while ( j < number_of_words )
        {
            fprintf ( output_pointer , "%ld\n" , store_values [ j ] + 1 );
            j ++;
        }
        fclose ( output_pointer );

        memset ( store_values , 0 , sizeof ( store_values ) );
        memset ( file_name , 0 , strlen ( file_name ) );
        memset ( str , 0 , strlen ( str ) );

        fclose ( input_file );
        for ( j = 0 ; j < number_of_words ; j ++ )
        {
            words_from_webpage [ j ] = '\0';
        }
        memset ( words_from_webpage , 0 , strlen ( words_from_webpage ) );
    }


    free ( file_name );
    free ( str );

    for ( j = 0 ; j < number_of_words ; j ++ )
    {
        free ( words_from_webpage [ j ] );
    }
    free ( words_from_webpage );

    for ( j = 0 ; i < LENGTH_OF_FILE ; j ++ )
    {
        free ( bigram_words_array [ j ] );
    }
    free ( bigram_words_array );

    free ( bigram_counts_array );

    fclose ( bigram_counts );
    fclose ( bigram_words );

    return ( EXIT_SUCCESS );
}


char *chomp ( char * word )
{
    int32_t word_length = 0;
    word_length = strlen ( word );
    if ( word [ word_length - 1 ] == '\n' )
    {
        word [ word_length - 1 ] = '\0';
    }
    return ( word );
}


void file_names ( unsigned int i , char * file_name , char * str )
{
    char common_path[] = {"/data/scores/"};

    str = itoa ( i  , str );
    strcat ( file_name , common_path );
    strcat ( str , ".dat" );
    strcat ( file_name , str );
}


unsigned long int number_of_lines ( FILE *input_file )
{
    char ch;
    unsigned long int number_of_words = 0;

    while  ( ! ( feof ( input_file ) ) )
    {
        ch = fgetc ( input_file );
        if ( ch == '\n' )
        {
            number_of_words++;
        }
    }
    return ( number_of_words );
}


void file_name_generator ( unsigned long int i , char * file_name , char *str )
{
    strcat ( file_name , "/data/files/" );
    str = itoa ( i , str );
    strcat ( file_name, str );
    strcat ( file_name , ".dat" );
}


char * itoa ( int n , char * s )
{
     int i, sign;
 
     if ( ( sign = n ) < 0 )  /* record sign */
         n = -n;          /* make n positive */
     i = 0;
     do {       /* generate digits in reverse order */
         s [ i++ ] = n % 10 + '0';   /* get next digit */
     } while ( ( n /= 10 ) > 0 );     /* delete it */
     if ( sign < 0 )
         s [ i++ ] = '-';
     s [ i ] = '\0';
     reverse ( s );
    return ( s );
}


char * reverse ( char s [ ] )
{
     int i, j;
     char c;
 
    for ( i = 0, j = strlen ( s ) - 1; i < j; i ++, j -- ) 
    {
             c = s [ i ];
             s [ i ] = s [ j ];
             s [ j ] = c;
         }
    return ( s );
}

 

10 More Discussions You Might Find Interesting

1. Programming

segmentation fault

sometimes for this code i get a segmentation fault for codes llike this : int main{ int * a= 0; int b; a = (int*)malloc(sizeof(int)); ///some code using these variable but no freeing of a if(a){ free(a); a = 0; } return... (3 Replies)
Discussion started by: wojtyla
3 Replies

2. Programming

Hi! segmentation fault

I have written a program which takes a directory as command line arguments and displays all the dir and files in it. I don't know why I have a problem with the /etc directory.It displays all the directories and files untill it reaches a sub directory called peers which is in /etc/ppp/peers.the... (4 Replies)
Discussion started by: vijlak
4 Replies

3. AIX

Segmentation fault

Hi , During execution a backup binary i get following error "Program error 11 (Segmentation fault), saving core file in '/usr/datatools" Riyaz (2 Replies)
Discussion started by: rshaikh
2 Replies

4. Programming

segmentation fault

Hi, I am having this segmentation fault not in the following program, bt. in my lab program . My lab program is horrible long so cannot post it here bt. I am using the following logic in my program which is giving the segmentation fault. Bt. if I run this sample program as it is it dosen't give... (3 Replies)
Discussion started by: mind@work
3 Replies

5. Programming

Segmentation fault.

I'm getting a segmentation fault. I'm new to Linux programming. Thanks so much for all of your input.:eek: #include </usr/include/mysql++/mysql++.h> #include <stdio.h> #include <iostream> #include <sstream> #include <string.h> using namespace std; int outputToImport(const char*... (1 Reply)
Discussion started by: sepoto
1 Replies

6. UNIX for Dummies Questions & Answers

Segmentation fault

#include<stdio.h> #include<malloc.h> #include<unistd.h> #include<stdlib.h> void *start_1(void *argv) { printf("thread 0x%x\n",(unsigned int)pthread_self()); pthread_exit((void*)1); } void *start_2(void *argv) { printf("thread 0x%x\n",(unsigned int)pthread_self()); return (void*)2; }... (2 Replies)
Discussion started by: vincent__tse
2 Replies

7. Programming

Using gdb, ignore beginning segmentation fault until reproduce environment segmentation fault

I use a binary name (ie polo) it gets some parameter , so for debugging normally i do this : i wrote script for watchdog my app (polo) and check every second if it's not running then start it , the problem is , if my app , remain in state of segmentation fault for a while (ie 15 ... (6 Replies)
Discussion started by: pooyair
6 Replies

8. Solaris

Segmentation fault

Hi Guys, I just installed and booted a zone called testzone. When I logged in remotely and tried changing to root user I get this error: "Segmentation fault" Can someone please help me resolve this? Thanks alot (2 Replies)
Discussion started by: cjashu
2 Replies

9. Shell Programming and Scripting

Count Segmentation fault and write to the file

Hi everyone Need to get version of npm application that have several output like this: root: nmp -version 10 root: nmp -version 10 root: nmp-new -version 3.1 root: nmp-old -version Segmentation fault count them , after that write to the file like this: 10 2 3.1 1 (1 Reply)
Discussion started by: indeed_1
1 Replies

10. Programming

C. To segmentation fault or not to segmentation fault, that is the question.

Oddities with gcc, 2.95.3 for the AMIGA and 4.2.1 for MY current OSX 10.14.1... I am creating a basic calculator for the AMIGA ADE *NIX emulator in C as it does not have one. Below are two very condensed snippets of which I have added the results inside the each code section. IMPORTANT!... (11 Replies)
Discussion started by: wisecracker
11 Replies
All times are GMT -4. The time now is 03:53 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy