24-2 Perform File I/O using file pointers FILE * data-type Opening and closing files Character Input and Output String Input and Output Related Chapter:

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

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,
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
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.
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.
FILES Files types and operations. Files Files are used to store data Data can be Text (ASCII only: 0  127) Binary (Full range: 0  256) Each file resides.
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.
File processing with functions H&K Chapter 3 Instructor – Gokcen Cilingir Cpt S 121 (June 27, 2011) Washington State University.
1 ICS103 Programming in C Lecture 8: Data Files. 2 Outline Why data files? Declaring FILE pointer variables Opening data files for input/output Scanning.
1 CSE1301 Computer Programming: Lecture 21 File I/O.
C Language Summary HTML version. I/O Data Types Expressions Functions Loops and Decisions Preprocessor Statements.
Guide To UNIX Using Linux Third Edition
CSE1301 Computer Programming: Lecture 19 File I/O
Console and File I/O - Basics Rudra Dutta CSC Spring 2007, Section 001.
Chapter 18 I/O in C. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Standard C Library I/O commands.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students C File Input and Output Checking input for errors.
CMPE13 Cyrus Bazeghi Chapter 18 I/O in C. CMPE Standard C Library I/O commands are not included as part of the C language. Instead, they are part.
16&17-2 Grasp the concept of top-down programming Identify Function Headers and Prototypes Understand when and where prototypes used Understand how arguments.
13&14-2 Know the forms of loop statements in C (while,do/while,for). Understanding how data conversion occurs. Read/Write data in files using Unix redirection.
18-2 Understand “Scope” of an Identifier Know the Storage Classes of variables and functions Related Chapter: ABC 5.10, 5.11.
20/03/01 Sudeshna Sarkar, CSE, IIT Kharagpur1 File Handling in C Lecture 17c 20/3/01.
C Basic File Input/Output Manipulation C Programming – File Outline v File handling in C - opening and closing. v Reading from and writing to files.
File Handling Spring 2013Programming and Data Structure1.
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.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 4.
CSEB114: Principle of programming Chapter 11: Data Files & File Processing.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
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:
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 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.
GAME203 – C Files stdio.h C standard Input/Output “getchar()”
Files A collection of related data treated as a unit. Two types Text
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.
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.
Silberschatz and Galvin  C Programming Language Kingdom of Saudi Arabia Ministry of Higher Education Al-Majma’ah University College of Education.
Chapter 7 Text Input/Output Objectives
Chapter 7 Text Input/Output Objectives
RECURSION Recursion is a process where a function calls itself. main()
Chapter 7 Text Input/Output Objectives
File Access (7.5) CSE 2031 Fall July 2018.
Chapter 18 I/O in C.
Lecture 13 & 14.
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.
Files I/O, Streams, I/O Redirection, Reading with fscanf
CSE1320 Files in C Dr. Sajib Datta
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
Programming and Data Structure
File Input and Output.
File Handling.
File Access (7.5) CSE 2031 Fall January 2019.
File Access (7.5) CSE 2031 Fall February 2019.
Programming in C Input / Output.
File Handling in C.
ETE 132 Lecture 8 By Munirul Haque.
ICS103: Programming in C 6: Pointers and Modular Programming
Files Chapter 8.
Presentation transcript:

24-2 Perform File I/O using file pointers FILE * data-type Opening and closing files Character Input and Output String Input and Output Related Chapter: ABC 11.3, 11.4

24-3 So far, we have seen that one way to manipulate files in a program is to redirect the standard input and output. E.g. in UNIX command line./a.out output.dat We redirect standard input as the file, input.dat (not a keyboard) And standard output as the file, output.dat(not screen) This method works for one input file and one output file. In this lecture we will consider an alternative, using FILE pointers.

Problem Definition Write a program that computes the average of a list of integers. The program should prompt the user for the name of both the input and output files and then read the values from the input file and print the average value in the output file. 2. Refine, Generalize, Decompose the problem definition (i.e., identify sub-problems, I/O, etc.) Input = String naming the file of integers to be read and a string naming the output file. Also, the integers in the file. Output = The average value is written to the output file.

/* C Program to compute the average of a list of numbers. */ #include void main(void) { int value,total = 0,count = 0; /* fileptrIn and fileptrOut are variables of type “FILE *” */ FILE * fileptrIn, * fileptrOut; char filenameIn[100],filenameOut[100]; printf("Please enter an input filename (use path if needed):"); scanf("%s",filenameIn); printf("Please enter an output filename (use path if needed):"); scanf("%s",filenameOut); /* open files to read “r” and write “w” */ fileptrIn = fopen(filenameIn, "r"); fileptrOut = fopen(filenameOut, "w");

/* check to see if program found file */ if ((fileptrIn == NULL) || (fileptrOut == NULL)) { printf("file not opened, program terminated.\n"); return; } /* fscanf */ while( EOF != fscanf(fileptrIn,"%i", &value)) { total += value; ++count; } /* end of while loop */ /* Write the average value to the file. fprintf */ fprintf(fileptrOut, "Ave of %i numbers = %f \n",count,total/(double)count); fclose(fileptrIn); fclose(fileptrOut); }

After the above program is compiled execution from the Unix prompt: > more input.dat >./a.out Please enter an input filename (use path if needed):input.dat Please enter an output filename (use path if needed):output.dat > more output.dat Ave of 5 numbers = >

24-8 FILE (all caps) is a structure defined in. In CS101 we will only use this data type in the declaration of pointer variables. Examples: FILE *ptrFileIn; FILE *ptrFileOut; declares ptrFileIn and ptrFileOut as pointer variables. They both “point” to values of data-type FILE. We must have #include in our source file to use FILE pointers.

24-9 To use fscanf and fprintf you must first open a file, we use the library function fopen (defined in. ) For example, to open the file input.dat for reading, we write FILE *fileIn; fileIn = fopen("infile.dat", "r"); The first argument “infile.dat” is a string that names the file to be read. The second argument the mode, “ r ”, stands for “reading” mode only. If the file cannot be opened, fopen returns NULL. (e.g. file doesn’t exit) fopen returns a pointer to the file(in this case infile.dat ). The file pointer is the handle to the file, once the file is opened with fopen a programmer can manipulate the file with this pointer.

24-10 FILE *fileOut; fileOut = fopen("output.dat", "w"); The first argument “output.dat” is a string that names the file to be read. The second argument the mode, “ w ”, stands for “write” mode only. fopen returns a pointer to the file(in this case ( output.dat ). Example: If an already existing file is opened with mode “w” the old contents are discarded.

24-11 FILE *fileOut; fileOut = fopen("out.dat", "a"); The first argument “out.dat” is a string that names the file the you to which you want to write. The writing takes place at the end of the original file. If the file out.dat already exists it will not be destroyed as in the case for “w” mode. fopen returns a pointer to the file(in this case ( out.dat ). Example:

23-12 When we open a file such as infile.dat we need to specify in advance whether we want to read from the file, write to the file, append to the file. A file can be opened in one mode, closed and then reopened in another mode. ModeIf the file existsIf the file doesn’t exist “r”Opens the file for readingfopen returns NULL “w”Opens file for writing and deletes contents creates a new file “a”Opens a file for appending (writing at the end of the file) creates a new file

24-13 fprintf function is in. It is the same as the printf function except that the first argument for fprintf is a file pointer: fprintf(ptrFileOut, ctrlString, expression(s)); where ctrlString contains the format specifiers, and expression(s) can contain one or more variable names and constants. Examples: fprintf(ptrFileOut, "%i ", intVlaue); fprintf(ptrFileOut, "%f ", a * b);

24-14 fscanf function is also in. It is the same as the scanf function except that the first argument for fscanf is a file pointer; i.e., fscanf(ptrFileIn, ctrlString,address(es)); Examples - fscanf(ptrFileIn, "%lf", &dataValue); fscanf(ptrFileIn, "%c%s%i", &ch, str, &num); - returns an int value equal to number of items successfully read from file (e.g., 0, 1, 2, 3 in last example), or returns the value of EOF if end of file is encountered before any values are read in. Thus, fscanf can be used as an input check just like scanf.

24-15 To close a file that is opened, you must use fclose. The function expects one argument, a pointer to FILE. The function fclose returns zero if it successfully closes the file and EOF otherwise.(e.g file doesn’t exist) FILE *fileIn; fileIn = fopen("infile.dat", "r"); … fclose(fileIn); When a program terminates, all open files are automatically closed.