STRCAT problem

Discussion in 'C' started by Frusciante, Jul 4, 2007.

  1. Frusciante

    Frusciante New Member

    Joined:
    Jul 4, 2007
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    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");
    			[B]else if (j==1) strcat (filename,*ip)[/B];
    			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);
    		}
    	  }
        }
    }
    
    Edited by DaWei to add code tags. Please do this yourself, in future.
     
  2. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    ip is a pointer to a char, not to an array of char (a string), as required by strcat.

    You also have a potentially bad issue: unless you have a C99 compliant C compiler, you'll have problems if you declare variables down in the body of the code. For C code, put all your declarations at the top, before any runtime statements. Even if your compiler tolerates it, it isn't portable code. If you're writing C and compiling it as C++, that's not a Good Thang.

    On the issue of formatting, please put your code in code tags, hereafter. Read the "Before you make a query" thread. I'd also recommend paying a little more attention to your formatting. Be consistent with your indentation. If you're planning on being a professional, someone else is going to wind up maintaining your code. They might be big, muscular, and mean.
     
  3. Frusciante

    Frusciante New Member

    Joined:
    Jul 4, 2007
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Thanks for your advices DaWei!
    I will remember them! :)

    Is it possible to concatenate just an int to the char filename?

    In that case I would be safe...
     
  4. Frusciante

    Frusciante New Member

    Joined:
    Jul 4, 2007
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    I got it...
    I can use sprintf

    PHP:
    sprintf(filename"injection-%1d_%1d.txt"i,j);
     
  5. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    Just as a reminder, move the red statements to where shown in blue. This is a scoping issue that will affect portability of C code. It's great in C++.
    Code:
    main ()
    {
    int i;
    char j;
    char *ip;
    [COLOR=Blue]FILE *fp;[/COLOR]
    [COLOR=Blue]char filename[17] ;[/COLOR]
    for (i = 0; i < 10; i++)
        {
    	ip=&j;
            for (j = 0; j<3; j++)
    	{
    	    [COLOR=Red]FILE *fp;[/COLOR]
                [COLOR=Red]char filename[17] ;[/COLOR]
    ...
    
     
  6. Frusciante

    Frusciante New Member

    Joined:
    Jul 4, 2007
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Good to know!
    :)
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice