Unix Processes Standard Streams
A Unix process executes in this environment
• Stream is is a sequence of bytes • stdio.h define three streams • stdin standard input stream scanf, getchar read from stdin • stdout standard output stream printf, putchar write to stdout • stderr standard error stream by convention used for error messages
I/O direction Unix Pipes
• If program run from terminal: stdin, stdout, stderr connected to terminal. • Unix shells allow you to re-direct stdin, stdout and stderr. • Unix shells allow you to connect stdout of one program to • Run a.out with stdin coming from file data.txt stdin of another program $ ./a.out
$ ./a.out >output.txt • wc counts chars, words and lines in its stdin • Unix other useful programs (filters) designed to be run like wc • Run a.out with stdout appended to file output.txt E.g.: grep, cut, sort, uniq $ ./a.out >>output.txt • covered in detail in COMP2041
• Run a.out with stderr going to file errors.txt
$ ./a.out2>errors.txt Using stderr for error Messages Accessing Files
• FILE *f = fopen(char *filename, char *mode) Create a stream to read from or write to file • fprintf allows you to specify stream to print to returns NULL if open fails • For example: • int fclose(FILE *f) fprintf(stderr, "error: can not open %s\n", f); finish operations on a file fclose called automatically on program exit • printf actually just calls fprintf specifying stdout Beware: some output may be cached until fclose called fprintf(stdout, ...); • int fgetc(FILE *f) Read a single character from a stream • Best if error messages written to stderr, so users sees them returns EOF if no character available even if stdout is directed to a file. • int fputc(int, FILE *f) • Common for stderr to be redirected separately to a log file for Writes a single character to a stream system programs. • int fscanf(FILE *f, ...) scanf from stream; returns number of values read • int fprintf(FILE *f, ...) printf to specified stream
Opening a File fopen mode parameter
• "r"
I Opens an existing text file for reading purpose. FILE*fp= fopen("filename.txt", "w"); • "w"
I Opens a text file for writing. • - opens a file fopen I If it does not exist, then a new file is created. • parameter 1 - the name of the file to be opened I Starts writing from the beginning of the file. • parameter 2 - the mode in which to open the file • "a" I Opens a text file for writing in appending mode. • return value - a pointer to the file which has been opened. I If it does not exist, then a new file is created. This pointer is then used to reference the opened file for I Starts writing at the end of the existing file contents. operations such as reading, writing, and closing of the file. • "r+" "w+" "a+" Return value will be NULL if file can not be opened. I Opens a file for both reading and writing. I w+ truncates the file length to zero if it exists, while a+ starts reading from the start of the file and writing at the end of the existing file contents. Writing to a File Reading from a File
char line[MAX_LINE_LENGTH]; if (fgets(line, MAX_LINE_LENGTH, fp) != NULL){ printf("read line\n"); fputs("the text I want to put in the file\n", fp); } else{ printf("Could not read a line\n"); or } fprintf(fp, "the text I to put in the file\n"); or or int c; intc= ’!’; c= fgetc(fp); fputc(c, fp); if (c != EOF) { printf("read a ’%d’\n", c); } else{ printf("Could not read a character\n"); }