Thread: Segmentation fault when I try to read in the lines of a file

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    1

    Question Segmentation fault when I try to read in the lines of a file

    Hi,

    I have to txt files, and want them to read into an array line by line and after then a split the lines with delimeters, at the first file I use ";\n", at the second file I want to split the lines to single words by spaces. My aim is to get the first word from the first file and compare with all the words from the other file, to check is there any matches.
    My problem is, that at the first file works everything fine, and I want to do the with the second file, but after reading the lines into lines_input[], I check the an empty = null line, I get segmentation fault.
    Please help me, I don't know what I'm doing wrong.
    Thanks for every response!

    You can see the code below:
    Code:
    #include <stdio.h>#include <stdlib.h>
    #include <string.h>
    #include <strings.h>
    #include <unistd.h>
    
    
    
    
    //Change the \n and \t to \0
    void strip(char *s) {
        char *p2 = s;
        while(*s != '\0') {
            if(*s != '\t' && *s != '\n') {
                *p2++ = *s++;
            } else {
                ++s;
            }
        }
        *p2 = '\0';
    }
    
    
    char *concat (char * a, char* b)
    {
    
    
    
    
        char *c = malloc(strlen(a) + strlen(b) + 1);
       if ( b != NULL )
       {
    
    
          if ( c != NULL )
          {
             strcpy(c, a);
             strcat(c, b);
    
    
          }
       }
       return c;
    
    
    
    
    }
    
    
    int main(){
        
        // File read
        FILE *dict_f;
        dict_f = fopen("fogalom.txt", "r");
        // Check
        if (dict_f == NULL){ perror("Sikertelen fajlmegnyitas\n"); exit(1); }
        
        char line_f[101];
        char *lines_f[15];
        char *eof;
        
        //Reading line by line to lines_f[]
        int j=0;
        while((eof = fgets(line_f, 101, dict_f)) != NULL){
            lines_f[j] = strdup(eof);
            j++;
        }
        
        //fill up the places in the lines_f[] with null
        while (j!= 14)
        {
            lines_f[j] = NULL;
            j++;
        }
        
        fclose(dict_f);
            printf("%s\n", lines_f[0]);
              printf("%s\n ", lines_f[1]);
        printf("%s\n ", lines_f[9]);
        
        
        
        char *tomb[LINES];
        char *sor;
        int i;
        
        //Splittink
       
        if(lines_f[0] != NULL)
        {    
            sor = strdup(lines_f[0]);
            tomb[0] = strtok(sor,";\n");
            i = 0;
            
            while(tomb[i]!= NULL)
            {
                printf("%i. elem: %s\n", i, tomb[i]);
                i++;
                tomb[i] = strtok(NULL, ";\n"); 
            }
        }
        
        
        
        //----------NEXT FILE ---------------------------
        
        FILE *input_f;
        input_f = fopen("fogalom.txt", "r");
        
        if (input_f == NULL){ perror("Sikertelen fajlmegnyitas:\n"); exit(1); }
        
        char line_input[101];
        char *lines_input[15];
        
        
        
        j=0;
        while((eof = fgets(line_input, 101, input_f)) != NULL){
            lines_input[j] = strdup(eof);
            j++;
        }
        
        while (j!= 14)
        {
            lines_input[j] = NULL;
            j++;
        }
        
        fclose(input_f);
    
    return 0;
    
    }
    Attached Files Attached Files

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well it would help if you supplied your input files as well.

    FWIW, if you have more lines than you expect, it will blow up.

    For example, this would help
    while(j<15 && (eof = fgets(line_input, 101, input_f)) != NULL)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My program don't read all lines of a text file
    By Netcode in forum C Programming
    Replies: 5
    Last Post: 04-13-2012, 07:45 PM
  2. Replies: 6
    Last Post: 02-14-2012, 06:46 AM
  3. Read lines from a file
    By steffi in forum C Programming
    Replies: 2
    Last Post: 11-13-2007, 06:05 AM
  4. Best way to read lines out of a text file
    By movl0x1 in forum C Programming
    Replies: 9
    Last Post: 05-29-2007, 12:45 PM
  5. read multiple lines from a file
    By YankeePride13 in forum C Programming
    Replies: 2
    Last Post: 11-10-2005, 10:30 PM

Tags for this Thread