Beginning C Lecture 11 Lecturer: Dr. Zhao Qinpei

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

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.
Memory and Files Dr. Andrew Wallace PhD BEng(hons) EurIng
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,
 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.
File I/O.
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.
1 Homework Introduction to HW7 –Complexity similar to HW6 –Don’t wait until last minute to start on it File Access will be needed in HW8.
A First Book of ANSI C Fourth Edition Chapter 10 Data Files.
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 In C By - AJAY SHARMA. We will learn about- FFile/Stream TText vs Binary Files FFILE Structure DDisk I/O function OOperations.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
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.
CSEB114: Principle of programming Chapter 11: Data Files & File Processing.
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.
CNG 140 C Programming (Lecture set 10) Spring Chapter 10 Data Files.
chap8 Chapter 12 Files (reference: Deitel ’ s chap 11)
CS 261 – Recitation 7 Spring 2015 Oregon State University School of Electrical Engineering and Computer Science.
GAME203 – C Files stdio.h C standard Input/Output “getchar()”
1 CSC103: Introduction to Computer and Programming Lecture No 27.
Files A collection of related data treated as a unit. Two types Text
 2007 Pearson Education, Inc. All rights reserved. 1 C File Processing.
Files Programming 1. 2 What is a File? Is a block of arbitrary information, or resource for storing information, which is available to a computer program.
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.
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.
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.
Real Numbers Device driver process within the operating system that interacts with I/O controller logical record 1 logical record 2 logical record 3.
Silberschatz and Galvin  C Programming Language Kingdom of Saudi Arabia Ministry of Higher Education Al-Majma’ah University College of Education.
C Formatted Input/Output
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
TMF1414 Introduction to Programming
File Access (7.5) CSE 2031 Fall July 2018.
File I/O.
CS 261 – Recitation 7 Fall 2013 Oregon State University
CSC215 Lecture Input and Output.
Plan for the Day: I/O (beyond scanf and printf)
CSE1320 Files in C Dr. Sajib Datta
File Input/Output.
CSE1320 Files in C Dr. Sajib Datta
Programming in C Input / Output.
CSE1320 Files in C Dr. Sajib Datta
File I/O We are used to reading from and writing to the terminal:
Lecture 13 Input/Output Files.
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
Text and Binary File Processing
File Input and Output.
Fundamental of Programming (C)
Programming in C Input / Output.
Files.
C Preprocessing File I/O
Chapter 12: File I/O.
EPSII 59:006 Spring 2004.
Module 12 Input and Output
A First Book of ANSI C Fourth Edition
CSc 352 File I/O Saumya Debray Dept. of Computer Science
Professor Jodi Neely-Ritz University of Florida
Professor Jodi Neely-Ritz University of Florida
File I/O We are used to reading from and writing to the terminal:
Presentation transcript:

Beginning C Lecture 11 Lecturer: Dr. Zhao Qinpei Email: qinpeizhao@tongji.edu.cn http://sse.tongji.edu.cn/zhaoqinpei/Courses/

Working with files What a file is in C How files are processed How to write and read formatted files and binary files How to use temporary work files in a program Beiginning C / Qinpei Zhao 2018/11/29

a file A file is essentially a serial sequence of bytes File streams Text file: written as characters organized as lines Binary file: written as a series of bytes Regardless of the file type, it ends up as just a series of bytes (e.g., 12 bytes in a binary file could be 12 characters, 12 8-bit signed integers, 12 8-bit unsigned integers, 6 16-bit signed integers, a 32-bit integer) Beiginning C / Qinpei Zhao 2018/11/29

Accessing a file - open When you process a file in C, your program references a file through a file pointer. open a file by calling the standard library function fopen() double quote Returns the file pointer for a specific external file Beiginning C / Qinpei Zhao 2018/11/29

Accessing a file - open A call to fopen() does two things: creates a file pointer that identifies the specific file on disk that your program is going to operate on Determines what you can do with the file within your program FILE specifies a structure type that has been predefined in the header file <stdio.h> through a typedef. There’s a limit to the number of files you can open at one time (FOPEN_MAX) What’s the FOPEN_MAX on your computer? Beiginning C / Qinpei Zhao 2018/11/29

Accessing a file - open Create a new text file! opens the file and associates the file “myfile.txt” with pointer pfile. “w” indicates you can only write, can’t read. The first argument is limited to FILENAME_MAX If the file does not exist, it will create a new file with this name. If the call to fopen() does fail for any reason, NULL will be returned. The file will be overwritten if the file is positioned at the beginning of any existing data for the first operation. Beiginning C / Qinpei Zhao 2018/11/29

Accessing a file - rename The integer that’s returned will be 0 if the renaming is successful, and nonzero otherwise. The file must be closed when you call rename(), otherwise the operation will fail. Note the double backslash ( \\ ) in the file path string. Beiginning C / Qinpei Zhao 2018/11/29

Accessing a file - close When you’ve finished with a file, you need to tell the operating system to free up your file pointer. Calling the fclose() function which accepts a file pointer as an argument and returns a value of type int EOF if an error 0 otherwise EOF: end-of-file character (usually = -1) It’s good programming practice to close a file as soon as you’ve finished with it. You must also close a file before attempting to rename or remove it. Beiginning C / Qinpei Zhao 2018/11/29

Accessing a file - delete fflush(): force any unwritten data left in a buffer to be written to a file. (flush the input buffer) delete the file from the current directory that has the name pfile.txt. Take particular care with the operations that delete files. Beiginning C / Qinpei Zhao 2018/11/29

Writing to a text file fputc(): writes a single character to a text file Buffer Beiginning C / Qinpei Zhao 2018/11/29

Reading from a text file fgetc(): reads a character from a text file that has been opened for reading mchar is type int getc() v.s. gets() getc(): reads a single character from the stream specified by its argument. gets(): reads a whole line of input from a standard input stream, which is the keyboard lec11_1_usefile.c Beiginning C / Qinpei Zhao 2018/11/29

Writing strings to a text file fputs(): it continues to write characters from a string until it reaches a ‘\0’ character, which is not written to the file. char *pstr: a pointer to the character string that’s to be written to the file. FILE *pfile: a file pointer. Example: Beiginning C / Qinpei Zhao 2018/11/29

Reading strings from a text file fgets(): reading a string from a text file. char *pstr: read a string into the memory area pointed to by pstr. FILE *pfile: a file pointer. Characters are read from the file until either a ‘\n’ is read or nchars-1 characters have been read from the file. lec11_2_stringfile.c Beiginning C / Qinpei Zhao 2018/11/29

Dealing with errors Write error messages to stderr The output will always be directed to the display and it will always be written immediately to the display device. Terminating a program by calling exit() ensures that output stream buffers will be flushed. Always include some basic error checking and reporting code in all of your programs. Beiginning C / Qinpei Zhao 2018/11/29

Binary file input and output binary mode: no data is transformed or precision lost, faster than text mode. Characters such as ‘\n’ and ‘\0’ that have specific significance in text mode are of no consequence in binary mode. Beiginning C / Qinpei Zhao 2018/11/29

Writing & reading a binary file fwrite(): writing a specified number of binary data items to a file. pdata, a pointer containing the starting address in memory of where the data items to be written are stored. The second argument specifies the size in bytes of each item to be written num_items, defines a count of the number of items to be written to the file The file to which the data is to be transferred is identified by pfile. There is no check that you opened the file in binary mode when you call the fwrite() function. Beiginning C / Qinpei Zhao 2018/11/29

File modes Beiginning C / Qinpei Zhao 2018/11/29