File I/O Lesson Outline

Slides:



Advertisements
Similar presentations
I/O means Input and Output. One way: use standard input and standard output. To read in data, use scanf() (or a few other functions) To write out data,
Advertisements

UNIT 15 File Processing.
Files in C Rohit Khokher.
BITS Pilani, Pilani Campus TA C252 Computer Programming - II Vikas Singh File Handling.
Files in C Rohit Khokher. Files in C Real life situations involve large volume of data and in such cases, the console oriented I/O operations pose two.
File I/O Lesson CS1313 Spring File I/O Lesson Outline 1.File I/O Lesson Outline 2.File I/O Using Redirection #1 3.File I/O Using Redirection #2.
Chapter 11: Data Files & File Processing In this chapter, you will learn about Files and streams Creating a sequential access file Reading data from a.
C Programming - Lecture 3 File handling in C - opening and closing. Reading from and writing to files. Special file streams stdin, stdout & stderr. How.
CSCI 171 Presentation 12 Files. Working with files File Streams – sequence of data that is connected with a specific file –Text Stream – Made up of lines.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Introduction Data files –Can be created, updated,
 2007 Pearson Education, Inc. All rights reserved C File Processing.
Chapter 11 C File Processing Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Console and File I/O - Basics Rudra Dutta CSC Spring 2007, Section 001.
V-1 University of Washington Computer Programming I File Input/Output © 2000 UW CSE.
Chapter 18 I/O in C. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Standard C Library I/O commands.
C Basic File Input/Output Manipulation C Programming – File Outline v File handling in C - opening and closing. v Reading from and writing to files.
Lone Leth Thomsen Input / Output and Files. April 2006Basis-C-8/LL2 sprintf() and sscanf() The functions sprintf() and sscanf() are string versions of.
CP104 Introduction to Programming File I/O Lecture 33 __ 1 File Input/Output Text file and binary files File Input/output File input / output functions.
File Handling Spring 2013Programming and Data Structure1.
Chapter 18 I/O in C.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
File IO and command line input CSE 2451 Rong Shi.
Lecture 8a: File I/O BJ Furman 21MAR2011. Learning Objectives Explain what is meant by a data stream Explain the concept of a ‘file’ Open and close files.
Chapter 11: Data Files and File Processing Files and streams Creating a sequential access file Reading data from a sequential access file Using fgetc()
Chapter 7 : File Processing1 File-Oriented Input & Output CHAPTER 7.
1 CHAPTER6 CHAPTER 6. Objectives: You’ll learn about;  Introduction  Files and streams  Creating a sequential access file  Reading data from a sequential.
Lecture 11: Files & Arrays B Burlingame 18 November 2015.
Using Files Declare a variable, called file pointer, of type FILE * Use function fopen to open a named file and associate this file with the file pointer.
24-2 Perform File I/O using file pointers FILE * data-type Opening and closing files Character Input and Output String Input and Output Related Chapter:
CS 261 – Recitation 7 Spring 2015 Oregon State University School of Electrical Engineering and Computer Science.
Files A collection of related data treated as a unit. Two types Text
 2007 Pearson Education, Inc. All rights reserved. 1 C File Processing.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the basic properties and characteristics of external files ❏ To.
CSC Programming for Science Lecture 18: More Data Files.
Lecture 20: C File Processing. Why Using Files? Storage of data in variables and arrays is temporary Data lost when a program terminates. Files are used.
1 Computer Programming Lecture 15 Text File I/O Assist. Prof Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering
Connecting to Files In order to read or write to a file, we need to make a connection to it. There are several functions for doing this. fopen() – makes.
CSE 251 Dr. Charles B. Owen Programming in C1 Strings and File I/O.
CSE 251 Dr. Charles B. Owen Programming in C1 Strings and File I/O.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
Introduction to C CSCE 343.
Chapter 22 – part a Stream refer to any source of input or any destination for output. Many small programs, obtain all their input from one stream usually.
TMF1414 Introduction to Programming
Chapter 7 Text Input/Output Objectives
CS1010 Programming Methodology
CS 261 – Recitation 7 Fall 2013 Oregon State University
CSC215 Lecture Input and Output.
Plan for the Day: I/O (beyond scanf and printf)
CS111 Computer Programming
File Input/Output.
Programming in C Input / Output.
Programming in C Input / Output.
Files I/O, Streams, I/O Redirection, Reading with fscanf
Input/Output and the Operating Systems
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
FILE HANDLING IN C.
Programming and Data Structure
File Input and Output.
File Handling.
File Access (7.5) CSE 2031 Fall January 2019.
Accessing Files in C Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Fundamental of Programming (C)
File Access (7.5) CSE 2031 Fall February 2019.
Programming in C Input / Output.
File Handling in C.
FILE handeling.
Module 12 Input and Output
EECE.2160 ECE Application Programming
ETE 132 Lecture 8 By Munirul Haque.
CSc 352 File I/O Saumya Debray Dept. of Computer Science
Presentation transcript:

File I/O Lesson Outline File I/O Using Redirection #1 File I/O Using Redirection #2 Direct File I/O #1 Direct File I/O #2 File I/O Mode FILE Pointer Reading from a File Writing to a File scanf vs fscanf/printf vs fprintf fclose How to Use File I/O File I/O Lesson CS1313 Spring 2017

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 File I/O Lesson CS1313 Spring 2017

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? File I/O Lesson CS1313 Spring 2017

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? File I/O Lesson CS1313 Spring 2017

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. File I/O Lesson CS1313 Spring 2017

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). File I/O Lesson CS1313 Spring 2017

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. File I/O Lesson CS1313 Spring 2017

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); File I/O Lesson CS1313 Spring 2017

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); File I/O Lesson CS1313 Spring 2017

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, ...). File I/O Lesson CS1313 Spring 2017

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) */ File I/O Lesson CS1313 Spring 2017

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", } /* if (fclose(fileptr) != file_close_success) */ File I/O Lesson CS1313 Spring 2017