CS240: Programming in C

Similar documents
CS240: Programming in C

Systems Programming. 08. Standard I/O Library. Alexander Holupirek

Naked C Lecture 6. File Operations and System Calls

System Software Experiment 1 Lecture 7

Mode Meaning r Opens the file for reading. If the file doesn't exist, fopen() returns NULL.

PROGRAMMAZIONE I A.A. 2017/2018

CSci 4061 Introduction to Operating Systems. Input/Output: High-level

File I/O. Preprocessor Macros

Content. Input Output Devices File access Function of File I/O Redirection Command-line arguments

UNIX System Programming

Standard C Library Functions

CS246 Spring14 Programming Paradigm Files, Pipes and Redirection

Input/Output and the Operating Systems

CSE 410: Systems Programming

25.2 Opening and Closing a File

Accessing Files in C. Professor Hugh C. Lauer CS-2303, System Programming Concepts

CSI 402 Lecture 2 Working with Files (Text and Binary)

Files and Streams Opening and Closing a File Reading/Writing Text Reading/Writing Raw Data Random Access Files. C File Processing CS 2060

Standard I/O in C, Computer System and programming in C

UNIT IV-2. The I/O library functions can be classified into two broad categories:

C-Refresher: Session 10 Disk IO

Input / Output Functions

Intermediate Programming, Spring 2017*

File and Console I/O. CS449 Spring 2016

File Access. FILE * fopen(const char *name, const char * mode);

CMPE-013/L. File I/O. File Processing. Gabriel Hugh Elkaim Winter File Processing. Files and Streams. Outline.

Quick review of previous lecture Ch6 Structure Ch7 I/O. EECS2031 Software Tools. C - Structures, Unions, Enums & Typedef (K&R Ch.

System Programming. Standard Input/Output Library (Cont d)

EECS2031. Modifiers. Data Types. Lecture 2 Data types. signed (unsigned) int long int long long int int may be omitted sizeof()

File (1A) Young Won Lim 11/25/16

Standard File Pointers

File IO and command line input CSE 2451

fopen() fclose() fgetc() fputc() fread() fwrite()

Goals of this Lecture

ENG120. Misc. Topics

2009 S2 COMP File Operations

I/O Management! Goals of this Lecture!

I/O Management! Goals of this Lecture!

CSE2301. Introduction. Streams and Files. File Access Random Numbers Testing and Debugging. In this part, we introduce

Data File and File Handling

M.CS201 Programming language

Chapter 5, Standard I/O. Not UNIX... C standard (library) Why? UNIX programmed in C stdio is very UNIX based

C PROGRAMMING. Characters and Strings File Processing Exercise

Lecture 7: Files. opening/closing files reading/writing strings reading/writing numbers (conversion to ASCII) command line arguments

Basic I/O. COSC Software Tools. Streams. Standard I/O. Standard I/O. Formatted Output

File and Console I/O. CS449 Fall 2017

Introduction to file management

File Handling. Reference:

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 12

CSC209H Lecture 3. Dan Zingaro. January 21, 2015

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

What Is Operating System? Operating Systems, System Calls, and Buffered I/O. Academic Computers in 1983 and Operating System

Computer programming

C Input/Output. Before we discuss I/O in C, let's review how C++ I/O works. int i; double x;

UNIT-V CONSOLE I/O. This section examines in detail the console I/O functions.

C File Processing: One-Page Summary

Darshan Institute of Engineering & Technology for Diploma Studies Unit 6

CSI 402 Systems Programming LECTURE 4 FILES AND FILE OPERATIONS

Chapter 12. Files (reference: Deitel s chap 11) chap8

Ch 11. C File Processing (review)

Lecture 9: File Processing. Quazi Rahman

CS113: Lecture 7. Topics: The C Preprocessor. I/O, Streams, Files

Lecture 8. Dr M Kasim A Jalil. Faculty of Mechanical Engineering UTM (source: Deitel Associates & Pearson)

File Processing. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

CSI 402 Lecture 2 (More on Files) 2 1 / 20

File I/O. Dong-kun Shin Embedded Software Laboratory Sungkyunkwan University Embedded Software Lab.

Topic 8: I/O. Reading: Chapter 7 in Kernighan & Ritchie more details in Appendix B (optional) even more details in GNU C Library manual (optional)

EM108 Software Development for Engineers

Slide Set 8. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng

structs as arguments

Chapter 11 File Processing

C Basics And Concepts Input And Output

File I/O. Arash Rafiey. November 7, 2017

C mini reference. 5 Binary numbers 12

CS 261 Fall Mike Lam, Professor. Structs and I/O

Introduction to Computer and Program Design. Lesson 6. File I/O. James C.C. Cheng Department of Computer Science National Chiao Tung University

Unit 6 Files. putchar(ch); ch = getc (fp); //Reads single character from file and advances position to next character

C for Engineers and Scientists: An Interpretive Approach. Chapter 14: File Processing

#define PERLIO_NOT_STDIO 0 /* For co-existence with stdio only */ #include <perlio.h> /* Usually via #include <perl.h> */

File I/O. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Physical Files and Logical Files. Opening Files. Chap 2. Fundamental File Processing Operations. File Structures. Physical file.

Files. Programs and data are stored on disk in structures called files Examples. a.out binary file lab1.c - text file term-paper.

Main Program Revisited. Reading Assignment. K.N. King Chapter 3, 22. K.N. King Sections Example of Argument Processing

Course organization. Course introduction ( Week 1)

All the scoring jobs will be done by script

IO = Input & Output 2

UNIX input and output

Computer System and programming in C

Outline. OS Interface to Devices. System Input/Output. CSCI 4061 Introduction to Operating Systems. System I/O and Files. Instructor: Abhishek Chandra

Chapter 12. Text and Binary File Processing. Instructor: Öğr. Gör. Okan Vardarlı. Copyright 2004 Pearson Addison-Wesley. All rights reserved.

Computer Programming Unit v

Contents. Programming Assignment 0 review & NOTICE. File IO & File IO exercise. What will be next project?

Lecture6 File Processing

Lecture 8: Structs & File I/O

UNIT- 2. Binary File

All the scoring jobs will be done by script

CSC 270 Survey of Programming Languages. Input and Output

Programming in C. Session 8. Seema Sirpal Delhi University Computer Centre

C Programming Language

Programming in C Lecture Tiina Niklander

Transcription:

CS240: Programming in C Lecture 13 si 14: Unix interface for working with files. Cristina Nita-Rotaru Lecture 13/Fall 2013 1

Working with Files (I/O) File system: specifies how the information is organized on the disk and can be accessed Directories Files In UNIX the following are files Peripheral devices (keyboard, screen, etc) Pipes (inter process communication) Sockets (communication via computer networks) Files representation Text files (human readable format) Binaries (for example executables files) Cristina Nita-Rotaru Lecture 13/Fall 2013 2

Functions and System Calls System calls: services provided by the operating system. C Library provides support such that a user can invoke system calls through C functions. Example: I/O operations (I/O access is slower than memory access) Memory allocation Cristina Nita-Rotaru Lecture 13/Fall 2013 3

File Descriptors In order to perform operations on a file, the filed must be opened. Any opened file has associated a non-negative integer called file descriptor. For each program the operating system opens implicitly three files: standard input, standard output and standard error, that have associated the file descriptors 0, 1, 2 in this order. Cristina Nita-Rotaru Lecture 13/Fall 2013 4

C Library I/O Interface The C Library provides two mechanisms for working with files: file descriptors: objects of type int int fd; streams: FILE * objects. FILE * fp; Cristina Nita-Rotaru Lecture 13/Fall 2013 5

File Descriptors Primitive, low-level interface to input and output operations. Must be used for control operations that are specific to a particular kind of device. Cristina Nita-Rotaru Lecture 13/Fall 2013 6

Streams Higher-level interface, layered on top of the primitive file descriptor facilities. More powerful set of functions for performing actual input and output operations than the corresponding facilities for file descriptors. It is implemented in terms of file descriptors the file descriptor can be extracted from a stream and then perform low-level operations directly on the file descriptor a file can be open as a file descriptor and then make a stream associated with that file descriptor. Cristina Nita-Rotaru Lecture 13/Fall 2013 7

Standard Input, Output and Error For every program the operating system opens three files and provides pointers to them: Standard input: stdin Standard output: stdout Standard error: stderr Normally, stdin is connected to the keyboard and stdout to the screen, but they may be redirected to pipes or files. #include <stdio.h> Cristina Nita-Rotaru Lecture 13/Fall 2013 8

Formatted Output printf(char *format, arg1, arg2, ); sprintf(char *string, char *format, arg1, arg2, ); fprintf(file *fp, char *format, arg1, arg2, ); Examples: int int_val; char ch_val[10]; char buffer[256]; FILE *f; printf( %d, %s, %p\n, int_val, ch_val, &int_val); fprintf(stderr, %d, %s\n, int_val, ch_val); fprintf(f, %d, %s\n, int_val, ch_val); sprintf(buffer, %d, %s\n, int_val, ch_val); Cristina Nita-Rotaru Lecture 13/Fall 2013 9

Formatted Input scanf(char *format, arg1, arg2 ); sscanf(char *string, char *format, arg1, arg2, ); Examples double sum, v; char ch[10]; sum = 0; scanf( %s, ch); while(scanf( %lf, &v) == 1) { printf( \t%.2f\n, sum += v ); } The arguments to scanf and sscanf MUST be pointers! Cristina Nita-Rotaru Lecture 13/Fall 2013 10

Printing Error Messages Standard output stream (stdout) can be redirected and in that case the error messages will not be printed on the screen Solution: print to standard error file stream, stderr. Output written to stderr will appear on the screen even if stdout is redirected. fprintf(stderr, Error in function %s\n, func_name); Cristina Nita-Rotaru Lecture 13/Fall 2013 11

Example: output redirection #include <stdio.h> int main() { fprintf(stdout, "Printing to stdout with fprintf\n"); printf("printing to stdout with printf\n"); fprintf(stderr, "Printing to stderr.\n"); } return 0;./test./test > a.txt Cristina Nita-Rotaru Lecture 13/Fall 2013 12

Example: input redirection #include <stdio.h> int main() { int c; } while((c=getchar())!= EOF) { putchar(c); } return 0; gcc -std=c99 stdin_test.c -o stdin_test./stdin_test < a.txt Cristina Nita-Rotaru Lecture 13/Fall 2013 13

The Stream Interface Open #include <stdio.h> FILE *fopen (const char *path, const char* mode); Opens the file path. Mode specifies if the file is opened for write, read or both, if is opened as a text or as a binary file. Returns NULL if it fails and errno is set to indicate the error. Cristina Nita-Rotaru Lecture 13/Fall 2013 14

The Stream Interface Open - Mode r Open text file for reading. The stream is positioned at the beginning of the file. r+ Open for reading and writing. The stream is positioned at the beginning of the file. w Truncate file to zero length or create text file for writing. The stream is positioned at the beginning of the file. w+ Open for reading and writing. The file is created if it does not exist, otherwise it is truncated. The stream is positioned at the beginning of the file. a Open for appending (writing at end of file). The file is created if it does not exist. The stream is positioned at the end of the file. a+ Open for reading and appending (writing at end of file). The file is created if it does not exist. The initial file position for reading is at the beginning of the file, but output is always appended to the end of the file. Cristina Nita-Rotaru Lecture 13/Fall 2013 15

The Stream Interface Close #include <stdio.h> int fclose (FILE *stream); Closes the file stream. Returns 0 on suceess, otherwise, EOF is returned and errno is set to indicate the error Further access on the file results in undefined behavior. int fileno (FILE *stream); Returns the file descriptor of the file stream. Cristina Nita-Rotaru Lecture 13/Fall 2013 16

Open/Close Streams Examples FILE *f1, *f2; int fd; /* open binary file for reading only. The stream is positioned at the beginning of the file. */ f1 = fopen( myfile, rb ); fd = fileno(f1); fprintf(stderr, file descriptor: %d\n, fd); fclose(f1); /* open text file for reading and writing. The file is created if it does not exist, otherwise it is truncated. The stream is positioned at the beginning of the file. */ f2 = fopen( otherfile, w+ ); fclose(f2); Cristina Nita-Rotaru Lecture 13/Fall 2013 17

Stream Interface Positioning #include <stdio.h> int feof (FILE *stream); Tests the end-of-file indicator for the stream pointed to by stream, returns non-zero if it is set. long int ftell (FILE *stream); Returns the current value of the file position indicator for the stream pointed to by stream. void rewind (FILE *stream); Sets the file position indicator for the file pointed to by stream to the beginning of the file. Cristina Nita-Rotaru Lecture 13/Fall 2013 18

Stream Interface Positioning #include <stdio.h> int fseek (FILE *stream, long int offset, int whence); Sets the file position indicator for the stream pointed to by stream. New position is measured in bytes. New position = offset + whence If whence is set to SEEK_SET, SEEK_CUR, or SEEK_END, the offset is relative to the start of the file, the current position indicator, or end-of-file, respectively. Cristina Nita-Rotaru Lecture 13/Fall 2013 19

Positioning Streams - Example int main() { long fsize; FILE *f; f = fopen( myfile, r ); /* compute the size of the file */ fseek(f, 0, SEEK_END) ; fsize = ftell(f) ; fprintf(stderr, file size is: %d\n, fsize); } fclose(f); return 0; Cristina Nita-Rotaru Lecture 13/Fall 2013 20

Stream Interface - Errors #include<stdio.h> void clearerr (FILE *stream); Clears the end-of-file and error indicators for the stream pointed to by stream. int ferror (FILE *stream); Tests the error indicator for the stream pointed to by stream, returning non-zero if it is set. The error indicator can only be reset by the clearerr function. Cristina Nita-Rotaru Lecture 13/Fall 2013 21

Text Stream I/O Read #include <stdio.h> int fgetc (FILE *stream); Reads the next character from stream and returns it as an unsigned char cast to an int, or EOF on end of file or error. char *fgets (char *s, int size, FILE *stream); Reads in at most one less than size characters from stream and stores them into the buffer pointed to by s. Reading stops after an EOF or a newline. If a newline is read, it is stored into the buffer. A '\0' is stored after the last character in the buffer. Cristina Nita-Rotaru Lecture 13/Fall 2013 22

Text Stream I/O Read - Examples #include <stdio.h> int main() { FILE *f; int n; char buf[100]; f = fopen( myfile, r ); fgets(buf, 10, f); fprintf(stderr, read from file: %s\n, buf); fclose(f); } return 0; Cristina Nita-Rotaru Lecture 13/Fall 2013 23

Text Stream I/O Write #include <stdio.h> int fputc (int c, FILE *stream); Writes the character c, cast to an unsigned char, to stream. Return the character written as an unsigned char cast to an int or EOF on error. int fputs(const char *s, FILE *stream); Writes the string s to stream, without '\0'. Returns a non - negative number on success, or EOF on error. Cristina Nita-Rotaru Lecture 13/Fall 2013 24

Text Stream I/O Write - Example int main() { FILE *f; int n; f = fopen( myfile, w+ ); n = fputs( Let s just write something\n, f); fprintf(stderr, fputs returned: %d\n, n); fclose(f); } return 0; Cristina Nita-Rotaru Lecture 13/Fall 2013 25

Binary Stream I/O - Read #include <stdio.h> size_t fread (void *ptr, size_t size, size_t nmemb, FILE *stream); Reads nmemb elements of data, each size bytes long, from the stream pointed to by stream, storing them at the location given by ptr. Returns the number of items successfully read. If an error occurs, or the end-of-file is reached, the return value is a short item count (or zero). Does not distinguish between end-of-file and error, use feof and ferror to determine which occurred. Cristina Nita-Rotaru Lecture 13/Fall 2013 26

Binary Stream I/O Read - Example int main() { FILE *f; int n, v[3]; f = fopen( myfile, rb ); /* read 3 int from file f */ n = fread(v, sizeof(int), 3, f); fprintf(stderr, fread returned: %d\n, n); fclose(f); } return 0; Cristina Nita-Rotaru Lecture 13/Fall 2013 27

Binary Stream I/O - Write #include <stdio.h> size_t fwrite (const void *ptr, size_t size, size_t nmemb, FILE *stream); Writes nmemb elements of data, each size bytes long, to the stream pointed to by stream, obtaining them from the location given by ptr. Returns the number of items successfully written. If an error occurs, or the end-of-file is reached, the return value is a short item count (or zero). Cristina Nita-Rotaru Lecture 13/Fall 2013 28

Binary Stream I/O Write - Example int main() { FILE *f; int n, v[3]={1, 2, 3}; f = fopen( myfile, wb+ ); /* writes 3 ints in file f */ n = fwrite(v, sizeof(int), 3, f); fprintf(stderr, fwrite returned: %d\n, n); fclose(f); } return 0; Cristina Nita-Rotaru Lecture 13/Fall 2013 29

Example: error handling Upon successful completion fopen(), fdopen(), and freopen() return a FILE pointer. Otherwise, NULL is returned and the global variable errno is set to indicate the error. You can use perror to print the associated error message Cristina Nita-Rotaru Lecture 15/ Spring 2010 30

Readings and exercises for this lecture K&R Chapter 5.11, 6.8, 6.9 Code all the examples in the lecture. Cristina Nita-Rotaru Lecture 13/Fall 2013 31