1 / 9

System IO

System IO. CAS CS210 Ying Ye. Boston University. Files. In Linux, all I/O devices are modeled as files All input and output is performed by reading and writing the appropriate files When a file is opened, system returns a file descriptor , which is used to access the file. Standard I/O.

phil
Télécharger la présentation

System IO

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. System IO CAS CS210 Ying Ye Boston University

  2. Files • In Linux, all I/O devices are modeled as files • All input and output is performed by reading and writing the appropriate files • When a file is opened, system returns a file descriptor, which is used to access the file

  3. Standard I/O • getchar(), putchar(), scanf(), printf()...... • When each process is created, three files are opened: • standard input • standard output • standard error

  4. Standard I/O • int getchar(void): read a character from standard input stream, return it as int • int putchar(int c): write the character c to standard output stream • Usage: char a = getchar(); putchar(a); • Practice: http://cs-people.bu.edu/yingy/input.c http://cs-people.bu.edu/yingy/output.c

  5. Standard I/O input.c: char string[6]; int i; for(i = 0; i < 5; i++) string[i] = getchar(); string[i] = '\0' printf(string); output.c: int len = 0; while(string[len] != '\0'){ len++; } int i; for(i = 0; i < len; i++) putchar(string[i]);

  6. File I/O • File object in Standard C library: FILE it contains the current position indicator in the file • Open file: FILE *fopen(char *name, char *mode); mode: "r": read only "r+": read and write e.g. FILE *fp = fopen("file_name", "r+");

  7. File I/O • Read: int getc(FILE *fp); returns a character from the current position in file, current position moves one byte forward e.g. char c = getc(fp); • Write: int putc(int c, FILE *fp); writes a character c to file fp, current position moves one byte forward e.g. char c = 'a'; putc(c, fp);

  8. File I/O • Close file: int fclose(FILE *fp); • Download: http://cs-people.bu.edu/yingy/file.c • First, create an empty file: vim testfile, then save and quit it • Open testfile after you run your program

  9. File I/O fp = fopen("testfile", "r+"); fprintf(fp, string); //doesn't put terminator to testfile buffer = (char *)malloc(29 * sizeof(char)); fseek(fp, 0, SEEK_SET); int i = 0; while(i < 28) buffer[i++] = getc(fp); buffer[i] = '\0'; fclose(fp);

More Related