Hi all;

I have problem in C. the problem is ,Here is my 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:

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?

if you are programming in linux ,then debug the program using gdb or some other debugger and find out where segementation fault problem is occuring.

according to mattp:

Segmentation fault is caused by referencing memory thats already been freed, using pointers incorrectly, or trying to access some messed up hardware.

I think you have problem in using pointers incorrectly.


try to use *arr not **arr what I read is **arr is for 2 dimensional array ***arr is for 3 dimensional array in your code you are using only one dimensioned array.

I will try your code at home...

try this *arr not **arr

int uni(char *arr,int size)

i will test your code later.....

What a strange code! All image elements points to the same memory (an array colour). Probably there are some other defects but it's a full stop. In actual fact you didn't get any useful data from the file.

Your array image is an array of pointers, not an array of strings. Try to feel the difference then redesign your code and come back...

1. remove [] in char *dst[]; /* The set of stirings without duplicates */

char *dst;

2. lack of "()" in sizeof(line)

your code: while ( fgets ( line, sizeof line, file ) != NULL ) /* read a line */

should be --> while ( fgets ( line, sizeof(line), file ) != NULL ) /* read a line */

i keep seeing your screen name, and thinking it says: Mr Daniweb


i figure I'm either kind of dyslexic, or I'm onto something big....

:S

don't even think about that I am not the the owner of this website. :)

"Only the True Messiah denies the nature of his divinity"

#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++;
      }
	for(i=0;i<1000;i++){   /* this for statement shows us what are image array include */
		printf("%s\n",image[i]);
		}
      fclose ( file );
   }
   else
   {
      perror ( filename ); /* why didn't the file open? */
   }
   return 0;
}

When i remove uni(image,1000) this line then compiler doesnt gives us seg-fault.but this time the program is copying the address of colour into image. Since the next sscanf() will overwrite the content of colour, all i ever will get is the last colour FOR ALL of my image[0..n] entries.

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++;
      }

how can i manege this proplem. what should i do?

don't remove uni(image,1000) who told you to remove that?

edit this:
char *dst[n]; /* The set of stirings without duplicates */

try char *dst; only

#
#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; /* 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;
#
}

it works now....

good effort helping this poster, but is that code ^ readable to you? if i copy and paste it, will it even compile?


.

try to compiled it on my pc it was successfully compiled as an exe file.

i mean, is it remotely readable? look at it. think about it.

hint: the answer is "no".

if on your side is "no" why don't you post the correct one. :)

do you have a reading/comprehension disability or something? i'm not saying your code is wrong. i have no idea if its wrong or right because I cant read the garbled formatting that includes 3 lines of junk for every line of code.

and have neither the time nor inclination to reformat your convoluted mess.. if you cant be coherent, then dont bother

"Only the True Messiah denies the nature of his divinity"

now you see that I am not the Messiah :)

yeah, well i'm still wondering what kind of person chooses a screenname that deceptively suggests they are somehow affiliated with or officially representing the administration or ownership of the website they just joined.

knowwutimean, Mr. Daniweb?

2. lack of "()" in sizeof(line)

your code: while ( fgets ( line, sizeof line, file ) != NULL ) /* read a line */

should be --> while ( fgets ( line, sizeof(line), file ) != NULL ) /* read a line */

What? sizeof doesn't always require (), but I like to use them anyway myself. There was nothing wrong with the syntax. Read this

try to compiled it on my pc it was successfully compiled as an exe file.

Maybe you did successfully compile the code you have on your computer, but you won't the code that you posted, because all of those # characters. Copy the code you pasted back to your compiler and try to compile it, I'm 100% certain that the compiler will hate it.

It almost looks like you copied that code off some web site because I have seen those # characters somewhere before. If you copied the code from a text file residing on your computer those # characters should not have appeared.

yeah, well i'm still wondering what kind of person chooses a screenname that deceptively suggests they are somehow affiliated with or officially representing the administration or ownership of the website they just joined.

knowwutimean, Mr. Daniweb?

now you know who has reading/comprehension disability or something?

mr jephthah

commented: F--- it. im tired of seeing your name. time for you to reroll. +6

Maybe you did successfully compile the code you have on your computer, but you won't the code that you posted, because all of those # characters. Copy the code you pasted back to your compiler and try to compile it, I'm 100% certain that the compiler will hate it.

It almost looks like you copied that code off some web site because I have seen those # characters somewhere before. If you copied the code from a text file residing on your computer those # characters should not have appeared.

sorry guys! I will fix that later.... I just copy the code in POST #1. I am very new in this forum so I am not aware of the results when I paste it. When I paste that..I logout immediately without reviewing that code. try it and you will see the results...

>>sorry guys! I will fix that later.... I just copy the code in POST #1

Oh, I see. Then you want to copy the code from post #1, click the link that says "Toggle Plain Text". Then right-click inside the box, in the menu select "Select All", do it again and select "Copy". That puts it into the clipboard so that you can paste it anywhere you want.

>>I logout immediately without reviewing that code.
DON'T EVER DO THAT. Always review what you posted so that you can correct mistakes (I do it all the time :) ) Next time your post will just get deleted.

>>try it and you will see the results..
Why should I bother ? Re-post it correctly and I might.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.