system command not executing with format specifier

This is a discussion on system command not executing with format specifier within the C Programming forums, part of the General Programming Boards category; Hello All, I need some help with using the system command in C. I am trying to use system command ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    4

    system command not executing with format specifier

    Hello All,

    I need some help with using the system command in C. I am trying to use system command to read the names text files from a particular directory on my PC and save it in another text file.

    My code is as follows:
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    int system(char*);
    
    int main()
    {
    	char dir_str[50] = "G:\\text";  // this is the directory that has my text files
     	char command1[100], command2[100];
    	char temp_file[100]= "G:\\temp.txt"; // this is the temp file to which i will save the names of all the text files that exist in directory  G:\text
    
    	sprintf(command1,"cd %s",dir_str); //command1 to be dos command to change directory
    	command1[strlen(command1)]='\0'; //introduced this to eliminate buffer overflow
            system(command1);   //doesn't seem to do anything
    
    	temp_file[strlen(temp_file)]='\0';  // to eliminate buffer overflow
    
    	sprintf(command2,"dir *.txt /b > %s",temp_file); //command to copy all .txt files from dir to temp_file ... I would assume dir to be G:\\text 'coz I called the system(command1) but this doesnt do anything
     
    	return 0;
    
    } 
    Someone kindly tell me what I am doing wrong. Interestingly, if I replaced sprintf(command2,"dir *.txt /b > %s",temp_file) with sprintf(command2,"dir *.txt /b > temp.txt") it will create a temp.txt file in the SAME folder from where I am running the program and save ONLY .txt files that are in the SAME folder from where I am running the program. In short, system command doesn't seem to like %s format specifier and string parameter.

    Thanks in advance for your help!

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Your system command only lasts for that single execution. It changes directories and then simply returns. The final line right before your return doesn't actually do anything practical, because all you're doing is stuffing a variable. You don't do anything with the variable, you just return.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    4
    Hey,

    Thanks a bunch for the reply. So the system command lasts only for that single execution? in that case, if I want my program to actually change directory mid way through the program, copy the names of the .txt files from the new directory into a text file, and then return back to the directory where I am executing the program, how do I go about it?

    thanks,

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    sprintf( buf, "dir %s >& %s", fromhere, tohere );
    Do it all in one shot.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    . . . and use fully qualified file names.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    Or you could try using the proper API for your system to read filenames and directories (there's examples in "da FAQ").

    It's much better than trying to write what is effectively a batch file in C.


    > temp_file[strlen(temp_file)]='\0'; // to eliminate buffer overflow
    This actually does bugger all.

    The strlen gives you the index into the array where the \0 is stored, so all you're doing is replacing one '\0' with another '\0' (in other words, nothing changes)
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Use "%.XXs" instead of "%s" with XX as a suitable number such that buffer overflow is impossible. You may, however, inadvertently truncate long file names. You should use system-specific APIs and the API-provided MAX_PATHNAME_LENGTH or similiar value instead.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 09:13 AM
  2. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM
  3. Replies: 1
    Last Post: 03-11-2003, 04:36 PM
  4. Executing system command
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 04-04-2002, 04:13 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21