CS1061 C Programming Lecture 17: Steams and Character I/O

12
CS1061 C Programming Lecture 17: Steams and Character I/O A. O’Riordan, 2004

description

CS1061 C Programming Lecture 17: Steams and Character I/O. A. O’Riordan, 2004. Creating Streams. Streams are an abstraction used in C for input and output operations through devices like files, keyboard, printer, screen and I/O ports. - PowerPoint PPT Presentation

Transcript of CS1061 C Programming Lecture 17: Steams and Character I/O

Page 1: CS1061 C Programming Lecture 17: Steams and Character I/O

CS1061 C ProgrammingLecture 17: Steams and Character I/O

A. O’Riordan, 2004

Page 2: CS1061 C Programming Lecture 17: Steams and Character I/O

Creating Streams

Streams are an abstraction used in C for input and output operations through devices like files, keyboard, printer, screen and I/O ports.

A stream is represented by a pointer to a FILE structure that contains internal info about properties. Normally data contained in these structures are not referred to directly.

You can declare streams by creating a pointer to a FILE structure. There are three streams open by default when you include stdio.h, stdin, stdout, and strerr.

On my computer these are declared using FILE _iob[] (in stdio.h):#define stdin(&_iob[0])#define stdout(&_iob[1])#define stderr(&_iob[2])

Page 3: CS1061 C Programming Lecture 17: Steams and Character I/O

stdin, stdout, stderr

stdin - standard input stream.

By default stdin corresponds to the keyboard, but this can be redirected.

stdout - standard output stream.

By default stdout is directed to the screen, but the operating system can redirect it to a file or any other output device.

stderr - standard error stream.

This is an output stream specifically intendend to receive error messages. By default is directed to the standard output (like stdout), but it can be redirected to a log file or any other output device.

Page 4: CS1061 C Programming Lecture 17: Steams and Character I/O

getchar()

A very useful function for character input.

Prototype:

int getchar(void);

int getc(FILE *stream);

getchar() gets the next character from stdin. Returns the next character from the standard input (stdin).

Corresponds to: getc(stdin)

If the end of file is reached or there has been an error, the function returns EOF.

Page 5: CS1061 C Programming Lecture 17: Steams and Character I/O

getchar() example

/* getchar example : typewriter */#include <stdio.h>

int main(){

char c;printf("Enter line (dot '.' in sentence to exit)");do{

c = getchar();putchar(c);

}while (c != '.');return 0;}

Page 6: CS1061 C Programming Lecture 17: Steams and Character I/O

putchar()

A very useful function for writing a character.

Prototype:

int putchar(int character);

int putc(int character, FILE * stream);

putchar() writes character to stdout. Writes to the current position in the standard output (stdout) and increments the file pointer to point to next character.

This routine corresponds to: putc(character,stdout).

The value returned is the written character. If an error occurs, EOF is returned.

Page 7: CS1061 C Programming Lecture 17: Steams and Character I/O

putchar() example

/* putchar example: printing alphabet */#include <stdio.h>

int main(){char c;for (c = 'A' ; c <= 'Z' ; c++){

putchar(c);}return 0;}

This program writes ABCDEFGHIJKLMNOPQRSTUVWXYZ to the standard output (screen).

Page 8: CS1061 C Programming Lecture 17: Steams and Character I/O

ungetc()

Sometimes it is handy to put a character back on the stream. This gives you a read-ahead facility.

int ungetc(int character, FILE *stream);

A character is pushed onto an input stream and the file pointer is reset to that previous position. This character will be returned by the next call to getc().

char c;

c = getc(stdio);

ungetc(c, stdio); /* put it back again */

Page 9: CS1061 C Programming Lecture 17: Steams and Character I/O

system()

Prototype:

int system(const char * command);

Invokes command shell to execute a command. Once terminated, the interpreter gives back control to the program returning an int value.

Need to include stdlib.h.

If a command was successfully executed the command interpreter returns 0. A return value of -1 indicates an error.

Following is a program to display a Unix directory listing.

Page 10: CS1061 C Programming Lecture 17: Steams and Character I/O

system() example

#include <stdio.h>#include <stdlib.h>

int main(){int i;printf("Trying to execute ls (directory listing");i = system("ls");if (i == -1)

printf("Error executing ls\n");else

printf("Command successfully executed\n");return 0;}

Page 11: CS1061 C Programming Lecture 17: Steams and Character I/O

getenv()

You can also get system environment variables.

Prototype:

char *getenv(const char * varname);

Gets string from environment - a pointer to the null-terminated string containing the value of the environment variable varname.

If the requested variable is not defined in the environment the function returns a NULL pointer.

Depending on system this function may or not be case sensitive.

Page 12: CS1061 C Programming Lecture 17: Steams and Character I/O

getenv() example

/* getenv example: getting path */

#include <stdio.h>

#include <stdlib.h>

int main (){

char * buffer;

buffer = getenv("PATH");

if (buffer != NULL)

printf("Current path is: %s",buffer);

return 0;

}