File Input/Output.

Slides:



Advertisements
Similar presentations
File Management in C. What is a File? A file is a collection of related data that a computers treats as a single unit. Computers store files to secondary.
Advertisements

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.
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,
UNIT 15 File Processing.
BITS Pilani, Pilani Campus TA C252 Computer Programming - II Vikas Singh File Handling.
File Management in C. A file is a collection of related data that a computers treats as a single unit. File is a collection of data stored permanently.
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.
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.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 11 – File Processing Outline 11.1Introduction 11.2The Data Hierarchy 11.3Files and Streams 11.4Creating.
File I/O.
CSE1301 Computer Programming: Lecture 19 File I/O
V-1 University of Washington Computer Programming I File Input/Output © 2000 UW CSE.
Files Programs and data are stored on disk in structures called files Examples Turbo C++ - binary file Word binary file lab1.c - text file lab1.data.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students C File Input and Output Checking input for errors.
A First Book of ANSI C Fourth Edition Chapter 10 Data Files.
C Programming – Part 6 File Input and Output
Introduction to Programming Using C Files. 2 Contents Files Working with files Sequential files Records.
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.
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.
Chapter 11: Data Files and File Processing Files and streams Creating a sequential access file Reading data from a sequential access file Using fgetc()
Basic I/O in C Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Stream Model of I/O header file: A stream provides a connection.
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.
CNG 140 C Programming (Lecture set 10) Spring Chapter 10 Data Files.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Input and Output.
GAME203 – C Files stdio.h C standard Input/Output “getchar()”
CS 1704 Introduction to Data Structures and Software Engineering.
Files A collection of related data treated as a unit. Two types Text
CSCI N305: C Programming Copyright ©2006  Department of Computer & Information Science File Handling in C.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the basic properties and characteristics of external files ❏ To.
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.
Real Numbers Device driver process within the operating system that interacts with I/O controller logical record 1 logical record 2 logical record 3.
Chapter 7 Text Input/Output Objectives
Chapter 7 Text Input/Output Objectives
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
File Access (7.5) CSE 2031 Fall July 2018.
Chapter 18 I/O in C.
File I/O.
CSC215 Lecture Input and Output.
Plan for the Day: I/O (beyond scanf and printf)
CS111 Computer Programming
CSE1320 Files in C Dr. Sajib Datta
CSE1320 Files in C Dr. Sajib Datta
Computer Programming Lecture 15 Text File I/O
Programming in C Input / Output.
Programming in C Input / Output.
CSE1320 Files in C Dr. Sajib Datta
Lecture 13 Input/Output Files.
Beginning C Lecture 11 Lecturer: Dr. Zhao Qinpei
Input/Output and the Operating Systems
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
Text and Binary File Processing
File Input and Output.
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
Chapter 11 Files chap8.
CSc 352 File I/O Saumya Debray Dept. of Computer Science
I/O CS580U - Fall 2018.
Professor Jodi Neely-Ritz University of Florida
Files Chapter 8.
Presentation transcript:

File Input/Output

Files A collection of related data treated as a unit. Two types Text Binary Stored in secondary storage devices. Buffer Temporary storage area that holds data while they are being transferred to or from memory.

Text Files Data are stored as human-readable characters Each line of data ends with a newline character

Standard Files We have already discussed the standard files: Standard input Associated with the keyboard Accessed using stdin Standard output Associated with the monitor Accessed using stdout Standard error Accessed using stderr

User Files External files defined by the user Must be explicitly opened in the program. C assigns a logical name to each external file. fopen() function Prepares a file for processing Makes the connection between the external file and the program Creates a program file table or control structure to store the information needed to process the file

fopen FILE * fopen(char *filename, char *mode) filename – string that supplies the name of the file as known to the external world. e.g. scores.dat mode Meaning r Open file for reading If file exists, the marker is positioned at beginning. If file does not exist, error returned w Open text file for writing If file exists, it is emptied. If file does not exist, it is created. a Open text file for append. If file exists, the marker is positioned at the end.

Examples // Define and open the file scores.dat for input FILE *dataFile; dataFile = fopen("scores.dat", "r"); // Define and open the file prog.out for output FILE *outFile; outFile = fopen("prog.out", "w");

fopen() If succesful, will return a pointer to a FILE object If not, a null pointer is returned Always check to make sure the open was successful. If not, print an error message and exit Example: FILE *input = fopen("data.dat", "r"); if(input == NULL) { printf("Failure to open file. Exiting\n"); exit(1); }

fopen() This semester, we will use the assert() function Example: FILE *input = fopen("data.dat", "r"); assert(input != NULL); We will need to have #include <assert.h> at the top of our program.

fclose int fclose(FILE *fp) Note that the parameter is a FILE *, not the name of the file. Used to close a file when no longer needed Prevents associated file from being accessed again Guarantees that data stored in the stream buffer is written to the file Releases the FILE structure so that it can be used with another file Frees system resources, such as buffer space Returns zero on success, or EOF on failure

fclose() Examples: fclose(input); fclose(dataFile); fclose(outFile);

Field Specification Consists of % character, a conversion code, and other formatting instructions With one exception (*), each field specification must have a matching parameter in the parameter list that follows the string The type in the field specification and the type of the parameter must match Can have up to six elements for output Can have up to five elements for input; precision is not allowed

Field Specification Elements of field specification scanf / fscanf: % flag Max width size conversion code * do not store data h short l long int l double (scan only) L long double left justify + sign (+ or -) space if positive 0 zero padding d, f, c, s, e, E, x, X, i, u, g, G % flag Max width precision size conversion code printf / fprintf

Field Specification Examples %d %7d %+d %lf %7.2lf

End of File Controlled Loops feof int feof(FILE *fp) Function to check if end of file has been reached. For an end of file controlled loop Read before the loop Test for end of file: while (!feof(ptr)) Inside loop: Process Read at the bottom of the loop