Thread: Concatenation using system()

  1. #1
    Registered User julianenepom's Avatar
    Join Date
    Sep 2010
    Location
    Portugal or UK
    Posts
    19

    Question Concatenation using system()

    Hi there. Im having some problems with string concatenation.

    The command in windows is:
    ren c:\temp\text.txt text2.txt

    But the number "2" i want to be a variable so i can change after i get this working.

    Code:
    main()
        {
        char var[] = "2";
    
        system("ren C:\\temp\\test.txt text + var[] + .txt");
      
        }
    Any help? =)

    Thx!

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Lookup strcat and strncat

    strcat - C++ Reference

    Tim S.

  3. #3
    Registered User julianenepom's Avatar
    Join Date
    Sep 2010
    Location
    Portugal or UK
    Posts
    19
    This code works, but i have some more issues when i use scanf! =O

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main ()
    {
      char var[] = "2";
      char str[80];
      strcpy (str,"ren C:\\temp\\text.txt text");
      strcat (str, var );
      strcat (str,".txt");
      puts (str);
      system(str);
      system("pause");
      
      return 0;
    }
    Last edited by julianenepom; 04-20-2011 at 10:08 AM.

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    sprintf() (or the safer snprintf() if you have it) is a cleaner way to achieve this.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by julianenepom View Post
    Yes!
    Its working! Thanks Stahta01!

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main ()
    {
      char var[] = "2";
      char str[80];
      strcpy (str,"ren C:\\temp\\text.txt text");
      strcat (str, var );
      strcat (str,".txt");
      puts (str);
      system(str);
      system("pause");
      
      return 0;
    }
    Of course you could also do this...

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main (void)
      { char fname[260];  
        int seq = 2;  
    
        sprintf(fname,"text%d.txt",seq);
        rename("c:\\temp\\text.txt", fname);
    
        return 0; }
    ... since C has a rename() function in it's libraries there's no reason to call system.
    (Well, unless you're experimenting with system() calls, that is)

  6. #6
    Registered User julianenepom's Avatar
    Join Date
    Sep 2010
    Location
    Portugal or UK
    Posts
    19

    Question More problems.. =P

    Well, now i cant do some other stuff...

    In this code, the file text.txt is beeing deleted.
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main (void)
      { char fname[260];  
        int seq = 2;  
    
        sprintf(fname,"text%d.txt",seq);
        rename("c:\\temp\\text.txt", fname);
    
        return 0; }
    The code I uded is ok, but Im getting this problem when I added "scanf":
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main ()
    {
      char var[] = "";
      char str[80];
    
      scanf("%c", &var);
    
      strcpy (str,"ren C:\\temp\\text.txt text");
      strcat (str, var );
      strcat (str,".txt");
      puts (str);
      system(str);
      system("pause");
      
      return 0;
    }
    If I put the number 632, the result is something like 6♂².

    Dont know what is happening... =P

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > char var[] = "";
    The minimum length of a string, assuming you want to append anything other than just \0 (a no-op) is 2 characters.

    char var[2] = "";
    scanf( "%c", &var[0] );

    But if you type in say 632, you need a longer string to being with.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User julianenepom's Avatar
    Join Date
    Sep 2010
    Location
    Portugal or UK
    Posts
    19
    Quote Originally Posted by Salem View Post
    > char var[] = "";
    The minimum length of a string, assuming you want to append anything other than just \0 (a no-op) is 2 characters.

    char var[2] = "";
    scanf( "%c", &var[0] );

    But if you type in say 632, you need a longer string to being with.
    Yea. The input size is 8. I will try it here.

    Thx

  9. #9
    Registered User julianenepom's Avatar
    Join Date
    Sep 2010
    Location
    Portugal or UK
    Posts
    19

    Unhappy

    Ok. The error that I get is:
    When I try to Input a date like "20110420" using scanf, it gets only the first number, that is "2".

    The code is:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main ()
    {
      char var[8] = "";
      char str[80];
    
      scanf( "%c", &var[0] );
    
      strcpy (str,"ren C:\\temp\\text.txt text");
      strcat (str, var );
      strcat (str,".txt");
      puts (str);
      system(str);
      system("pause");
      
      return 0;
    }
    I think its a problem in the variable, but i really cant see what Im doing wrong... =P

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main ()
    {
      char var[8] = "";
      char str[80];
    
      scanf( "%s", &var[0] );
    
      strcpy (str,"ren C:\\temp\\text.txt text");
      strcat (str, var );
      strcat (str,".txt");
      puts (str);
      system(str);
      system("pause");
      
      return 0;
    }

  11. #11
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    The snippet below you are getting a single character.
    Code:
    scanf( "%c", &var[0] );
    If you want to get a character string you would use:
    Code:
    scanf("%s",var);
    Also don't forget to allow for the end of string character. You have defined your C-string having room for 7 characters plus the end of string but you want to input 8 characters so your array should be at least 9 characters.

    Jim

  12. #12
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    %c is telling scanf to read in one character. If you want to read in a string, you need %s. If you know that you are only going to read in x number of characters, you can use %xs to ensure that you don't overflow your input buffer. Make sure and leave room for a terminating null byte.
    bit∙hub [bit-huhb] n. A source and destination for information.

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by julianenepom View Post
    In this code, the file text.txt is beeing deleted.
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main (void)
      { char fname[260];  
        int seq = 2;  
    
        sprintf(fname,"text%d.txt",seq);
        rename("c:\\temp\\text.txt", fname);
    
        return 0; }
    Ahhh... my bad. I don't use rename() very often and it turns out it needs the full path both times. Your file wasn't being deleted it was being moved to the folder the program is in (i.e. the current working directory). Try it like this...

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main (void)
      { char fname[260];  
        int seq = 2;  
       
        sprintf(fname,"C:\\temp\\text%d.txt",seq);
        printf("%s",fname);
        rename("C:\\temp\\text.txt", fname);
        return 0; }

  14. #14
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    When I try to Input a date like "20110420"
    to enter string of 8 chars you need a buffer of 9 chars at least

    Code:
    char vals[9]={0};
    scanf("%8s",vals); /* 8 here is put to prevent reading more data than yu have space in your buffer */
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  15. #15
    Registered User julianenepom's Avatar
    Join Date
    Sep 2010
    Location
    Portugal or UK
    Posts
    19
    Thanks all!! Its working! =)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. concatenation
    By valthyx in forum C Programming
    Replies: 5
    Last Post: 01-17-2010, 08:22 AM
  2. Concatenation ?
    By koolguysj in forum C Programming
    Replies: 8
    Last Post: 04-15-2005, 04:26 PM
  3. concatenation
    By rose2626 in forum C++ Programming
    Replies: 10
    Last Post: 04-25-2003, 01:27 PM
  4. concatenation
    By F*SH in forum C++ Programming
    Replies: 34
    Last Post: 11-13-2002, 06:47 PM
  5. Concatenation in C++
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 12-05-2001, 01:05 PM