system fucntion in C

A

aki

Hi Guys ,

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

cp said:
>\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
 
S

Seebs

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

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top