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?