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

Similar documents
Pointers (1A) Young Won Lim 11/1/17

Applications of Pointers (1A) Young Won Lim 12/26/17

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

25.2 Opening and Closing a File

Input / Output Functions

Pointers (1A) Young Won Lim 1/9/18

Pointers (1A) Young Won Lim 1/5/18

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

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

Computer programming

File IO and command line input CSE 2451

Overview (1A) Young Won Lim 9/14/17

Overview (1A) Young Won Lim 9/9/17

Pointers (1A) Young Won Lim 3/5/18

Overview (1A) Young Won Lim 9/25/17

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

File I/O. Preprocessor Macros

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

Standard File Pointers

Applications of Arrays (1A) Young Won Lim 2/11/17

Applications of Arrays (1A) Young Won Lim 3/15/17

C-Refresher: Session 10 Disk IO

Pointers (1A) Young Won Lim 1/14/18

Function Overview (1A) Young Won Lim 10/23/17

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

Day14 A. Young W. Lim Thr. Young W. Lim Day14 A Thr 1 / 14

Day14 A. Young W. Lim Tue. Young W. Lim Day14 A Tue 1 / 15

Pointers (1A) Young Won Lim 10/18/17

Standard C Library Functions

Introduction to file management

Applications of Structures (1A) Young Won Lim 12/8/17

UNIX System Programming

Input/Output and the Operating Systems

Applications of Pointers (1A) Young Won Lim 2/27/18

Pointers (1A) Young Won Lim 10/23/17

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

File Handling. Reference:

Applications of Pointers (1A) Young Won Lim 3/14/18

Applications of Pointers (1A) Young Won Lim 3/21/18

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

Applications of Structures (1A) Young Won Lim 12/4/17

Applications of Pointers (1A) Young Won Lim 1/5/18

CS240: Programming in C

Applications of Pointers (1A) Young Won Lim 4/24/18

Structure (1A) Young Won Lim 7/30/13

Pointers (1A) Young Won Lim 1/22/18

Applications of Pointers (1A) Young Won Lim 4/11/18

Pointers (1A) Young Won Lim 12/4/17

Applications of Pointers (1A) Young Won Lim 3/31/18

Computer System and programming in C

Arrays (1A) Young Won Lim 12/4/17

System Software Experiment 1 Lecture 7

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

Arrays and Strings (2H) Young Won Lim 3/7/18

Intermediate Programming, Spring 2017*

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

C Basics And Concepts Input And Output

Pointers (1A) Young Won Lim 2/6/18

Memory Arrays (4H) Gate Level Design. Young Won Lim 3/15/16

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

Number System (1A) Young Won Lim 7/7/10

EM108 Software Development for Engineers

CE Lecture 11

PROGRAMMAZIONE I A.A. 2017/2018

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

Pointers (1A) Young Won Lim 2/10/18

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

Computer Programming Unit v

Arrays (1A) Young Won Lim 1/27/17

ENG120. Misc. Topics

Fundamentals of Programming. Lecture 10 Hamed Rasifard

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

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

CSE 410: Systems Programming

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

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

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

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)

Structures (1A) Young Won Lim 12/4/17

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

CP2 Revision. theme: file access and unix programs

Structures (1A) Young Won Lim 11/8/16

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

C PROGRAMMING. Characters and Strings File Processing Exercise

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

Organization of a file

CSC209H Lecture 3. Dan Zingaro. January 21, 2015

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

Input/Output: Advanced Concepts

UNIT- 2. Binary File

Advanced C Programming Topics

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

Memory (1A) Young Won Lim 9/7/17

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

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

a = ^ ^ ^ ^ ^ ^ ^ b = c = a^b =

ELF (1A) Young Won Lim 10/22/14

M.CS201 Programming language

Variables (2D) Young Won Lim 3/28/18

CSI 402 Systems Programming LECTURE 4 FILES AND FILE OPERATIONS

Transcription:

File (1A)

Copyright (c) 2010-2016 Young W. Lim. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license is included in the section entitled "GNU Free Documentation License". Please send corrections (or suggestions) to youngwlim@hotmail.com. This document was produced by using OpenOffice.

FILE Pointer and Functions FILE *fp; fopen fprintf fscanf fclose opens a file prints formatted wide character to a file stream read formatted input from a file stream closes a file File Pointer 3

Text and Binary File Formatted Input / Output (Text Mode) int fprintf (FILE *stream, const char *format,...); int fscanf (FILE *stream, const char *format,...); Unformatted Input / Output (Text Mode) int fputc (int c, FILE *stream); int fgetc (FILE *stream); Binary Stream Input / Output (Binary Mode) size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream); size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream); File Pointer 4

Formatted Input / Output (Text Mode) int scanf ( const char *format,... ); int fscanf ( FILE *stream, const char *format,... ); int sscanf ( const char *buffer, const char *format,... ); int printf ( const char *format,... ); int fprintf ( FILE *stream, const char *format,... ); int sprintf ( char *buffer, const char *format,... ); File Pointer 5

Unformatted Input / Output (Text Mode) int fgetc (FILE *stream); char *fgets (char *s, int size, FILE *stream); int getc (FILE *stream); int getchar (void); char *gets (char *s); int ungetc (int c, FILE *stream); int fputc (int c, FILE *stream); int fputs (const char *s, FILE *stream); int putc (int c, FILE *stream); int putchar (int c); int puts (const char *s); File Pointer 6

Direct Input / Output size_t fread (void *buffer, size_t size, size_t count, FILE *stream); // where the read objects are stored // size of each object in bytes // the number of the objects // the stream to read size_t fwrite (const void *buffer, // where the objects are written size_t size, // size of each object in bytes size_t count, // the number of the objects FILE *stream); // the stream to read File Pointer 7

fopen() FILE *fopen(const char *path, const char *mode); opens a file and associates a stream with it The file name is the string pointed to by path mode points to a string consists of the following characters Returns a FILE pointer (successful) Returns NULL pointer and set errno (unsuccessful) File Pointer 8

Mode r for reading fpos_t : beginning r+ for reading and writing fpos_t : beginning w for writing fpos_t : beginning w+ for reading and writing fpos_t : beginning a for appending fpos_t : end a+ for reading and appending fpos_t : end(append), begining(read) File Pointer 9

File Positioning long ftell(file *stream); returns the current file position indicator int fgetpos(file *stream, fpos_t *pos); gets the file position indicator int fseek(file *stream, long offset, int whence); moves the file position indicator to a specific location in a file int fsetpos(file *stream, const fpos_t *pos); moves the file position indicator to a specific location in a file void rewind(file *stream); removes the file position indicator to the beginning in a file File Pointer 10

Error Handling void clearerr(file *stream); clears the end-of-file and error indicators int feof(file *stream); tests the end-of-file indicator returning nonzero if it is set. int ferror(file *stream); tests the end-of-file indicator returning nonzero if it is set. File Pointer 11

FILE Structure FILE : known as a file handle an opaque type containing the information about a file or text stream needed to perform input or output operations on it, an opaque pointer is a special case of an opaque data type, a datatype declared to be a pointer to a record or data structure of some unspecified type. File Pointer 12

FILE Structure containing the information about a file or text stream platform-specific identifier of the associated I/O device, such as a file descriptor the buffer stream orientation indicator (unset, narrow, or wide) stream buffering state indicator (unbuffered, line buffered, fully buffered) I/O mode indicator (input stream, output stream, or update stream) binary/text mode indicator end-of-file indicator error indicator the current stream position and multibyte conversion state (an object of type fpos_t) reentrant lock (required as of C11) File Pointer 13

FILE Structure fpos_t a non-array type capable of uniquely identifying the position of every byte in a file and every conversion state that can occur in all supported multibyte character encodings size_t an unsigned integer type which is the type of the result of the sizeof operator. File Pointer 14

References [1] Essential C, Nick Parlante [2] Efficient C Programming, Mark A. Weiss [3] C A Reference Manual, Samuel P. Harbison & Guy L. Steele Jr. [4] C Language Express, I. K. Chun