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.

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

Week 5 Part I Kyle Dewey. Overview Exam will be back Thursday New office hour More on functions File I/O Project #2.
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.
File AccessCS-2301, B-Term File Access CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming Language, 2 nd.
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.
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.
Standard Input and Output. Overview Data communication with a C program and the outside world is performed through files Files are a non-volatile way.
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 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.
File IO and command line input CSE 2451 Rong Shi.
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:
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()
chap8 Chapter 12 Files (reference: Deitel ’ s chap 11)
GAME203 – C Files stdio.h C standard Input/Output “getchar()”
Files A collection of related data treated as a unit. Two types Text
CSC Programming for Science Lecture 18: More Data Files.
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.
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.
Chapter 7 Text Input/Output Objectives
TMF1414 Introduction to Programming
Chapter 7 Text Input/Output Objectives
File Access (7.5) CSE 2031 Fall July 2018.
File I/O.
Input/Output (Continued)
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
File Input/Output.
CSE1320 Files in C Dr. Sajib Datta
Programming in C Input / Output.
Computer Science 210 Computer Organization
Programming in C Input / Output.
Files I/O, Streams, I/O Redirection, Reading with fscanf
CSE1320 Files in C Dr. Sajib Datta
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
CSC215 Lecture Input and Output.
CSE1320 Strings Dr. Sajib Datta
Beginning C Lecture 11 Lecturer: Dr. Zhao Qinpei
Exam 4 review Copyright © 2008 W. W. Norton & Company.
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
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)
Programming in C Input / Output.
Files.
File Handling in C.
Chapter 12: File I/O.
EPSII 59:006 Spring 2004.
FILE handeling.
Module 12 Input and Output
A First Book of ANSI C Fourth Edition
CS1100 Computational Engineering
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
Files Chapter 8.
Presentation transcript:

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 associated with the keyboard. They write their output to another stream usually associated with the screen. Accessing a stream is done through file pointers which have type FILE * The FILE type is defined in <stdio .h> For example, if a program needs two streams in addition to the standard ones: FILE *fp1, *fp2; A program may declare any number of FILE * variables, although operating systems usually limit the number of streams that can be open at any one time.

<stdio. h> provides three standard streams <stdio.h> provides three standard streams. These streams are ready to use. printf, scanf, putchar, getchar, puts, and gets obtain input from stdin and send output to stdout. By default, stdin represents the keyboard and stdout and stderr represent the screen. Some operating systems, however, allow these default meanings to be changed via a mechanism known as redirection.

A program can obtain its input from a file instead of from the keyboard. Put the name of the file on the command line, preceded by the < character: demo <in.dat This technique, known as input redirection, essentially makes the stdin stream represent a file (in.dat, in this case) instead of the keyboard. Output redirection is similar. Put a file name on the command line, preceded by the > character: demo >out. dat All data written to stdout will go to the out.dat file instead of the screen. Combine output redirection with input redirection: demo <in.dat >out.dat

<stdio. h> supports two kinds of files: text and binary. In a text file, the bytes represent characters, making it possible for a human to examine the file or edit it. The source code for a C program is stored in a text file. In a binary file, bytes do not represent characters; groups of bytes represent other types of data, such as integers and floating-point numbers. An executable C program is stored in a binary file.

Difference between text files and binary files for the number 32,767. 1. Store the number in text form as the characters 3, 2, 7, 6, and 7. Use the ASCII character set and obtain the following five bytes: p.637 – ASCII Character Set – 2=50, 3=51, 4=52, 5=53, 6=54, 7=55 3 in ASCII =51=(2^5)+(2^4)+(0)+(0)+(2^1)+(2^0)=(32)+(16)+(0)+(0)+(2)+(1)=51 2. Store the number in binary form, which takes as few as two bytes: (2^15) – 1 = (32768) – 1 = 32767 This example shows, storing numbers in binary saves quite a bit of space.

<stdio.h> provides other file operations: Opening a File Opening a file for use as a stream requires a call of the fopen function. fopen's first argument is a string containing the name of the file to be opened. The second argument is a "mode string" that specifies what operations we intend to perform on the file. fopen returns a file pointer that the program saves in a variable.

A typical call of fopen looks like this: fp = fopen("in.dat", "r"); /* opens in.dat for reading */ The “r” indicates data will be read from the file, but none will be written to it. fopen to open a binary file, include the letter b in the mode string. Table 22.3 lists mode strings for binary files.

<stdio .h> distinguishes between writing data and appending data. When data is written to a file, it normally overwrites what was previously there When a file is opened for appending, the file actually adds it to the end of the file, thus preserving the file's original contents. By the way, special rules apply when a file is opened for both reading and writing (the mode string contains the + character). Can't switch from reading to writing without calling a file-positioning function. Can't switch from writing to reading without either calling fflush (covered later in this section) or calling a file-positioning function.

Closing a File The fclose function allows a program to close a file that it's no longer using. The argument to fclose must be a file pointer obtained from a call of fopen or freopen, fclose returns zero if the file was closed successfully; otherwise, it returns the error code EOF (a macro defined in <stdio.h>). To show how fopen and fclose are used in practice,

This program opens the file example This program opens the file example.dat for reading, checks that it was opened successfully, then closes it before terminating: #include <stdio.h> #include <stdlib.h> #define FILE_NAME "example.dat“ main() { FILE *fp; fp = fopen(FILE_NAME, "r"); if (fp == NULL) { printf("Can't open %s\n", FILE_NAME); exit(EXIT_FAILURE) ; } fclose(fp); return 0; } It is not unusual to see the call of fopen combined with the declaration of fp: FILE *fp = fopen(FILE_NAME, "r"); or the test against NULL: if ((fp = fopen(PILE_NAME, "r")) == NULL) ...

Attaching a File to a Stream freopen attaches a different file to a stream that is already open. It associates a file with one of the standard streams: stdin, stdout, or stderr. To cause a program to begin writing to the file foo, use the following: if. (freopen("foo", "w", stdout) == NULL) { /* error; foo can't be opened */ } freopen's normal return value is its third argument (a file pointer). If it can not open the new file, freopen returns a null pointer, freopen ignores the error if the old file can not be closed.

Obtaining File Names from the Command Line When writing a program that will need to open a file, one problem soon becomes apparent: how do we supply the file name to the program? Execute a program named demo, supply it with file names by putting them on the command line: demo names.dat dates.dat to access command-line arguments; main as a function with two parameters argc is the number of command-line arguments; argv is an array of pointers to the argument strings, main(int argc, char *argv[ ]) { … }

argc is the number of command-line arguments; argv is an array of pointers to the argument strings argv [ 0 ] points to the program name, argv [ 1 ] through argv[argc-1] point to the remaining arguments, and argv [argc] is a null pointer. demo names.dat dates.dat argc is 3 argv [ 0 ] points to a string containing the program name argv [ 1 ] points to the string " names.dat", and argv [ 2 ] points to the string " dates.dat":