File I/O We are used to reading from and writing to the terminal:

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

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.
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,
Chapter 11 C File Processing Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
File I/O.
Console and File I/O - Basics Rudra Dutta CSC Spring 2007, Section 001.
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.
22. FILE INPUT/OUTPUT. File Pointers and Streams Declarations of functions that perform file I/O appear in. Each function requires a file pointer as a.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
1 Lecture09: File I/O 5/6/2013 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
1. Introduction File Declaration and Initialization Creating and Opening File Closing File EOF Reading from and Writing into a File Extra : Random Access.
File IO and command line input CSE 2451 Rong Shi.
Chapter 8 File-Oriented Input and Output. 8.1 INTRODUCTION a file can also be designed to store data. We can easily update files, A data file as input.
File I/O, Project 1: List ADT Bryce Boe 2013/07/02 CS24, Summer 2013 C.
CSEB114: Principle of programming Chapter 11: Data Files & File Processing.
1 File Handling. 2 Storage seen so far All variables stored in memory Problem: the contents of memory are wiped out when the computer is powered off Example:
1 Lecture09: File I/O 11/19/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
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.
Memory Layout, File I/O Bryce Boe 2013/06/27 CS24, Summer 2013 C.
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 11 File Processing. Objectives In this chapter, you will learn: –To be able to create, read, write and update files. –To become familiar with.
 2000 Prentice Hall, Inc. All rights reserved Introduction Data files –Can be created, updated, and processed by C programs –Are used for permanent.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 11 – File Processing Outline 11.1Introduction.
Lecture 11: Files & Arrays B Burlingame 18 November 2015.
GAME203 – C Files stdio.h C standard Input/Output “getchar()”
 2007 Pearson Education, Inc. All rights reserved. 1 C File Processing.
FILES IN C. File Operations  Creation of a new file  Opening an existing file  Reading from a file  Writing to a file  Moving to a specific location.
Files. FILE * u In C, we use a FILE * data type to access files. u FILE * is defined in /usr/include/stdio.h u An example: #include int main() { FILE.
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.
C Programming Day 2. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Union –mechanism to create user defined data types.
UniMAP SemI-11/12EKT120: Computer Programming1 Files.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
6/9/2016Course material created by D. Woit 1 CPS 393 Introduction to Unix and C START OF WEEK 10 (C-4)
UniMAP SemI-11/12EKT120: Computer Programming1 Files.
Real Numbers Device driver process within the operating system that interacts with I/O controller logical record 1 logical record 2 logical record 3.
 Dynamic Memory Allocation  Linked Lists  void main() {{ ◦ FILE *file; ◦ char file_name[] = “file.txt”; ◦ if ((file = fopen(file_name, "w")) ==
Chapter 4 File Processing
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.
Lecture 11 File input/output
TMF1414 Introduction to Programming
EKT120: Computer Programming
CS1010 Programming Methodology
File I/O.
CSC215 Lecture Input and Output.
Plan for the Day: I/O (beyond scanf and printf)
C Programming:Part 3 Characters and Strings File Processing Exercise.
CS111 Computer Programming
What you need for the 1st phase of project
Files I/O, Streams, I/O Redirection, Reading with fscanf
Hank Childs, University of Oregon
Chapter 11 – File Processing
CSC215 Lecture Input and Output.
Beginning C Lecture 11 Lecturer: Dr. Zhao Qinpei
Programming and Data Structure
Text and Binary File Processing
File Input and Output.
File Handling.
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)
Files.
C Preprocessing File I/O
EPSII 59:006 Spring 2004.
Module 12 Input and Output
CSc 352 File I/O Saumya Debray Dept. of Computer Science
I/O CS580U - Fall 2018.
Professor Jodi Neely-Ritz University of Florida
File I/O We are used to reading from and writing to the terminal:
Presentation transcript:

File I/O We are used to reading from and writing to the terminal: read from stdin write to stdout But we can also read from and write to files! The ability to read data from and write data to files is the primary means of storing persistent data, or data which does not disappear when your program finishes running.

Step 1: Create a reference to the file FILE* file; Step 2: Open the file file = fopen(“file.txt”, “r”); 1st argument -- path to the file 2nd argument -- mode “r” -- read, “w” -- write, “a” -- append The abstraction of files provided by the standard I/O library (stdio) is implemented in a structure called FILE. Calling fopen returns a pointer to the file you have just opened. If it returns NULL, then it could not open the requested file. Almost all of the functions you’ll then use to manipulate your newly opened file take this file pointer as one of one of their arguments.

Step 3a: Read from the file fgetc -- returns the next character fgets -- returns a line of text fread -- reads a certain # of bytes and places them into an array fseek -- moves to a certain position Step 3b: Write to the file fputc -- write a character fputs -- returns a line of text fprintf -- print a formatted output to a file fwrite -- write an array of bytes to a file These functions allow us to read from or write to the file we’ve just opened.

Always open a file before reading from or writing to it Step 4: Close the file fclose(file); Remember! Always open a file before reading from or writing to it Always close a file if you open it Be sure to fclose any and all files that you have fopened. UNIX will close any files you’ve left open when your program terminates, but it is considered poor coding practice to rely on this behavior.

Example #1 Writing to a file #include <stdio.h> #define STUDENTS 3 int main(void) { int scores[] = { 96, 90, 83 }; FILE* file = fopen(“database”, “w”); if (file != NULL) { for (int i = 0; i < STUDENTS; i++) { fprintf(file, “%i\n”, scores[i]); } fclose(file); } } In this program, we are storing the values of an array called scores in a file named database. We open the file using the fopen function. The second argument "w" tells fopen that we want to write to this file. If a file named database does not already exist, fopen will create it. if (file != NULL) is a sanity check that call to fopen didn’t fail. fprintf takes a file pointer as its first argument. Instead of printing to the screen, fprintf prints to the file we pass to it.

What does this program do? Example #2 What does this program do? #include <stdio.h> int main(int argc, char* argv[]) { if (argc < 2) printf("Usage: cat file [file ...]\n"); return 1; } for (int i = 1; i < argc; i++) FILE* file = fopen(argv[i], "r"); if (file == NULL) printf("cat: %s: No such file or directory\n", argv[i]); for (int c = fgetc(file); c != EOF; c = fgetc(file)) putchar(c); fclose(file); return 0; This program opens each file whose name is specified in argv, reads into memory its characters, and prints those characters to stdout.