# 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. ## 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.

2. 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.

3. 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. Code:
sprintf( buf, "dir %s >& %s", fromhere, tohere );
Do it all in one shot.

Quzah.

5. . . . and use fully qualified file names.

6. 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)

7. 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.