Trying to create a C program that can execute bash commands, and I'm trying to do this by reading commands from a text file. The problem I'm having is getchar is perfect for reading single characters like 1, 2, 3 etc but I'm struggling to have a string that's more than one character.
My code:
int main(int argc, char *argv[]) {
// the only variables that are used
char choice;
char str[50], dirName;
int i;
int finished = 1;
while(finished) {
printChoice(); // only prints to terminal [1] to cd [2] to exit prog
choice = getchar();
dirName = getchar();
switch(choice){
case ('1'):
while(dirName !='
') { // im sure i've placed this while loop in the wrong place but I dont know any better
str[i] = dirName;
i++;
dirName = getchar();
}
printf("Enter directory you'd like to move into: ");
changeDir(str); // function to cd
break;
case ('2'): // exit program
printf("Closing Program...
");
finished=0;
break;
default: // incorrect user choice
printf("Invalid choice
");
break;
}
}
return 0;}
The text file input I'm trying to read (if I just run the program with 1 2, the program works as intended):
1 Documents 2
Desired output:
cd Documents // once finished
exit program
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…