File Handling in C.

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.
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,
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.
V-1 University of Washington Computer Programming I File Input/Output © 2000 UW CSE.
20/03/01 Sudeshna Sarkar, CSE, IIT Kharagpur1 File Handling in C Lecture 17c 20/3/01.
N305: C Programming Copyright ©2006  Department of Computer & Information Science File Handling in C.
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.
Chapter 7 Files By C. Shing ITEC Dept Radford University.
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.
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
Chapter 7 Text Input/Output Objectives
RECURSION Recursion is a process where a function calls itself. main()
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.
Chapter 7 Text Input/Output Objectives
File Access (7.5) CSE 2031 Fall July 2018.
File I/O.
CSC215 Lecture Input and Output.
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.
What you need for the 1st phase of project
File Management in C.
CSE1320 Files in C Dr. Sajib Datta
CSC215 Lecture Input and Output.
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
FILE HANDLING IN C.
Programming and Data Structure
Text and Binary File Processing
File Input and Output.
File Handling.
File Access (7.5) CSE 2031 Fall January 2019.
File Access (7.5) CSE 2031 Fall February 2019.
Files.
Chapter 5 File Handling in C
Chapter 12: File I/O.
FILE handeling.
ETE 132 Lecture 8 By Munirul Haque.
Quick Review EECS May 2019.
Chapter 11 Files chap8.
CSc 352 File I/O Saumya Debray Dept. of Computer Science
Files Chapter 8.
Presentation transcript:

File Handling in C

Introduction Files are places where data can be stored permanently. Some programs expect the same set of data to be fed as input every time it is run. Cumbersome. Better if the data are kept in a file, and the program reads from the file. Programs generating large volumes of output. Difficult to view on the screen. Better to store them in a file for later viewing/ processing

Basic File Operations Opening a file Reading data from a file Writing data to a file Closing a file

Opening a File A file must be “opened” before it can be used. FILE *fp; : fp = fopen (filename, mode); fp is declared as a pointer to the data type FILE. filename is a string - specifies the name of the file. fopen returns a pointer to the file which is used in all subsequent file operations. mode 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

Contd. Points to note: Several files may be opened at the same time. For the “w” and “a” modes, if the named file does not exist, it is automatically created. For the “w” mode, if the named file exists, its contents will be overwritten.

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”) ;

Closing a File After all operations on a file have been completed, it must be closed. Ensures that all file data stored in memory buffers are properly written to the file. General format: fclose (file_pointer) ; FILE *xyz ; xyz = fopen (“test”, “w”) ; ……. fclose (xyz) ;

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

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) ; }

Contd. We can also use the file versions of scanf and printf, called fscanf and fprintf. General format: fscanf (file_pointer, control_string, list) ; fprintf (file_pointer, control_string, list) ; Examples: fscanf (fp, “%d %s %f”, &roll, dept_code, &cgpa) ; fprintf (out, “\nThe result is: %d”, xyz) ;

Some Points How to check EOF condition when using fscanf? Use the function feof if (feof (fp)) printf (“\n Reached end of file”) ; How to check successful open? For opening in “r” mode, the file must exist. if (fp == NULL) printf (“\n Unable to open file”) ;

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);

Arguments to main () Command line arguments are parameters supplied to a program, when the program is invoked. cc myfile.c cc xyz.c -lm netscape www.mailcity.com average 10 20 30 40 50 How do these parameters get into the program? Every C program has a main function. main can take two arguments conventionally called argc and argv. Information regarding command line arguments are passed to the program through argc and argv.

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