#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;
}
this 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. how can i fix this problem
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
input1.txt file include 1000 line something like that