reading txt file then sending to function

This is a discussion on reading txt file then sending to function within the C Programming forums, part of the General Programming Boards category; Code: #include <string.h> #include <stdlib.h> #include <stdio.h> /* uni() function takes an array an thier size.and it produce an array ...

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

    reading txt file then sending to function

    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 name[20];
       char *image[1000];
       int i=0,h,x,y,w,j=0;
    
       /*Malloc for image*/
       for(j=0;j<1000;j++)
    	image[j] = malloc(sizeof(char *)*10);
       /*End Malloc*/
       
       FILE *file = fopen ( filename, "r" );
       if ( file != NULL )
       {
          char line [ 128 ];
    
          while ( fgets ( line, sizeof line, file ) != NULL ){         
    	sscanf(line,"%s%d%d%d%d%s",name,&w,&h,&x,&y,image[i++]); 
     
          }  
          fclose ( file );
            
       }
       else
       {
          perror ( filename ); /* why didn't the file open? */
       }
    	
    	uni(image,1000);
       return 0;
    }


    1) uni() function for making a unique array
    2) the main function read a txt file which is include

    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

    something like that (1000 or more line ).
    then i want to filter only colour name without duplicating. But my program doesn't work .it produce all colour (with duplicate colour name).. I tested uni() function it works properly. Why my program doesn't work properly (i want to print out only colour name without duplicated )..

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You can't compare strings using the == operator. You need something like strcmp function.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    IMO you're making this tad more complicated than it needs to be, and strcmp() is the way to go for comparing strings.

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    And i can also see the declaring variables all over the place. That is not a C standard. It is defined to be true with the C++. But with the common C practice all variables should be declared at the top of the function. Which makes it neat.

    -ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by ssharish2005 View Post
    And i can also see the declaring variables all over the place. That is not a C standard. It is defined to be true with the C++. But with the common C practice all variables should be declared at the top of the function. Which makes it neat.

    -ssharish
    That's not entirely accurate. It has to be at the top of a code block.
    Code:
    if( x == y )
    {
        int thisisfine = 1;
        ...
        for( thisisfine = 1; thisisfine; thisisfine++ )
        {
            char thisisalsofine[] = "yep";
            ...
        }
    }
    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  3. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  4. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21