system fucntion in C

Discussion in 'C Programming' started by aki, Oct 28, 2010.

  1. aki

    aki Guest

    Hi Guys ,

    I want to use system function in C to do the following work.

    cp <file1> <file2> and then ><file1>
    e,g cp \var\log\cpm_cpmd_1.log.1 \var\log\cpm_cpmd_1.log.2 and then
    >\var\log\cpm_cpmd_1.log.1


    1. g_config_info.cpmm_config.cpm_log_path=\var\log\
    2. p_g_log_limit_info[thread_id]->file_name= cpm_cpmd_1.log

    system("cp (g_config_info.cpmm_config.cpm_log_path)+
    (p_g_log_limit_info[thread_id]->file_name)
    +'.'+'1' (g_config_info.cpmm_config.cpm_log_path)+
    (p_g_log_limit_info[thread_id]->file_name)+'.'+'2'");


    I am doing like this but it is not working . It seems the command
    being executed is
    cp \var\log\cpm_cpmd_1.log.1\var\log\cpm_cpmd_1.log.2

    CAn anybody help .

    regards
    Akhielsh
    aki, Oct 28, 2010
    #1
    1. Advertising

  2. aki

    Seebs Guest

    On 2010-10-28, aki <> wrote:
    > I want to use system function in C to do the following work.


    I would suggest that you consider the possibility that you are not yet
    anywhere near ready to attempt this. I'll explain why.

    > cp <file1> <file2> and then ><file1>


    > e,g cp \var\log\cpm_cpmd_1.log.1 \var\log\cpm_cpmd_1.log.2 and then
    > >\var\log\cpm_cpmd_1.log.1


    Okay, right here:

    There are no systems on which there is such a thing as /var/log which use
    backslashes in paths.

    If you're sufficiently careless about details to get that wrong, you
    should NOT be writing code which will perform any sort of file operations!

    > 1. g_config_info.cpmm_config.cpm_log_path=\var\log\
    > 2. p_g_log_limit_info[thread_id]->file_name= cpm_cpmd_1.log


    Again, it's not backslashes.

    > system("cp (g_config_info.cpmm_config.cpm_log_path)+
    > (p_g_log_limit_info[thread_id]->file_name)


    Strings don't add in C.

    > +'.'+'1' (g_config_info.cpmm_config.cpm_log_path)+
    > (p_g_log_limit_info[thread_id]->file_name)+'.'+'2'");


    If you want to use a language where stuff like this works, it's not
    C.

    > CAn anybody help .


    I could, but I am concerned that if I give you any direction towards
    what you're trying to do, you'll end up doing horrible damage.

    .... But hey, not to *my* system.

    So, here's what to do.

    1. LEARN THE DIFFERENCE BETWEEN SLASH AND BACKSLASH. Now. Stop everything
    else until you can get that right consistently and effortlessly. More
    generally, if you don't immediately spot punctuation mismatches, you're
    not in a good place to be working in C.
    2. Understand that, in C, '.' is not a string, it's a character. Strings
    and characters are different.
    3. Usually, the right idiom for something like this is to use snprintf()
    to populate a command buffer. (If you have a very old compiler, or a MS
    compiler, it may lack snprintf(), in which case you might fall back on
    sprintf(), but you'll be in significant danger of buffer overruns.)

    Typically, the right way to do this is to figure out what your
    command looks like in the abstract:
    "cp <file1> <file2>"
    then figure out how to convert it to a format string.

    As to the ">file1", you shouldn't use system() for that, because you can
    do it natively in C with less code.

    -s
    --
    Copyright 2010, all wrongs reversed. Peter Seebach /
    http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
    http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
    I am not speaking for my employer, although they do rent some of my opinions.
    Seebs, Oct 28, 2010
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ra294

    Now() Fucntion and CurrentCulture

    ra294, Nov 25, 2004, in forum: ASP .Net
    Replies:
    5
    Views:
    615
    Hans Kesting
    Nov 26, 2004
  2. TOMERDR
    Replies:
    6
    Views:
    366
    Jonathan Mcdougall
    May 22, 2006
  3. Replies:
    7
    Views:
    348
  4. sophie_newbie
    Replies:
    4
    Views:
    232
    Paul Hankin
    Oct 18, 2007
  5. Steven D'Aprano

    Re: More Help with python .find fucntion

    Steven D'Aprano, Jan 8, 2011, in forum: Python
    Replies:
    0
    Views:
    498
    Steven D'Aprano
    Jan 8, 2011
Loading...

Share This Page