Thread: Calling system with a string variable

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    23

    Calling system with a string variable

    Can a call to system be string variable or actual string in double quoted form?
    Code:
        	sprintf(cmd, "cp %d.txt %d.csv",i,i);
    	pid_err=system(cmd); //Problem here: it's printing garbage on the screen

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Quote Originally Posted by 911help View Post
    Can a call to system be string variable or actual string in double quoted form?
    Code:
            sprintf(cmd, "cp %d.txt %d.csv",i,i);
        pid_err=system(cmd); //Problem here: it's printing garbage on the screen
    >Can a call to system be string variable or actual string in double quoted form?
    Either of them is acceptable. Fo instance

    Code:
    char str[] = "clear";
    system(str);
    or
    system("clear");
    >Problem here: it's printing garbage on the screen
    If the return value was other than 0 then the system function failed.

    ssharish
    Last edited by ssharish2005; 12-27-2007 at 08:44 AM.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    23
    I am using gdb to walk through the break points.

    printf("print before system(cmd) call\n");
    pid_err=system(cmd); //Problem here: it's printing garbage on the screen
    printf("print after system(cmd) call\n");

    Soon as I call system(cmd), garbage is printed out on the screen. why?

    sample output:
    ��ႇ
    .x��ႇ
    .x��ႇ
    .x��ႇ
    �<\�p��<\�p�

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Check the system() return value and see if your system call was successful.

    Code:
    if( system("clear") )
    {
        printf( " Error " );
        return 1;
    }
    else
       printf( " < Message > " );
    ssharish

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    23
    I don't know what the problem here. I tried to gzip the file using system(). Text file is there, but never get gzipped. I get garbage printed out on the console soon as I call system();.

    Do I have to have root access on the linux shell to be able to call system? Does
    sprintf(cmd, "gzip -c ./data_id_%d.txt",i); put a '\0' in cmd, after placing the string "gzip -c ./data_id_%d.txt"?

    server.conf:
    Code:
    num_of_files=3;
    file_temp.c:
    Code:
    #include <string.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <fcntl.h>
    #include <unistd.h>
    
    #define SIZE 256
    
    FILE *configfile;
    int  num_files;
    char conf[100];
    char *buffer;
    
    char cmd[SIZE];
    
    int main (int argc, char *argv[])
    {
    int i=0;
    	configfile= fopen("./server.conf", "r");
    	fgets(conf,100, configfile);
    	fclose(configfile);
        buffer=strtok(conf,";");
        strtok(buffer,"=");
        num_files=atoi(strtok(NULL,"=")); 
        sprintf(cmd, "gzip -c ./data_id_%d.txt",i);
        printf("system(%s)\n", cmd);
        if( system(cmd) )
        {
        	printf( " Error \n" );
        	return 1;
        }
        else
        	printf( " < System call success > \n" );
        printf("After the system call\n");
        
        exit (0);
    }
    Here is my gdb output.
    Code:
    ****printted whole bunch of garbage here.********* and then stuff below*********
    System call has success > 
    
    Breakpoint 2, main () at file_temp.c:34
    34          printf("After the system call\n");
    (gdb) continue
    Continuing.
    After the system call
    
    Program exited normally.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    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.

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    23
    Salem, I tried fork as you mentioned on Daniweb. It gave a problem. I wanted to try system call. This is also giving me problems.

    Can someone help with this? No one had done zipping stuff from system call?

    Anything else I need to do, i.e setuid?
    Last edited by 911help; 12-27-2007 at 01:07 PM. Reason: this is driving me crazy. ;(

  8. #8
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by 911help View Post
    ****printted whole bunch of garbage here.********* and then stuff below*********
    The "garbage" is undoubtedly the output of gzip -c. (With the -c flag, gzip sends output to stdout.)

    D
    Last edited by Dave Evans; 12-27-2007 at 05:01 PM.

  9. #9
    Registered User
    Join Date
    Dec 2007
    Posts
    23
    million thanks for everyone.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need some help...
    By darkconvoy in forum C Programming
    Replies: 32
    Last Post: 04-29-2008, 03:33 PM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  3. can anyone see anything wrong with this code
    By occ0708 in forum C++ Programming
    Replies: 6
    Last Post: 12-07-2004, 12:47 PM
  4. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM