COMP1917: 15 File IO

Similar documents
COMP1917: 09 Arrays and Strings

Introduction to file management

Standard File Pointers

EM108 Software Development for Engineers

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

Standard C Library Functions

System Software Experiment 1 Lecture 7

CE Lecture 11

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

2009 S2 COMP File Operations

M.CS201 Programming language

PROGRAMMAZIONE I A.A. 2017/2018

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

Laboratory: USING FILES I. THEORETICAL ASPECTS

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)

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

CS246 Spring14 Programming Paradigm Files, Pipes and Redirection

Input / Output Functions

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

Organization of a file

Darshan Institute of Engineering & Technology for Diploma Studies Unit 6

File I/O Lesson Outline

C Programming Language

CSI 402 Systems Programming LECTURE 4 FILES AND FILE OPERATIONS

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

File I/O BJ Furman 23OCT2010

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

Goals of this Lecture

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

I/O Management! Goals of this Lecture!

I/O Management! Goals of this Lecture!

The fopen command can open an external file in C language. If the file is exist, the file pointer is not NULL. Otherwise, the file pointer is NULL.

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

Chapter 10. File Processing 248 FILE PROCESSING

ENCM 335 Fall 2018 Lab 6 for the Week of October 22 Complete Instructions

Introduction to Computer Programming Lecture 18 Binary Files

File I/O Lesson Outline

Programming & Data Structure

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

Programming Language B

File IO and command line input CSE 2451

Today s Learning Objectives

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

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

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

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

Introduction to C Recursion, sorting algorithms, files

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

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

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

ENG120. Misc. Topics

Continued from previous lecture

C programming basics T3-1 -

Week 8: Arrays and File I/O. BJ Furman 21OCT2009

25.2 Opening and Closing a File

Naked C Lecture 6. File Operations and System Calls

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

Input/Output and the Operating Systems

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

HIGH LEVEL FILE PROCESSING

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

CS1003: Intro to CS, Summer 2008

Basic and Practice in Programming Lab 10

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

C Basics And Concepts Input And Output

Data File and File Handling

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

Guide for The C Programming Language Chapter 4

CS240: Programming in C

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

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

The Design of C: A Rational Reconstruction: Part 2

Lecture 9: File Processing. Quazi Rahman

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

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

A Crash Course in C. Steven Reeves

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

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

Programming. Functions and File I/O

UNIX System Programming

IO = Input & Output 2

Computer programming

Programming Fundamentals

Intermediate Programming, Spring 2017*

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

gcc o driver std=c99 -Wall driver.c everynth.c

Fundamentals of Programming & Procedural Programming

CSC209H Lecture 3. Dan Zingaro. January 21, 2015

File Handling. 21 July 2009 Programming and Data Structure 1

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

Ch 11. C File Processing (review)

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

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

File Input and Output

Princeton University Computer Science 217: Introduction to Programming Systems The Design of C

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

CSE 410: Systems Programming

Recitation 2/18/2012

C-Refresher: Session 10 Disk IO

DATA STRUCTURES USING C

Transcription:

COMP1917: 15 File IO Sim Mautner s.mautner@unsw.edu.au October 9, 2016 Sim Mautner (UNSW) COMP1917: 15 File IO October 9, 2016 1 / 8

Purpose Read/write external files from within an application. Previously, all data had to be entered each time an application was run. This lets us save data from the application and retrieve it for later use. This lecture covers text files, not binary files. There are different modes and read/write functions for binary files. Sim Mautner (UNSW) COMP1917: 15 File IO October 9, 2016 2 / 8

Examples In a game: Settings file for an application (sound on/off). High scores. Saved games. Data (eg maps) for different levels of a game. In a word processor: The data in a text document for a word processor. The saved styles for different types of headings, paragraphs etc that might be used in a different document. In a browser: Bookmarks. Autofill usernames and passwords. In a calendar: All the events, their times and dates, etc. Sim Mautner (UNSW) COMP1917: 15 File IO October 9, 2016 3 / 8

File IO in C Open a file Read/Write to the file Close the file While a file is open it is considered locked and cannot be used by any other applications until it has been closed. Closing a file releases the lock on the file, allowing other applications to gain access to it. Sim Mautner (UNSW) COMP1917: 15 File IO October 9, 2016 4 / 8

Opening a File FILE *fp = fopen("filename.txt", "w"); fopen - opens a file parameter 1 - the name of the file to be opened parameter 2 - the mode in which to open the file return value - a pointer to the file which has been opened. This pointer is then used to reference the opened file for operations such as reading, writing, and closing of the file. Sim Mautner (UNSW) COMP1917: 15 File IO October 9, 2016 5 / 8

File Modes r Opens an existing text file for reading purpose. w Opens a text file for writing. If it does not exist, then a new file is created. Starts writing from the beginning of the file. a Opens a text file for writing in appending mode. If it does not exist, then a new file is created. Starts writing at the end of the existing file contents. r+, w+, a+ Opens a file for both reading and writing. w+ truncates the file length to zero if it exists, while a+ starts reading from the start of the file and writing at the end of the existing file contents. Sim Mautner (UNSW) COMP1917: 15 File IO October 9, 2016 6 / 8

Demo Ex 1: Use fopen to create a file. Check that the file exists. Sim Mautner (UNSW) COMP1917: 15 File IO October 9, 2016 7 / 8

Writing/Appending to a File Or fputs("the text I want to write in the file\n", fp); fprintf(fp, "the text I want to write in the file\n"); Sim Mautner (UNSW) COMP1917: 15 File IO October 9, 2016 8 / 8

Demo Ex 2: Use fputs or fprintf to write to a file. Check that the text has been written to the file. Sim Mautner (UNSW) COMP1917: 15 File IO October 9, 2016 9 / 8

Reading from a File Or char lineoftext[max_line_length]; char * success = fgets(lineoftext, MAX_LINE_LENGTH, fp); char lineoftext[max_line_length]; fscanf(fp, "%s", lineoftext); Sim Mautner (UNSW) COMP1917: 15 File IO October 9, 2016 10 / 8

Demo Ex 3: Use fgets or fscanf to read from the file from Ex 2. Check that the contents is as expected by displaying it. Sim Mautner (UNSW) COMP1917: 15 File IO October 9, 2016 11 / 8

Closing a File int fileclosesuccess = fclose(fp); fclose - closes a file. parameter 1 - the file which should be closed. This parameter should be of type FILE*. return value - returns 0 if the close has been successful or EOF if there was an error. Don t forget to close the file. If you forget, it may be unable to be opened by another application, or at by the same application at a later stage because it has been locked. Sim Mautner (UNSW) COMP1917: 15 File IO October 9, 2016 12 / 8

Exercises Ex 4: Write an application which uses the following functions: 1 Write a function which reads in 10 names and grades from stdin into an appropriately designed data structure and returns that data structure. 2 Write a function which saves the data in your structure into a file called grades.txt. Ex 5: Write an application which uses the following functions: 1 Write a function which reads in 10 names and grades from grades.txt (created in Ex 4) into an appropriate data structure. 2 Write a function which prints out the data in the structure. Sim Mautner (UNSW) COMP1917: 15 File IO October 9, 2016 13 / 8