Weeks 9 10
This presentation is the property of its rightful owner.
Sponsored Links
1 / 9

Weeks 9-10 PowerPoint PPT Presentation


  • 59 Views
  • Uploaded on
  • Presentation posted in: General

Weeks 9-10. IO Standard IO (stdin, stdout) and Pipes Formatted IO File IO System Calls System interface to obtain services from the OS, include many subsystems Memory Scheduler File/Storage System Inter-process Communication and Network, etc A popular subsystem: File system

Download Presentation

Weeks 9-10

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Weeks 9 10

Weeks 9-10

  • IO

    • Standard IO (stdin, stdout) and Pipes

    • Formatted IO

    • File IO

  • System Calls

    • System interface to obtain services from the OS, include many subsystems

      • Memory

      • Scheduler

      • File/Storage System

      • Inter-process Communication and Network, etc

    • A popular subsystem: File system

      • File descriptors

      • Low level IO

      • File Management

      • Examples


Standard io and pipes

Standard IO and Pipes

  • stdin, stdout

    cmd < inputfile

    cmd > outputfile

  • Numbered file descriptors

    • 0: stdin

    • 1: stdout

    • 2: stderr …

  • Pipes

    cmd1| cmd2 | cmd3

  • IO Redirection

    cmd1 < inputfile > outputfile: specifies the input and output file

    cmd1 >> outputfile: append output to outputfile


Formatted io

Formatted IO

  • Output: printf

  • Input: scanf

  • Arguments: a format string, followed by the arguments

    printf (char *fmt, arg1, arg2, …)

    scanf (char *fmt, arg1, arg2, …)

  • Arguments for scanf have to be memory addresses


Formatting

Formatting

  • Output goes to stdout

  • Input comes from stdin

  • Format String: regular string + conversion specification

    • Start of a format specification: %

    • Width, precision, adjustment:

    • Between ‘%’ and the conversion character, in order

      • ‘-’: left adjustment

      • A number: minimum field width

      • ‘.’: separates the field width from the precision

      • A number: precision

      • l or h: long or short.

    • Go to web page http://www.cplusplus.com/reference/clibrary/cstdio/printf/ for more details


String based input

String-based input

  • String-based output

    • int sprintf ( char * str, const char * format, ... );

  • String-based input

    • int sscanf (char *line, char *fmt, arg1, arg2, …);

    • ex: char st[]=“3.21, 4.33”;

      • float x,y;

      • sscanf(st,”%f,%f”,&x,&y);//x=3.21, y=4.33

      • char newst[32];

      • sprintf(newst,”%f,%f”,y,x);//newst=“4.33,3.21”


Variable length arguments

Variable length arguments

  • Variable length arguments

    int printf(char *fmt, …);

  • <stdarg.h>

    • a new datatype: va_list

    • Associated macro functions:

      • va_start(ap, last)

        • Initialize ap to be the va_list after the argument: last.

      • va_arg(ap, int)

        • Expand ap to an expression that has type/value that match int

        • ap moves to the next argument in the variable list

      • va_end(ap)

        • Cleanup the variable argument list when done

    • Example: http://www.cprogramming.com/tutorial/c/lesson17.html


File accesses

File Accesses

  • Files and file descriptors:

    • FILE *fp;

    • FILE *fopen(char *name, char *mode);

      • Name can be a long path

      • Mode: a combination of ‘r’, ‘w’, ‘x’, or ‘a’

  • File input/output

    • int getc(FILE *fp)

    • int putc(int c, FILE *fp);

    • These works with default stdin/stdout:

      • getchar()

      • putchar()


Line oriented input output

Line-oriented input/output

  • int getline(char *line, size_t n, FILE *fp)

  • char *fgets(char *line, size_t n, FILE *fp);

    • gets(char *line, size_t n); /* buggy, never use it */

  • char *fputs(char *line, FILE *fp);


Error handling

Error handling

  • fprintf(stderr, char *fmt, arg1, arg2, …)

  • exit(1): exit with non-zero status

  • ferror(FILE *fp): test for any error on fp

  • feof(FILE *fp) : test for end-of-file

  • perror(char *s):

    • print the error message, s, to stderr for the last system or library calls


  • Login