Pomegranate Society
Pomegranate Society

Reputation: 127

How to get commands from file redirection?

I am trying to get information from an input file which contains commands that need to be passed to my program. When I execute the C file, I use ./inventory test01/inventory01-actual.txt < test01/input01.txt > test01/actual01.txt. The file that contains the commands is input-01.txt. For instance, input-01.txt has the following contents:

PRINT
QUIT

As shown in my code below, the first while loop goes through the file test01/inventory01-actual.txt and parses through the input. When I call scanf, it only reads in the first command (PRINT) and then program terminates. I understand that I need a while loop to make it go through and read each command in the input file, but I am not sure how to reference this in my code.

I have thought of perhaps

while (______ ! EOF) {
.... 
}

...but I am not sure what to put in the blank in order to reference input-01.txt. Would I just use something like feof? (I would put the scanf and if-else statements inside this while loop, of course).

FILE *src_file;

src_file = fopen(argv[1], "r");
//Initialize data for node
int id; 
char name[MAX_NAME];
char summary[MAX_SUM];
int count;

char buffer[MAX_LEN_COMMAND];

//Parse file input line by line
while (fgets(buffer, sizeof(buffer), src_file) != NULL) {
    if (sscanf(buffer, "%d, %[^,], %[^,], %d\n", &id, name, summary, &count) == INPUT_COUNT) {
        if (count < 0) {        
            printf("Invalid count value.");         
            exit(EXIT_BAD_INPUT);    
        }
        if (isEmpty(summary) || isEmpty(name)) {
            //Skip this iteration
            printf("RECORD NOT INSERTED\n");
            continue;
        }
        printf("RECORD INSERTED: %d\n", id);
        //Add each struct to the linked list      
        addRecord(list, id, name, summary, count); 
    } else {
        printf("RECORD NOT INSERTED\n");  
    }
}

//Get user input for commands    
char command[MAX_LEN_COMMAND];

//Keep re-prompting user for commands until you reach EOF  
printf("====================\nCommand? ");    
scanf("%s", command);    
if (strcmp(command, "PRINT") == 0) {     
    print(list);     
} else if (strcmp(command, "QUIT") == 0) {
    quit(argv[1], list);
    exit(EXIT_SUCCESS);   
} else {
    printf("Invalid command passed.\n"); 
    exit(EXIT_BAD_INPUT);
}

My goal is get my program to read each command in the input-01.txt file, while my program currently only reads the first line of this file.

Upvotes: 0

Views: 59

Answers (1)

sudoer123
sudoer123

Reputation: 89

So the problem right now is that when you call scanf("%s", ...). With one %s as a format specifier, scanf() will read the first string until a white space is found including a new line. If you were to have scanf("%s %s", command1, command2) you would get the desired result.

However, you probably want your code to be more scalable with respect to the number of commands in your input file. In that case, I would suggest using fgets().

  //Get user input for commands    
  char command[MAX_LEN_COMMAND];
  while(fgets(command, sizeof(command), stdin) != NULL)
  {
    /* Do whatever you have to do with command */
  }

Also be careful, you are directly comparing command to some strings. Make sure your input file does not have any trailing whitespaces at every line.

Upvotes: 2

Related Questions