Thread: STRCAT problem

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    5

    STRCAT problem

    Hi!
    I wanted to thank you a lot for the help given yesterday...
    It`s so great to have people so well prepared trying to help you!

    I have another problem: the bold line :
    strcat want a pointer as argument, but probably i'm mistaking it ... can anybody help me?

    Code:
    #include <stdio.h>
    #include <string.h>
    
    main ()
    {
    int i;
    char j;
    char *ip;
    
    for (i = 0; i < 10; i++)
        {
    	ip=&j;
          for (j = 0; j<3; j++)
    	  {
    		FILE *fp;
    		
          char filename[17] ;
    	  
          if      (i == 0) 
    		{
    		 	strcpy(filename, "injection-0");
    			if (j==0) strcat(filename,"_11.txt");
    			else if (j==1) strcat (filename,*ip);
    			else if (j==2) strcat (filename,"_12.txt");
    		}
    if ((fp = fopen(filename, "w")) != NULL)
    		{
    			fprintf(fp, "mass_part    mass_flow_part   diam_part      accretion      surf_ID   cel_ID    temp           vel_abs        vel_nor        vx             vy             vz             Ax             Ay             Az             Angle		Wa		Wb		Wc		WEAR\n");
    			fclose(fp);
    		}
    	  }
        }
    }

  2. #2
    Registered User
    Join Date
    Jun 2006
    Posts
    130
    I think the problem with *ip here is the mistake
    Last edited by Moony; 07-04-2007 at 05:33 AM.

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    First of all you need a proper indentation. It is horrible. The error you tend to get is because of this

    Code:
    strcat (filename,*ip);
    which should be

    Code:
    strcat (filename,ip); // Which takes a pointer, not the dereferenced value
    And what is this
    Code:
     for (j = 0; j<3; j++)
    J is a char, you tend to use it as an int. And always make sure that target string has enough space before using strcat. Otherwise u will end up with SF.

    ssharish2005

  4. #4
    Registered User
    Join Date
    Jun 2006
    Posts
    130
    try ip without *.

  5. #5
    Registered User
    Join Date
    Jul 2007
    Posts
    5
    Or in the pointing process..
    If strcat needs a pointer as second argument... isn't this the way to pass the parameter?

    It gives me these warning... both in the bold line...
    Inizialize_dpm.c(23) : warning C4047: 'function' : 'const char *' differs in levels of indirection Inizialize_dpm.c(23) : warning C4024: 'strcat' : different types for formal and actual parameter 2

  6. #6
    Registered User
    Join Date
    Jul 2007
    Posts
    5
    Yes, the indentation makes people puke...

    if i switch fron char to int and taking out the "*"...
    it tells me...

    warning C4133: 'function' : incompatible types - from 'int *' to 'const char *'

    this is why I was trying to use a char instead of a int...
    what do you think?

  7. #7
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    First af all you have to increase the size of filename. You're trying to squeeze 20 chars into a buffer of 17 chars.
    I cannot follow the logic of how that files are supposed to be called but I would try something like this
    Code:
    char filename[21];
    for ( i = 0; i < 12; ++i )
         sprintf(filename, "injection-0_&#37;02d.txt", i );
    Kurt

  8. #8
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Quote Originally Posted by Frusciante View Post
    Yes, the indentation makes people puke...

    if i switch fron char to int and taking out the "*"...
    it tells me...

    warning C4133: 'function' : incompatible types - from 'int *' to 'const char *'

    this is why I was trying to use a char instead of a int...
    what do you think?
    Who said it should be changed to an int?

  9. #9
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main ()
    {
        int i;
        int j;
        char ip[5];
        FILE *fp;
        char filename[25] ;
        
        for (i = 0; i < 10; i++)
        {
            for (j = 0; j<3; j++)
    	    {
                sprintf(ip,"&#37;d",j);
    
                if(i == 0) 
    		    {
    		 	     strcpy(filename, "injection-0");
    			     if (j==0) 
                        strcat(filename,"_11.txt");
    			     else if (j==1) 
    			     {
                        strcat (filename,ip);
                        strcat (filename,".txt");
                     }   
    			     else if (j==2) 
                        strcat (filename,"_12.txt");
                }
                
                if ((fp = fopen(filename, "w")) != NULL)
    		    {
                    fprintf(fp, "mass_part    mass_flow_part   diam_part      accretion      surf_ID   cel_ID    temp           vel_abs        vel_nor        vx             vy             vz             Ax             Ay             Az             Angle		Wa		Wb		Wc		WEAR\n");
    			    fclose(fp);
    	        }
           }
        }
        getchar();
        return 0;
    }
    This is what u wanted. Now dont just copy the code and paste it. Go through it again, Look at the chnages make. And see why the changes has been made. If you still dont understand ask questions.

    ssharish2005

  10. #10
    Registered User
    Join Date
    Jul 2007
    Posts
    5
    Zuk...
    I got it... thanks for the sprintf hint!
    Now it works pretty well!
    And thanks everybody for the patience!

    Frusciante

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with strcat
    By firyace in forum C Programming
    Replies: 9
    Last Post: 05-15-2007, 02:31 PM
  2. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  3. Problem with strcat() function
    By sureshkumarct in forum C Programming
    Replies: 6
    Last Post: 01-03-2007, 08:05 PM
  4. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  5. beginner problem
    By The_Nymph in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 05:46 PM