J'essaie de transmettre une chaîne à chdir(). Mais j'ai toujours l'impression que certains éléments traînants font chdir() échouer.analyse de chaîne en C
#define IN_LEN 128
int main(int argc, char** argv) {
int counter;
char command[IN_LEN];
char** tokens = (char**) malloc(sizeof(char)*IN_LEN);
size_t path_len; char path[IN_LEN];
...
fgets(command, IN_LEN, stdin)
counter = 0;
tmp = strtok(command, delim);
while(tmp != NULL) {
*(tokens+counter) = tmp;
tmp = strtok(NULL, delim);
counter++;
}
if(strncmp(*tokens, cd_command, strlen(cd_command)) == 0) {
path_len = strlen(*(tokens+1));
strncpy(path, *(tokens+1), path_len-1);
// this is where I try to remove the trailing junk...
// but it doesn't work on a second system
if(chdir(path) < 0) {
error_string = strerror(errno);
fprintf(stderr, "path: %s\n%s\n", path, error_string);
}
// just to check if the chdir worked
char buffer[1000];
printf("%s\n", getcwd(buffer, 1000));
}
return 0;
}
Il doit y avoir une meilleure façon de procéder. Quelqu'un peut-il aider? J'ai essayé d'utiliser scanf mais quand le programme appelle scanf, il se bloque.
Merci
ah oui. Quelle erreur stupide. Merci beaucoup aussi. Je devenais très frustré. – devin
Si j'avais un nickel pour chaque fois que j'ai fait une erreur de programmation stupide ... –