Question

I was wondering if someone could explain what the '<' and the '&>' mean in this statement here.

./pa1 < test.lig &> test.out

Here is the code for the frontend of the C program. This is an update to show how I am reading the command line arguments to supply to the program for the input output operations.

#include "tokens.h"
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>

extern FILE *yyin;
extern FILE *yyout;

int main(int argc, char** argv){
    if(argc == 3){
        printf("Input file = %s\n", argv[1]);
        yyin = fopen(argv[1], "r");
        yyout = fopen(argv[2], "w");
        if (yyin == 0 || yyout == 0) {
            printf("Error: %s\n", strerror(errno));
            yyin = stdin;
            exit(0);
        }else{
            fprintf(yyout, "File %s opened!\n", argv[1]);
        }
    }else{
        exit(1);
    }

    //Variables
    /*
        Integers
            1) tokenCount - Counts the number of tokens present in the file. 
            2) lexReturnToken - Checks to see if it is a valid token (not 999 according to tokens.h) 
                and then checks to see if it is a bracket, parentheses or curly so proper checks may be called.
     */
    int tokenCount = 0, lexReturnToken;

    //Standard Output Information
    while(1){
        lexReturnToken = yylex();
        if(lexReturnToken > 0){
            //Token Count
            if(lexReturnToken != 999){
                tokenCount++;
            }//else{

               // }
            //}
        }else{
            printf("Total tokens: %d\n", tokenCount);
            break;
        }
    }

        //Bool Main Detection (preceded by function)
        //Bool Bracket, Parentheses, Curly Match

    return 0;
}
Was it helpful?

Solution 2

The redirections are setting up stdin and stdout, but your program opens files named on the command line -- so it isn't using stdin and stdout.

When you run ./pa1 <test.lig &>test.out, the <test.lig and &>test.out redirections are parsed as commands to the shell -- so they aren't included in your argv, which only contains {"./pa1", NULL}.

If you want the redirections to work, then when argc shows that no arguments have been provided, ensure that you proceed with yyin as stdin and yyout as stdout, instead of calling exit(1) as you currently do.

OTHER TIPS

< is accepting input from a file, in this case sending anything in test.lig to pa1. As you might expect > is redirecting output, sending anything you'd normally see outputed from pa1 on the screen to the file test.out.

&> is sending both standard output AND errors to test.out, without it any errors would still show up in your terminal and not pollute the output file.

it means that the input of ./pa1 program is in the test.lig file and the output will be the test.out file

Take the contents of file test.lig and pass it as input to ./pa1, then push the output to test.out.

The &> tells it to pipe both stdout and stderr to the file instead of just stdout.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top