20/03/01 Sudeshna Sarkar, CSE, IIT Kharagpur1 File Handling in C Lecture 17c 20/3/01.

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.
Files in C Rohit Khokher.
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.
File Management in C. Console oriented Input/Output Console oriented – use terminal (keyboard/screen) scanf(“%d”,&i) – read data from keyboard printf(“%d”,i)
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Introduction Data files –Can be created, updated,
File I/O.
Command line arguments. – main can take two arguments conventionally called argc and argv. – Information regarding command line arguments are passed to.
V-1 University of Washington Computer Programming I File Input/Output © 2000 UW CSE.
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.
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.
Introduction to Programming Using C Files. 2 Contents Files Working with files Sequential files Records.
CSC 211 Data Structures Lecture 32
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 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 3 Input and Output
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.
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:
Fundamentals and History of C  C is developed by Dennis Ritchie  C is a structured programming language  C supports functions that enables easy maintainability.
GAME203 – C Files stdio.h C standard Input/Output “getchar()”
Files A collection of related data treated as a unit. Two types Text
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.
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.
6/9/2016Course material created by D. Woit 1 CPS 393 Introduction to Unix and C START OF WEEK 10 (C-4)
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.
University of Washington Computer Programming I
Chapter 7 Text Input/Output Objectives
Command Line Arguments
File Access (7.5) CSE 2031 Fall July 2018.
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
File Input/Output.
CSE1320 Files in C Dr. Sajib Datta
Computer Programming Lecture 15 Text File I/O
Programming in C Input / Output.
What you need for the 1st phase of project
File Management in C.
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
FILE HANDLING 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.
Files.
File Handling in C.
Chapter 5 File Handling in C
Chapter 12: File I/O.
ETE 132 Lecture 8 By Munirul Haque.
Quick Review EECS May 2019.
CS1100 Computational Engineering
Chapter 11 Files chap8.
Files Chapter 8.
Presentation transcript:

20/03/01 Sudeshna Sarkar, CSE, IIT Kharagpur1 File Handling in C Lecture 17c 20/3/01

20/03/01 Sudeshna Sarkar, CSE, IIT Kharagpur2 Introduction zFiles are places where data can be stored permanently. zSome programs expect the same set of data to be fed as input every time it is run. yCumbersome. yBetter if the data are kept in a file, and the program reads from the file. zPrograms generating large volumes of output. yDifficult to view on the screen. yBetter to store them in a file for later viewing/ processing

20/03/01 Sudeshna Sarkar, CSE, IIT Kharagpur3 Basic File Operations zOpening a file zReading data from a file zWriting data to a file zClosing a file

20/03/01 Sudeshna Sarkar, CSE, IIT Kharagpur4 Opening a File zA file must be “opened” before it can be used. FILE *fp; : fp = fopen (filename, mode); yfp is declared as a pointer to the data type FILE. yfilename is a string - specifies the name of the file. yfopen returns a pointer to the file which is used in all subsequent file operations. ymode is a string which specifies the purpose of opening the file: “r” :: open the file for reading only “w” :: open the file for writing only “a” :: open the file for appending data to it

20/03/01 Sudeshna Sarkar, CSE, IIT Kharagpur5 Contd. zPoints to note: ySeveral files may be opened at the same time. yFor the “w” and “a” modes, if the named file does not exist, it is automatically created. yFor the “w” mode, if the named file exists, its contents will be overwritten.

20/03/01 Sudeshna Sarkar, CSE, IIT Kharagpur6 Examples FILE *in, *out ; in = fopen (“mydata.dat”, “r”) ; out = fopen (“result.dat”, “w”); FILE *empl ; char filename[25]; scanf (“%s”, filename); empl = fopen (filename, “r”) ;

20/03/01 Sudeshna Sarkar, CSE, IIT Kharagpur7 Closing a File zAfter all operations on a file have been completed, it must be closed. yEnsures that all file data stored in memory buffers are properly written to the file. zGeneral format: fclose (file_pointer) ; FILE *xyz ; xyz = fopen (“test”, “w”) ; ……. fclose (xyz) ;

20/03/01 Sudeshna Sarkar, CSE, IIT Kharagpur8 Read/Write Operations on Files zThe simplest file input-output (I/O) function are getc and putc. zgetc is used to read a character from a file and return it. char ch; FILE *fp; ….. ch = getc (fp) ; ygetc will return an end-of-file marker EOF, when the end of the file has been reached. zputc is used to write a character to a file. char ch; FILE *fp; …… putc (c, fp) ;

20/03/01 Sudeshna Sarkar, CSE, IIT Kharagpur9 Example :: convert a text file to all UPPERCASE main() { FILE *in, *out ; char c ; in = fopen (“infile.dat”, “r”) ; out = fopen (“outfile.dat”, “w”) ; while ((c = getc (in)) != EOF) putc (toupper (c), out); fclose (in) ; fclose (out) ; }

20/03/01 Sudeshna Sarkar, CSE, IIT Kharagpur10 Contd. zWe can also use the file versions of scanf and printf, called fscanf and fprintf. zGeneral format: fscanf (file_pointer, control_string, list) ; fprintf (file_pointer, control_string, list) ; zExamples: fscanf (fp, “%d %s %f”, &roll, dept_code, &cgpa) ; fprintf (out, “\nThe result is: %d”, xyz) ;

20/03/01 Sudeshna Sarkar, CSE, IIT Kharagpur11 Some Points zHow to check EOF condition when using fscanf? yUse the function feof if (feof (fp)) printf (“\n Reached end of file”) ; zHow to check successful open? yFor opening in “r” mode, the file must exist. if (fp == NULL) printf (“\n Unable to open file”) ;

20/03/01 Sudeshna Sarkar, CSE, IIT Kharagpur12 Example typedef struct { int roll; char dept_code[6]; float cgpa; } STUD; main() { FILE *stud; STUD s; float sum = 0.0; int count = 0; stud = fopen (“stud.dat”, “r”) ; while (1) { if (feof (stud)) break; fscanf (stud, “%d %s %f”, &s.roll, s.dept_code, &s.cgpa); count ++; sum += s.cgpa; } printf (“\nThe average cgpa is %f”, sum/count); fclose (stud); }

20/03/01 Sudeshna Sarkar, CSE, IIT Kharagpur13 Arguments to main () zCommand line arguments are parameters supplied to a program, when the program is invoked. cc myfile.c cc xyz.c -lm netscape average zHow do these parameters get into the program? yEvery C program has a main function. ymain can take two arguments conventionally called argc and argv. yInformation regarding command line arguments are passed to the program through argc and argv.

20/03/01 Sudeshna Sarkar, CSE, IIT Kharagpur14 Echoing the command line arguments int main (int argc, char *argv[]) { int i; printf (“argc = %d\n”, argc) ; for (i=0; i<argc; ++i) printf (“argv[%d] = %s\n”, i,argv[i]) ; return 0; } $ a.out how many argc = 3 argv[0] = a.out argv[1] = how argv[2] = many

20/03/01 Sudeshna Sarkar, CSE, IIT Kharagpur15 Example :: convert a text file to all UPPERCASE, using command line arguments main (int argc, char *argv[ ] { FILE *in, *out ; char c ; in = fopen (argv[1], “r”) ; out = fopen (argv[2], “w”) ; while ((c = getc (in)) != EOF) putc (toupper (c), out); fclose (in) ; fclose (out) ; } Run this program as: a.out old new