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

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

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

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

Standard C Library Functions

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

CS240: Programming in C

System Software Experiment 1 Lecture 7

Introduction to file management

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

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

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

C Basics And Concepts Input And Output

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

UNIX System Programming

Standard File Pointers

PROGRAMMAZIONE I A.A. 2017/2018

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

File IO and command line input CSE 2451

Computer System and programming in C

ENG120. Misc. Topics

Laboratory: USING FILES I. THEORETICAL ASPECTS

CS246 Spring14 Programming Paradigm Files, Pipes and Redirection

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

Naked C Lecture 6. File Operations and System Calls

Input / Output Functions

CSC209H Lecture 3. Dan Zingaro. January 21, 2015

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

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

File Handling. Reference:

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

Darshan Institute of Engineering & Technology for Diploma Studies Unit 6

Input/Output and the Operating Systems

C-Refresher: Session 10 Disk IO

C File Processing: One-Page Summary

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

M.CS201 Programming language

File I/O. Preprocessor Macros

Lecture 8: Structs & File I/O

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

Preprocessing directives are lines in your program that start with `#'. The `#' is followed by an identifier that is the directive name.

Ch 11. C File Processing (review)

Data File and File Handling

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

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

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

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

C mini reference. 5 Binary numbers 12

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

Computer programming

EM108 Software Development for Engineers

Lecture 7: file I/O, more Unix

Organization of a file

Computer Programming Unit v

CSI 402 Systems Programming LECTURE 4 FILES AND FILE OPERATIONS

File I/O, Project 1: List ADT. Bryce Boe 2013/07/02 CS24, Summer 2013 C

C Programming Language

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

Lecture 9: File Processing. Quazi Rahman

Intermediate Programming, Spring 2017*

First of all, it is a variable, just like other variables you studied

C programming basics T3-1 -

Lecture6 File Processing

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

File handling is an important part of any web application. You often need to open and process a file for different tasks.

UNIT- 2. Binary File

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)

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

IO = Input & Output 2

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

2009 S2 COMP File Operations

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

Exercise Session 3 Systems Programming and Computer Architecture

File System User API

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

CE Lecture 11

Programming Fundamentals

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

CS240: Programming in C

Chapter 11 File Processing

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

Fundamentals of Programming & Procedural Programming

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING UNIT-1

Memory Layout, File I/O. Bryce Boe 2013/06/27 CS24, Summer 2013 C

CS1003: Intro to CS, Summer 2008

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

SOFTWARE ARCHITECTURE 2. FILE SYSTEM. Tatsuya Hagino lecture URL.

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

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

Procedural Programming

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

Fundamentals of Programming. Lecture 10 Hamed Rasifard

Fundamentals of Programming. Lecture 15: C File Processing

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

25.2 Opening and Closing a File

Data Files. Computer Basics

LAB 13 FILE PROCESSING

mywbut.com 12 File Input/Output

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

STRUCTURES & FILE IO

8. Structures, File I/O, Recursion. 18 th October IIT Kanpur

Transcription:

The ability to read data from and write data to files is the primary means of storing persistent data, data that does not disappear when your program stops running. The abstraction of files that C provides is implemented in a data structure known as a FILE. Almost universally when working with files, we will be using pointers to them, FILE*.

The file manipulation functions all live in stdio.h. All of them accept FILE* as one of their parameters, except for the function fopen(), which is used to get a file pointer in the first place. Some of the most common file input/output (I/O) functions that we ll be working with are: fopen() fclose() fgetc() fputc() fread() fwrite()

fopen() Opens a file and returns a file pointer to it. Always check the return value to make sure you don t get back NULL. FILE* ptr = fopen(<filename>, <operation>);

fopen() Opens a file and returns a file pointer to it. Always check the return value to make sure you don t get back NULL. FILE* ptr1 = fopen( file1.txt, r );

fopen() Opens a file and returns a file pointer to it. Always check the return value to make sure you don t get back NULL. FILE* ptr2 = fopen( file2.txt, w );

fopen() Opens a file and returns a file pointer to it. Always check the return value to make sure you don t get back NULL. FILE* ptr3 = fopen( file3.txt, a );

fclose() Closes the file pointed to by the given file pointer. fclose(<file pointer>);

fclose() Closes the file pointed to by the given file pointer. fclose(ptr1);

fgetc() Reads and returns the next character from the file pointed to. Note: The operation of the file pointer passed in as a parameter must be r for read, or you will suffer an error. char ch = fgetc(<file pointer>);

fgetc() Reads and returns the next character from the file pointed to. Note: The operation of the file pointer passed in as a parameter must be r for read, or you will suffer an error. char ch = fgetc(ptr1);

The ability to get single characters from files, if wrapped in a loop, means we could read all the characters from a file and print them to the screen, one-by-one, essentially. char ch; while((ch = fgetc(ptr))!= EOF) printf( %c, ch); We might put this in a file called cat.c, after the Linux command cat which essentially does just this.

The ability to get single characters from files, if wrapped in a loop, means we could read all the characters from a file and print them to the screen, one-by-one, essentially. char ch; while((ch = fgetc(ptr))!= EOF) printf( %c, ch); We might put this in a file called cat.c, after the Linux command cat which essentially does just this.

The ability to get single characters from files, if wrapped in a loop, means we could read all the characters from a file and print them to the screen, one-by-one, essentially. char ch; while((ch = fgetc(ptr))!= EOF) printf( %c, ch); We might put this in a file called cat.c, after the Linux command cat which essentially does just this.

fputc() Writes or appends the specified character to the pointed-to file. Note: The operation of the file pointer passed in as a parameter must be w for write or a for append, or you will suffer an error. fputc(<character>, <file pointer>);

fputc() Writes or appends the specified character to the pointed-to file. Note: The operation of the file pointer passed in as a parameter must be w for write or a for append, or you will suffer an error. fputc( A, ptr2);

fputc() Writes or appends the specified character to the pointed-to file. Note: The operation of the file pointer passed in as a parameter must be w for write or a for append, or you will suffer an error. fputc(!, ptr3);

Now we can read characters from files and write characters to them. Let s extend our previous example to copy one file to another, instead of printing to the screen. char ch; while((ch = fgetc(ptr))!= EOF) printf( %c, ch);

Now we can read characters from files and write characters to them. Let s extend our previous example to copy one file to another, instead of printing to the screen. char ch; while((ch = fgetc(ptr))!= EOF) fputc(ch, ptr2); We might put this in a file called cp.c, after the Linux command cp which essentially does just this.

fread() Reads <qty> units of size <size> from the file pointed to and stores them in memory in a buffer (usually an array) pointed to by <buffer>. Note: The operation of the file pointer passed in as a parameter must be r for read, or you will suffer an error. fread(<buffer>, <size>, <qty>, <file pointer>);

fread() Reads <qty> units of size <size> from the file pointed to and stores them in memory in a buffer (usually an array) pointed to by <buffer>. Note: The operation of the file pointer passed in as a parameter must be r for read, or you will suffer an error. int arr[10]; fread(arr, sizeof(int), 10, ptr);

fread() Reads <qty> units of size <size> from the file pointed to and stores them in memory in a buffer (usually an array) pointed to by <buffer>. Note: The operation of the file pointer passed in as a parameter must be r for read, or you will suffer an error. double* arr2 = malloc(sizeof(double) * 80); fread(arr2, sizeof(double), 80, ptr);

fread() Reads <qty> units of size <size> from the file pointed to and stores them in memory in a buffer (usually an array) pointed to by <buffer>. Note: The operation of the file pointer passed in as a parameter must be r for read, or you will suffer an error. char c; fread(&c, sizeof(char), 1, ptr);

fread() Reads <qty> units of size <size> from the file pointed to and stores them in memory in a buffer (usually an array) pointed to by <buffer>. Note: The operation of the file pointer passed in as a parameter must be r for read, or you will suffer an error. char c; fread(&c, sizeof(char), 1, ptr);

fwrite() Writes <qty> units of size <size> to the file pointed to by reading them from a buffer (usually an array) pointed to by <buffer>. Note: The operation of the file pointer passed in as a parameter must be w for write or a for append, or you will suffer an error. fwrite(<buffer>, <size>, <qty>, <file pointer>);

fwrite() Writes <qty> units of size <size> to the file pointed to by reading them from a buffer (usually an array) pointed to by <buffer>. Note: The operation of the file pointer passed in as a parameter must be w for write or a for append, or you will suffer an error. int arr[10]; fwrite(arr, sizeof(int), 10, ptr);

fwrite() Writes <qty> units of size <size> to the file pointed to by reading them from a buffer (usually an array) pointed to by <buffer>. Note: The operation of the file pointer passed in as a parameter must be w for write or a for append, or you will suffer an error. double* arr2 = malloc(sizeof(double) * 80); fwrite(arr2, sizeof(double), 80, ptr);

fwrite() Writes <qty> units of size <size> to the file pointed to by reading them from a buffer (usually an array) pointed to by <buffer>. Note: The operation of the file pointer passed in as a parameter must be w for write or a for append, or you will suffer an error. char c; fwrite(&c, sizeof(char), 1, ptr);

fwrite() Writes <qty> units of size <size> to the file pointed to by reading them from a buffer (usually an array) pointed to by <buffer>. Note: The operation of the file pointer passed in as a parameter must be w for write or a for append, or you will suffer an error. char c; fwrite(&c, sizeof(char), 1, ptr);

Lots of other useful functions abound in stdio.h for you to work with. Here are some of the ones you may find useful!

Lots of other useful functions abound in stdio.h for you to work with. Here are some of the ones you may find useful! Function fgets() fputs() fprintf() fseek() ftell() feof() ferror() Description Reads a full string from a file. Writes a full string to a file. Writes a formatted string to a file. Allows you rewind or fast-forward within a file. Tells you at what (byte) position you are at within a file. Tells you whether you ve read to the end of a file. Indicates whether an error has occurred in working with a file.