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.

Slides:



Advertisements
Similar presentations
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.
Advertisements

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.
Kernel File Interface operating systems (or programming I/O in Unix)
File I/O.
 Types of files  Command line arguments  File input and output functions  Binary files  Random access.
Random File Access CHAPTER 7. C.12 2 Text and Binary Files » While the concepts of “text files” given, we processed files sequentially, one character,
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.
Lone Leth Thomsen Input / Output and Files. April 2006Basis-C-8/LL2 sprintf() and sscanf() The functions sprintf() and sscanf() are string versions of.
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.
1 Lecture09: File I/O 5/6/2013 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Characters and Strings File Processing Exercise C Programming:Part 3.
Text and Binary File Processing 程式設計 潘仁義 CCU COMM.
File IO and command line input CSE 2451 Rong Shi.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 4.
ECE 103 Engineering Programming Chapter 44 File I/O Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103 material developed.
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.
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Introduction Introduce some standard library functions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 11 – File Processing Outline 11.1Introduction.
C Program Design C File Processing 主講人:虞台文. Content Introduction Data Hierarchy Files and Streams Creating a Sequential-Access File Reading Data from.
Chapter 7 Files By C. Shing ITEC Dept Radford University.
CNG 140 C Programming (Lecture set 10) Spring Chapter 10 Data Files.
FILE IO in ‘C’ by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
chap8 Chapter 12 Files (reference: Deitel ’ s chap 11)
C Programming Lecture 12 : File Processing
C File Processing. Objectives To be able to create, write and update files. To become familiar with sequential access file processing. To become familiar.
Structured Programming Approach Module VIII - Additional C Data Types File Handling Prof: Muhammed Salman Shamsi.
GAME203 – C Files stdio.h C standard Input/Output “getchar()”
1 CSC103: Introduction to Computer and Programming Lecture No 28.
Adv. UNIX:fp/101 Advanced UNIX v Objectives of these slides: –a more detailed look at file processing in C Special Topics in Comp. Eng.
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 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.
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.
Advanced Programming in the UNIX Environment Hop Lee.
 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)
External Files: Abstractly, a file can be thought of as a stream of data (either char or binary). C has two groups of files: standard files, such as stdin,
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
File I/O.
Input/Output (Continued)
Plan for the Day: I/O (beyond scanf and printf)
C Programming:Part 3 Characters and Strings File Processing Exercise.
CS111 Computer Programming
CSE1320 Files in C Dr. Sajib Datta
What you need for the 1st phase of project
File I/O We are used to reading from and writing to the terminal:
Chapter 11 – File Processing
CSC215 Lecture Input and Output.
CSC215 Lecture Input and Output.
Exam 4 review Copyright © 2008 W. W. Norton & Company.
Random File Access CHAPTER 7.
Text and Binary File Processing
File Input and Output.
Fundamental of Programming (C)
C Preprocessing File I/O
CSc 352 File I/O Saumya Debray Dept. of Computer Science
Professor Jodi Neely-Ritz University of Florida
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:

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 parameter. A file pointer is a pointer to a FILE structure (FILE is defined in ). A program may declare as many file pointers as needed: FILE *fp1, *fp2; A file pointer represents a stream, which may be a file or—in general—any source of input or output.

File Pointers and Streams Three streams are standard: stdin Standard input stdout Standard output stderr Standard error These streams need not be opened or closed. Standard input and output can be redirected in both UNIX and Windows: prog result UNIX also allows redirection of the standard error stream. Only certain versions of Windows (NT and 2000) allow this. Redirection of standard error is done by using 2> instead of >.

Opening and Closing Files Files can be opened by calling fopen: FILE *fopen(const char *filename, const char *mode); mode can be one of the following: "r" Open for reading "w" Open for writing (file need not exist) "a" Open for appending (file need not exist) "r+" Open for reading and writing, starting at beginning "w+" Open for reading and writing (truncate if file exists) "a+" Open for reading and writing (append if file exists) If the file cannot be opened, fopen returns NULL. Files can be closed by calling fclose: int fclose(FILE *stream);

Opening and Closing Files Example: #include #define INPUT_FILE "example.dat" int main(void) { FILE *fp; fp = fopen(INPUT_FILE, "r"); if (fp == NULL) { printf("Can't open %s\n", INPUT_FILE); exit(EXIT_FAILURE); } … fclose(fp); return 0; }

Command-Line Arguments The main function may have parameters named argc and argv, which allow access to the command line when the program is executed: int main(int argc, char *argv[]) { … } argc is a count of the number of command line arguments (including the name of the program itself). argv is an array of pointers to the command line arguments. argv[0] contains a pointer to the name of the program. argv[1] through argv[argc-1] point to the remaining command line arguments.

Command-Line Arguments The following program (exist.c) obtains a file name from the command line and checks whether the file can be opened: #include int main(int argc, char *argv[]) { FILE *fp; if (argc != 2) { printf("usage: exist filename\n"); exit(EXIT_FAILURE); } if ((fp = fopen(argv[1], "r")) == NULL) printf("%s does not exist\n", argv[1]); else { printf("%s exists\n", argv[1]); fclose(fp); } return 0;}

Text I/O The standard I/O library provides a number of functions and macros for reading character data. Here is a partial list: getc(fp) Reads a character from fp (macro) getchar() Reads a character from stdin (macro) fgetc(fp) Similar to getc, but is a function scanf(format,...) Reads formatted input from stdin fscanf(fp, format,...) Reads formatted input from fp gets(s) Reads characters from stdin up to next newline fgets(s, n, fp) Reads at most n – 1 characters from fp

Text I/O Here is a partial list of output functions: putc(c, fp) Writes the character c to fp (macro) putchar(c) Writes the character c to stdout (macro) fputc(c, fp) Similar to putc, but is a function printf(format,...) Writes formatted output to stdout fprintf(fp, format,...) Writes formatted output to fp puts(s) Writes the string s to stdout fputs(s, fp) Writes the string s to fp

Text I/O Watch out for small differences between similar functions: puts always adds a new-line character to the end of its output; fputs doesn’t. fgets always includes a new-line character at the end of its input string; gets doesn’t. All output functions return a value: putc, putchar, and fputc return the character written. printf and fprintf return the number of bytes written. puts and fputs return the last character written. All seven functions return EOF (a macro defined in ) if an error occurs during output.

Detecting End-of-File When getc, getchar, or fgetc detects that the end of the input file has been reached or that an input error has occurred, it returns EOF. Note: All three return an integer, not a character. The feof function can confirm that end-of-file was actually reached: int feof(FILE *stream); /* returns nonzero if eof */ The ferror function can confirm that an error occurred: int ferror(FILE *stream); /* returns nonzero if error */

Detecting End-of-File The value returned by scanf and fscanf indicates the actual number of input items that were read. If end-of-file occurred before even one item could be read, the return value is EOF. gets and fgets return a pointer to the string read; on error or end-of-file, they return NULL.

Binary Streams There are two kinds of streams: A text stream consists of lines of characters terminated by the newline character. A binary stream consists of a sequence of bytes (characters). Under Windows, there are two differences between text streams and binary streams: When a new-line character is written to a text stream, it is expanded into a carriage- return/line-feed pair. The reverse translation takes place during input. A control-Z character (\x1a) in an input file is assumed to mark the end of the file.

Binary Streams Under UNIX, there is no difference between a text stream and a binary stream. Whether a file is treated as a text file or a binary file depends on the mode that was specified when it was opened. The modes listed previously are used for text files. When a binary file is opened, the mode string should include the letter b: "rb" Open for reading "wb" Open for writing (file need not exist) "ab" Open for appending (file need not exist) "rb+" Open for reading and writing, starting at beginning "wb+" Open for reading and writing (truncate if file exists) "ab+" Open for reading and writing (append if file exists)

Binary I/O The fwrite function writes a block of binary data: size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream); fwrite writes nmemb elements of size size to stream. fwrite returns the actual number of elements written. The fread function reads a block of binary data: size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream); fread reads up to nmemb elements of size size from stream, storing them at the address specified by ptr. fread returns the actual number of elements read.

Binary I/O Numeric data written using printf is converted to character form. Numeric data written using fwrite is left in binary form. Advantages of using fwrite: Less disk space is required. Writing and reading takes less time. Disadvantages: Data can’t be read easily by humans. Data isn’t portable between different types of computers. Although fread and fwrite are normally applied to binary streams, they can be used with text streams as well. Conversely, the text I/O functions can be used with binary streams. In both cases, however, watch out for unexpected results.

Random Access The fseek, ftell, and rewind functions support random access within files. Random access is most often used with binary files. These functions can also be used with text files, but some restrictions apply. fseek allows repositioning within a file. int fseek(FILE *stream, long int offset, int whence); The new file position is determined by offset and whence. offset is a (possibly negative) byte count relative to the position specified by whence. whence must have one of the following values: SEEK_SET Beginning of file SEEK_CUR Current file position SEEK_END End of file

Random Access Examples of fseek: fseek(fp, 0, SEEK_SET); /* move to beginning of file */ fseek(fp, 0, SEEK_END); /* move to end of file */ fseek(fp, -10, SEEK_CUR); /* move back 10 bytes */ ftell returns the current file position: long int ftell(FILE *stream); This may be saved and later supplied to a call of fseek: long int file_pos; file_pos = ftell(fp); … fseek(fp, file_pos, SEEK_SET); /* return to previous position */ The call rewind(fp) is equivalent to fseek(fp, 0, SEEK_SET).

Example Program: Modifying a Database #include #define NAME_LEN 25 #define MAX_PARTS 100 #define DATA_FILE "invent.dat" struct part { int part_no; char part_name[NAME_LEN+1]; int on_hand; } inventory[MAX_PARTS]; int num_parts;

Example Program: Modifying a Database int main(void) { FILE *fp = fopen(DATA_FILE, "rb+"); if (fp == NULL) { fprintf(stderr, "Can't open %s\n", DATA_FILE); exit(EXIT_FAILURE); } num_parts = fread(inventory, sizeof(struct part), MAX_PARTS, fp); /* modify inventory */ rewind(fp); fwrite(inventory, sizeof(struct part), num_parts, fp); fclose(fp); return 0; }