weeks 9 10 n.
Download
Skip this Video
Download Presentation
Weeks 9-10

Loading in 2 Seconds...

play fullscreen
1 / 9

Weeks 9-10 - PowerPoint PPT Presentation


  • 91 Views
  • Uploaded on

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

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Weeks 9-10' - fell


Download Now 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
ad