Thread: segmetation fault (reading and writing file in c)

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    9

    segmetation fault (reading and writing file in c)

    Hi all;

    I have problem in C. the problem is ,Here is my code

    Code:
    #include <string.h>
    #include <stdlib.h>
    #include <stdio.h>
    
    
    /*  uni() function takes an array an thier size.and it produce an array which is include unique element  */
    int uni(char **arr,int size)
    {
      int unique = 0; /* The length of dst after removing duplicates */
      int n=size;
      char *dst[n];     /* The set of stirings without duplicates */
      int i;
    
      /* The first number is never a duplicate */
      dst[unique++] = arr[0];
    
      /* Load the unique strings into the destination list */
      for ( i = unique; i < n; i++ ) {
        int has_dup = 0;
        int j;
    
        for ( j = 0; j < unique; j++ ) {
          if ( arr[i] == dst[j] )
            has_dup = 1;
        }
    
        if ( has_dup == 0 )
          dst[unique++] = arr[i];
      }
    
     /* Display the unique strings*/
       for ( i = 0; i < unique; i++ )
        printf ( "%s ", dst[i] );
      printf ( "\n" );
    
      return 0;
    }
    
    int main ( void )
    {
       static const char filename[] = "input1.txt";    
       char colour[20],name[20];
       char *image[1000];
    
       int i=0,h,x,y,w;
       FILE *file = fopen ( filename, "r" );
       if ( file != NULL )
       {
          char line [ 128 ]; /* or other suitable maximum line size */
    
          while ( fgets ( line, sizeof line, file ) != NULL ) /* read a line */
          {         
    	sscanf(line,"%s%d%d%d%d%s",name,&w,&h,&x,&y,colour); 
    
                   image[i]=colour;
                   i++;
          }
          uni(image,1000);
    	
          fclose ( file );
       }
       else
       {
          perror ( filename ); /* why didn't the file open? */
       }
       return 0;
    }
    input1.txt file include 1000 line something like that:
    Code:
    rectangle 0 0 1 1  orange
    rectangle 0 1 1 1  green
    rectangle 0 2 1 1  white
    rectangle 0 3 1 1  orange
    rectangle 0 4 1 1  white
    rectangle 0 5 1 1  black
    rectangle 0 6 1 1  blue
    rectangle 0 7 1 1  red
    rectangle 0 8 1 1  blue
    rectangle 0 9 1 1  white
    rectangle 0 10 1 1  green
    rectangle 0 11 1 1  green
    rectangle 0 12 1 1  orange
    rectangle 0 13 1 1  red
    rectangle 0 14 1 1  white
    rectangle 0 15 1 1  green
    rectangle 0 16 1 1  red
    rectangle 0 17 1 1  black
    i have read the input1.txt file then
    i want to take only colour word.then
    i want the hold this colour in array then
    i want to sent this array uni funcion then
    finally i want to print this array stdout.

    I want to do all of this steps , but my programs give me segmentation fault. !can i anyone help me?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    First error:
    Code:
                   image[i]=colour;
    is probably not what you want to do. You are copying the address of colour into image[i]. Since the next sscanf() will overwrite the content of colour, all you ever will get is the last colour FOR ALL of your image[0..n] entries.

    Second:
    Code:
          if ( arr[i] == dst[j] )
            has_dup = 1;
    Again, you are using the address of a string to compare with another. If you were to fix the above problem, this would fail for every string [as the code stands now, every string is a duplicate, since all strings are set to the same address]. You need to use strcmp() to compare strings - and notice that strcmp returns 0 for a match.

    This does in fact work, as long as you do not need dst AFTER the image variable is gone.
    Code:
        if ( has_dup == 0 )
          dst[unique++] = arr[i];
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    83
    Quote Originally Posted by tasosa View Post
    Hi all;

    I have problem in C. the problem is ,Here is my code

    Code:
    #include <string.h>
    #include <stdlib.h>
    #include <stdio.h>
    
    
    /*  uni() function takes an array an thier size.and it produce an array which is include unique element  */
    int uni(char **arr,int size)
    {
      int unique = 0; /* The length of dst after removing duplicates */
      int n=size;
      char *dst[n];     /* The set of stirings without duplicates */
      int i;
    
      /* The first number is never a duplicate */
      dst[unique++] = arr[0];
    
      /* Load the unique strings into the destination list */
      for ( i = unique; i < n; i++ ) {
        int has_dup = 0;
        int j;
    
        for ( j = 0; j < unique; j++ ) {
          if ( arr[i] == dst[j] )
            has_dup = 1;
        }
    
        if ( has_dup == 0 )
          dst[unique++] = arr[i];
      }
    
     /* Display the unique strings*/
       for ( i = 0; i < unique; i++ )
        printf ( "%s ", dst[i] );
      printf ( "\n" );
    
      return 0;
    }
    
    int main ( void )
    {
       static const char filename[] = "input1.txt";    
       char colour[20],name[20];
       char *image[1000];
    
       int i=0,h,x,y,w;
       FILE *file = fopen ( filename, "r" );
       if ( file != NULL )
       {
          char line [ 128 ]; /* or other suitable maximum line size */
    
          while ( fgets ( line, sizeof line, file ) != NULL ) /* read a line */
          {         
    	sscanf(line,"%s%d%d%d%d%s",name,&w,&h,&x,&y,colour); 
    
                   image[i]=colour;
                   i++;
          }
          uni(image,1000);
    	
          fclose ( file );
       }
       else
       {
          perror ( filename ); /* why didn't the file open? */
       }
       return 0;
    }
    input1.txt file include 1000 line something like that:
    Code:
    rectangle 0 0 1 1  orange
    rectangle 0 1 1 1  green
    rectangle 0 2 1 1  white
    rectangle 0 3 1 1  orange
    rectangle 0 4 1 1  white
    rectangle 0 5 1 1  black
    rectangle 0 6 1 1  blue
    rectangle 0 7 1 1  red
    rectangle 0 8 1 1  blue
    rectangle 0 9 1 1  white
    rectangle 0 10 1 1  green
    rectangle 0 11 1 1  green
    rectangle 0 12 1 1  orange
    rectangle 0 13 1 1  red
    rectangle 0 14 1 1  white
    rectangle 0 15 1 1  green
    rectangle 0 16 1 1  red
    rectangle 0 17 1 1  black
    i have read the input1.txt file then
    i want to take only colour word.then
    i want the hold this colour in array then
    i want to sent this array uni funcion then
    finally i want to print this array stdout.

    I want to do all of this steps , but my programs give me segmentation fault. !can i anyone help me?
    I think problem is char *dst[n]; with statement.
    It requires constant expression same as switch statment.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by shwetha_siddu View Post
    I think problem is char *dst[n]; with statement.
    It requires constant expression same as switch statment.
    The compiler would complain if that was the case (and there is of course nothing to prevent a const being added to size [and n is really not needed!] - but compilers know if something is a constant or not, and should give an error, not cause a seg-fault.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    1. why are you passing 1000 as array size if only i strings were entered?
    2. image[i]=colour; - you just store pointer to the local buffer, this pointer will be the same on each iteration, and buffer will be overwritten with the new data on the next iteration
    3. arr[i] == dst[j] - this compares pointers not strings, to compare strings strcmp function should be used
    4. has_dup = 1;
    you can exit the loop after that - no need to continue checking other strings
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Registered User
    Join Date
    Apr 2009
    Posts
    9
    Quote Originally Posted by matsp View Post
    First error:
    Code:
                   image[i]=colour;
    is probably not what you want to do. You are copying the address of colour into image[i]. Since the next sscanf() will overwrite the content of colour, all you ever will get is the last colour FOR ALL of your image[0..n] entries.

    --
    Mats
    actually i think main problem is that. When i compile the program it produce only one colour like you say "last colour FOR ALL of your image[0..n] entries. ". how can i manage. what should i do in order to fix this problem. i want to store colour into the array
    Last edited by tasosa; 04-13-2009 at 07:35 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 03-05-2009, 03:14 AM
  2. Reading out of and writing into the same file
    By Wiretron in forum C Programming
    Replies: 8
    Last Post: 12-30-2006, 02:04 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. file writing and reading
    By Micko in forum C Programming
    Replies: 8
    Last Post: 01-13-2004, 11:18 AM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM