Hello,
In my code if I use chdir (".."); then issue system ("dir"); it shows a list of the previous directory which is what I want.
However if I pass chdir my built string chdir (message) it will go to that and then go back to the original dir. How can I make it remain in that directory?
P.S. I know message is working because if I concatenate DIR to it and pass it to system (message) then it will list the contents of the directory from message.
Also directorylist.txt has path names in it C:\Program Files\hi after message is created it, messages value is C:\"Program files"\"hi"
Here is the code.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <dos.h>
#include <string.h>
int changedir();
int main(){
changedir();
return 0;
}
int changedir(){
char buffer[200] = ""; /* declare a char array */
char message[200] = "";
char field[200] = "";
char result[200] = "";
int n, first_run = 1;
FILE *file; /* declare a FILE pointer */
file = fopen("directorylist.txt", "r");
/* open a text file for reading */
if(file==NULL) {
printf("Error: can't open file.\n");
/* fclose(file); DON'T PASS A NULL POINTER TO fclose !! */
return 1;
}
else {
fgets(buffer, 200, file);
const char *ptr = buffer;
while ( sscanf(ptr, "%31[^\\]%n", field, &n) == 1 ){ //delimiters
ptr += n; /* advance the pointer by the number of characters read */
if ( *ptr != '\\' ){
break; /* didn't find an expected delimiter, done? */
}else{
if (first_run) //so message isn't C:"\" but instead C:\"
;
else
strcat(field, "\"");
strcat(message, field);
strcat(message, "\\\"");
}
++ptr; /* skip the delimiter */
first_run = 0;
}
//sprintf (result, "cd %s", message); // append CD to message
//printf ("%s", message);
//system (result);
chdir (message);
//system ("chdir c:\\");
system ("dir");
fclose(file);
return 0;
}
}