File I/O Lesson Outline

Similar documents
File I/O Lesson Outline

Standard File Pointers

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

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

Programming & Data Structure

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

System Software Experiment 1 Lecture 7

Input / Output Functions

C programming basics T3-1 -

EE458 - Embedded Systems Lecture 4 Embedded Devel.

File I/O BJ Furman 23OCT2010

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

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

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

File IO and command line input CSE 2451

Writing to and reading from files

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

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.

PROGRAMMAZIONE I A.A. 2017/2018

Binghamton University. CS-211 Fall Input and Output. Streams and Stream IO

CMPT 102 Introduction to Scientific Computer Programming. Input and Output. Your first program

C PROGRAMMING. Characters and Strings File Processing Exercise

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

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)

Intermediate Programming, Spring 2017*

Organization of a file

File Handling. 21 July 2009 Programming and Data Structure 1

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

Solutions to Chapter 7

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

CS240: Programming in C

CSE 230 Intermediate Programming in C and C++ Input/Output and Operating System

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

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

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

C Basics And Concepts Input And Output

Goals of this Lecture

User Defined Functions 2 Outline

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

I/O Management! Goals of this Lecture!

I/O Management! Goals of this Lecture!

COMP1917: 15 File IO

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

UNIX input and output

File Input/Output. Similarly, for reading from files and writing to files, we'll use the functions

25.2 Opening and Closing a File

Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

CSC209H Lecture 3. Dan Zingaro. January 21, 2015

Programming Language B

2009 S2 COMP File Operations

Basic and Practice in Programming Lab 10

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

Recitation 2/18/2012

Lecture 7: file I/O, more Unix

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

Lab # 4. Files & Queues in C

Binghamton University. CS-211 Fall Input and Output. Streams and Stream IO

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

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

M.CS201 Programming language

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

Standard C Library Functions

DATA STRUCTURES USING C

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

CS246 Spring14 Programming Paradigm Files, Pipes and Redirection

Lecture 9: File Processing. Quazi Rahman

Input/Output: Advanced Concepts

Input/Output and the Operating Systems

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

Lecture 8: Structs & File I/O

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

8. Characters, Strings and Files

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

Darshan Institute of Engineering & Technology for Diploma Studies Unit 6

Data File and File Handling

ENG120. Misc. Topics

Pointers and File Handling

Introduction to file management

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

CAAM 420 Notes Chapter 2: The C Programming Language

Figure 1 Ring Structures

EM108 Software Development for Engineers

CpSc 111 Lab 3 Integer Variables, Mathematical Operations, & Redirection

211: Computer Architecture Summer 2016

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-0-0)

C File Processing: One-Page Summary

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

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

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

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

Floating-point lab deadline moved until Wednesday Today: characters, strings, scanf Characters, strings, scanf questions clicker questions

Binghamton University. CS-220 Spring Includes & Streams

CE Lecture 11

High-performance computing and programming Intro to C on Unix/Linux. Uppsala universitet

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

CMPSC 311- Introduction to Systems Programming Module: Input/Output

structs as arguments

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

today cs3157-fall2002-sklar-lect05 1

Chapter 10: File Input / Output

Transcription:

Outline 1. Outline 2. File I/O Using Redirection #1 3. File I/O Using Redirection #2 4. Direct File I/O #1 5. Direct File I/O #2 6. File I/O Mode 7. FILE Pointer 8. Reading from a File 9. Writing to a File 10.scanf vs fscanf/printf vs fprintf 11.fclose 12. How to Use File I/O CS1313 Spring 2017 1

File I/O Using Redirection #1 So far in C, we ve been using only standard input (keyboard) and standard output (monitor). We know that we can input from a file by redirecting the file into standard input: % big_statistics < actual_cs1313_2097spring.txt Likewise, we can output to a file by redirecting standard output into a file: % big_statistics > actual_cs1313_2097spring_output.txt CS1313 Spring 2017 2

File I/O Using Redirection #2 In fact, we can combine redirected input with redirected output: % big_statistics < actual_cs1313_2097spring.txt \ > actual_cs1313_2097spring_output.txt But what if we wanted to use multiple files at the same time? For example, suppose we wanted to run a weather forecast, and we had a file containing our initial conditions (for example, the observed weather at midnight) and a different file containing data describing the terrain of the continental US. We want to use both of these files. But how? CS1313 Spring 2017 3

Direct File I/O #1 Most programming languages support reading and writing files directly from within the program, without having to use redirecting. In C, we can open a file using the fopen function: char filename[filename_length+1]; FILE* fileptr = (FILE*)NULL; strcpy(filename, "actual_cs1313_2097spring.txt"); fileptr = fopen(filename, "r"); What does this mean? CS1313 Spring 2017 4

Direct File I/O #2 char filename[filename_length+1]; FILE* fileptr = (FILE*)NULL; strcpy(filename, "actual_cs1313_2097spring.txt"); fileptr = fopen(filename, "r"); The fopen function opens a file in preparation for reading, writing or appending to a file. The first argument is a string representing the filename. The second argument is a string that encodes the I/O mode. CS1313 Spring 2017 5

File I/O Mode char filename[filename_length+1]; FILE* fileptr = (FILE*)NULL; strcpy(filename, "actual_cs1313_2097spring.txt"); fileptr = fopen(filename, "r"); The second argument is a string that encodes the I/O mode, which can be: "r" Open the file for reading. "w" Open the file for writing. "a" Open the file for appending (writing at the end of an existing file). CS1313 Spring 2017 6

FILE Pointer char filename[filename_length+1]; FILE* fileptr = (FILE*)NULL; strcpy(filename, "actual_cs1313_2097spring.txt"); fileptr = fopen(filename, "r"); The function fopen returns a file pointer, which is a pointer to a special data type that s used to identify and describe a file. If for some reason the file can t be opened, then the return value of fopen is NULL. CS1313 Spring 2017 7

Reading from a File In C, we can read from a file using the function fscanf, which is exactly like scanf except that its first argument is a file pointer: fscanf(fileptr, "%d", &number_of_elements); CS1313 Spring 2017 8

Writing to a File In C, we can write to a file using the function fprintf, which is exactly like printf except that its first argument is a file pointer: fprintf(fileptr, "The number of elements is %d.\n", number_of_elements); CS1313 Spring 2017 9

scanf vs fscanf/printf vs fprintf What s the difference between scanf and fscanf, or between printf and fprintf? Well, scanf reads from stdin only, while fscanf can read from any file. Likewise, printf writes to stdout only, while fprintf can write to any file. In fact, some implementations of C define scanf() as fscanf(stdin, ), and printf() as fprintf(stdout, ). CS1313 Spring 2017 10

fclose Notice that the C standard library also has a function named fclose that takes a file pointer argument. It closes the appropriate file and returns 0 if the file closed properly, or an error code otherwise: const int file_close_success = 0; int file_close_status; file_close_status = fclose(fileptr); if (file_close_status!= file_close_success) { printf("error: couldn't close the file %s.\n", filename); exit(program_failure_code); } /* if (file_close_status!= file_close_success) */ CS1313 Spring 2017 11

How to Use File I/O FILE* fileptr = (FILE*)NULL; fileptr = fopen(filename, "r"); if (fileptr == (FILE*)NULL) { printf("error: can't open file %s to read.\n", filename); exit(program_failure_code); } /* if fileptr == (FILE*)NULL) */ fscanf(fileptr, "%d", &number_of_elements); for (element = first_element; element < number_of_elements; element++) { fscanf(fileptr, "%f %f", &input_variable1[element], &input_variable2[element]); } /* for element */ if (fclose(fileptr)!= file_close_success) { printf("error: can't close file %s after reading.\n", filename); exit(program_failure_code); } /* if (fclose(fileptr)!= file_close_success) */ CS1313 Spring 2017 12