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

Similar documents
Input / Output Functions

File IO and command line input CSE 2451

Standard File Pointers

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

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

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

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

System Software Experiment 1 Lecture 7

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

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

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

7/21/ FILE INPUT / OUTPUT. Dong-Chul Kim BioMeCIS UTA

Introduction to file management

25.2 Opening and Closing a File

EM108 Software Development for Engineers

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

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

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

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

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)

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #47. File Handling

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

C Basics And Concepts Input And Output

Course organization. Course introduction ( Week 1)

Darshan Institute of Engineering & Technology for Diploma Studies Unit 6

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

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

C-Refresher: Session 10 Disk IO

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

File I/O. Preprocessor Macros

Input/Output and the Operating Systems

PROGRAMMAZIONE I A.A. 2017/2018

CS246 Spring14 Programming Paradigm Files, Pipes and Redirection

Standard C Library Functions

Computer Programming Unit v

Fundamentals of Programming. Lecture 15: C File Processing

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

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

CS240: Programming in C

UNIX System Programming

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

M.CS201 Programming language

C programming basics T3-1 -

2009 S2 COMP File Operations

Goals of this Lecture

Data File and File Handling

I/O Management! Goals of this Lecture!

I/O Management! Goals of this Lecture!

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

C File Processing: One-Page Summary

C Programming 1. File Access. Goutam Biswas. Lect 29

ENG120. Misc. Topics

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

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

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

Programming & Data Structure

Programming Fundamentals

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

CE Lecture 11

Organization of a file

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

DS: CS Computer Sc & Engg: IIT Kharagpur 1. File Access. Goutam Biswas. ect 29

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

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

Simple Output and Input. see chapter 7

Basic and Practice in Programming Lab 10

File System User API

Ch 11. C File Processing (review)

HIGH LEVEL FILE PROCESSING

Advanced C Programming Topics

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

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

Naked C Lecture 6. File Operations and System Calls

C mini reference. 5 Binary numbers 12

Chapter 11 File Processing

Lecture 12 CSE July Today we ll cover the things that you still don t know that you need to know in order to do the assignment.

Text Output and Input; Redirection

UNIX Shell. The shell sits between you and the operating system, acting as a command interpreter

Computer programming

Lecture 9: File Processing. Quazi Rahman

8. Characters, Strings and Files

The Design of C: A Rational Reconstruction (cont.)

CP2 Revision. theme: file access and unix programs

Contents. A Review of C language. Visual C Visual C++ 6.0

Goals of C "" The Goals of C (cont.) "" Goals of this Lecture"" The Design of C: A Rational Reconstruction"

LANGUAGE OF THE C. C: Part 6. Listing 1 1 #include <stdio.h> 2 3 int main(int argc, char *argv[]) PROGRAMMING

Lecture6 File Processing

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

Input/Output: Advanced Concepts

Computer Programming: Skills & Concepts (CP) Files in C

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

Stream Model of I/O. Basic I/O in C

The Design of C: A Rational Reconstruction (cont.)" Jennifer Rexford!

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

DATA STRUCTURES USING C

Computer Programming: Skills & Concepts (CP1) Files in C. 18th November, 2010

C Programming Language

Pointers and File Handling

Laboratory: USING FILES I. THEORETICAL ASPECTS

File Handling in C. EECS 2031 Fall October 27, 2014

Transcription:

File IO part 2

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

Overview File Pointer (FILE *) Standard: stdin, stdout, stderr Or fopen Usage: FILE* file=fopen(filename, modes); modes: r, w, or a : read, write, or append char ch=fgetc(file); fclose(file);

File Pointers Each stream in C is manipulated with the file pointer type FILE *stream FILE is a struct type containing multiple parts file for stream, current element in file, etc. FILE * is the address where the FILE type is located in memory FILEs always manipulated as FILE *

Standard File Pointers <stdio.h> contains three standard file pointers that are created for you (each of type FILE *) stdin - file pointer connected to the keyboard stdout - file pointer connected to the output window/terminal stderr - file pointer connected to the error window (generally the output window)/terminal

Text Files and Binary Files All files are coded as long sequences of bits (0s and 1s) Some files are coded as sequences of ASCII character values (referred to as text files) files are organized as bytes, with each byte being an ASCII character UTF-8 for unicode texts Other files are generally referred to as binary files

Memory Text file Binary file

Structure of Files String of bits: 010000110110000101110100 Interpreted as ASCII numbers: 01000011 01100001 01110100 67 97 116 Files as ASCII: 67 97 116 115 32 97 110 100 10 68 111 103 115 10 0 As characters: Cats and\ndogs\n<eof> In editor: Cats and Dogs

Structure of Text Files (cont) Two special characters \n - end-of-line character <EOF> - end-of-file marker File lab.data: 723 85 93 99 131 78 91 85 458 82 75 86 as a string of characters 723 85 93 99\n131 78 91 85\n458 82 75 86\n<EOF>

Windows and old mac text files are different! fopen(filename, rb ) - MS-DOS \r\n - Mac (prior to Mac OS X) \r - Unix/linux \n fopen(filename, r )

IO functions Text Files File input fscanf(file pointer, format string, address list) single character getchar, getc, fgetc ungetc File output fprintf(file pointer, format string, value list) single character putchar, putc, fputc

The ungetc function int ungetc(int c, FILE *fp) pushes the charater specified by c back onto the input stream (only one pushback is guaranteed at a time)

The rewind and gets function void rewind(file * fp) Sets the file-position pointer to the start of the file char * gets ( char * str ); Reads characters from the standard input (stdin) and stores them into str until a newline character or the end-of-file is reached. On success, the function returns str. If the end-of-file is encountered while attempting to read a character, the eof indicator is set (feof). If this happens before any characters could be read, the pointer returned is a null pointer (and the contents of str remain unchanged). If a read error occurs, the error indicator (ferror) is set and a null pointer is also returned (but the contents pointed by str may have changed).

The fopen function Syntax: fopen( FileName, mode ); File Name is an appropriate name for a file on the computer you are working on, example: C:\My Files\lab.dat Mode indicates the type of stream: r - file is opened for reading characters w - file is opened for writing characters (existing file deleted) a - file opened for writing characters (appended to the end of the existing file)

The fopen function (cont) fopen returns a value of type FILE * that is a stream connected to the specified file if the fopen command fails, a special value, NULL is returned reasons for failure: file doesn t exist (read) can t create file (append)

The fprintf function Syntax: fprintf( filep, Format, ValueList); Works similarly to printf, but data sent to file rather than screen printf( Format,ValueList) is a shorthand for fprintf(stdout, Format,ValueList) fprintf returns the number of characters printed or EOF (-1) if an error occurs File pointer should be write/append stream

The fscanf function Syntax: fscanf( filep, Format, AddrList); Works similarly to scanf, but data received from file rather than keyboard scanf( Format,AddrList) is a shorthand for fscanf(stdin, Format,AddrList) fscanf returns the number of successful data conversions or EOF if end-of-file reached File pointer should be a read stream

A sample run Add a word $./addaword Enter words to add to the file; press the Enter key at the beginning of a line to terminate. The fabulous programmer[enter] [enter] File contents: The fabulous programmer $

A sample run Add a word $./addaword Enter words to add to the file; press the Enter key at the beginning of a line to terminate. The fabulous programmer[enter] [enter] File contents: The fabulous programmer $./addaword Enter words to add to the file; press the Enter key at the beginning of a line to terminate. enchanted the[enter] large[enter] [enter] File contents: The fabulous programmer enchanted the large

?

Append mode By using the "a+" mode, the program can both read and write in the file The first time the program is used, it creates the wordy file When you use the program subsequently, it enables you to add (append) words to the previous contents. The append mode a only enables you to add material to the end of the file But the "a+" mode does enable you to read the whole file.

The fgets function char * fgets ( char * str, int num, FILE * stream ); Reads characters from stream and stores them as a C string into str until (num-1) characters have been read or either a newline or the end-of-file is reached, whichever happens first. A newline character makes fgets stop reading, but it is considered a valid character by the function and included in the string copied to str. On success, the function returns str. If the end-of-file is encountered while attempting to read a character, the eof indicator is set (feof). If this happens before any characters could be read, the pointer returned is a null pointer (and the contents of str remain unchanged). If a read error occurs, the error indicator (ferror) is set and a null pointer is also returned (but the contents pointed by str may have changed).

Parrot A sample run The silent knight The silent knight strode solemnly down the dank and dark hall. strode solemnly down the dank and dark hall. [enter]

Terminating condition?

Do you notice anything odd?

Do you notice anything odd? The program works fine. This should seem surprising because the second line entered contains 44 characters, and the line array holds only 20, including the newline character!

Do you notice anything odd? fputs( strode solemnly dow, stdout); fputs( n the dank and dark hall\n, stdout);