File Handling Spring 2013Programming and Data Structure1.

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,
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.
C Programming - Lecture 3 File handling in C - opening and closing. Reading from and writing to files. Special file streams stdin, stdout & stderr. How.
File Management in C. Console oriented Input/Output Console oriented – use terminal (keyboard/screen) scanf(“%d”,&i) – read data from keyboard printf(“%d”,i)
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.
CS1061 C Programming Lecture 17: Steams and Character I/O A. O’Riordan, 2004.
Chapter 11 C File Processing Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Programming in C #2 Input / Output.
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.
Files Programs and data are stored on disk in structures called files Examples Turbo C++ - binary file Word binary file lab1.c - text file lab1.data.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students C File Input and Output Checking input for errors.
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.
Lone Leth Thomsen Input / Output and Files. April 2006Basis-C-8/LL2 sprintf() and sscanf() The functions sprintf() and sscanf() are string versions of.
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.
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.
 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.
File IO and command line input CSE 2451 Rong Shi.
CSEB114: Principle of programming Chapter 11: Data Files & File Processing.
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:
Lecture 8a: File I/O BJ Furman 21MAR2011. Learning Objectives Explain what is meant by a data stream Explain the concept of a ‘file’ Open and close files.
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.
CNG 140 C Programming (Lecture set 10) Spring Chapter 10 Data Files.
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:
CS 261 – Recitation 7 Spring 2015 Oregon State University School of Electrical Engineering and Computer Science.
GAME203 – C Files stdio.h C standard Input/Output “getchar()”
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 Programming 1. 2 What is a File? Is a block of arbitrary information, or resource for storing information, which is available to a computer program.
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 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.
File I/O.
CS 261 – Recitation 7 Fall 2013 Oregon State University
CSC215 Lecture Input and Output.
Plan for the Day: I/O (beyond scanf and printf)
Programming and Data Structures
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.
File Management in C.
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 We are used to reading from and writing to the terminal:
CSC215 Lecture Input and Output.
FILE HANDLING IN C.
Programming and Data Structure
File Input and Output.
File Handling.
Programming in C Input / Output.
File Handling in C.
Chapter 5 File Handling in C
Module 12 Input and Output
CS1100 Computational Engineering
C Programming - Lecture 3
CSc 352 File I/O Saumya Debray Dept. of Computer Science
I/O CS580U - Fall 2018.
File I/O We are used to reading from and writing to the terminal:
Presentation transcript:

File Handling Spring 2013Programming and Data Structure1

File handling in C In C we use FILE * to represent a pointer to a file. fopen is used to open a file. It returns the special value NULL to indicate that it couldn't open the file. Spring 2013Programming and Data Structure2 FILE *fptr; char filename[]= "file2.dat"; fptr= fopen (filename,"w"); if (fptr == NULL) { printf (“ERROR IN FILE CREATION”); /* DO SOMETHING */ }

Modes for opening files The second argument of fopen is the mode in which we open the file. There are three "r" opens a file for reading "w" creates a file for writing - and writes over all previous contents (deletes the file so be careful!) "a" opens a file for appending - writing on the end of the file “rb” read binary file (raw bytes) “wb” write binary file Spring 2013Programming and Data Structure3

The exit() function Sometimes error checking means we want an "emergency exit" from a program. We want it to stop dead. In main we can use "return" to stop. In functions we can use exit to do this. Exit is part of the stdlib.h library Spring 2013Programming and Data Structure4 exit(-1); in a function is exactly the same as return -1; in the main routine

Usage of exit( ) FILE *fptr; char filename[]= "file2.dat"; fptr= fopen (filename,"w"); if (fptr == NULL) { printf (“ERROR IN FILE CREATION”); /* Do something */ exit(-1); } Spring 2013Programming and Data Structure5

Writing to a file using fprintf( ) fprintf( ) works just like printf except that its first argument is a file pointer. Spring 2013Programming and Data Structure6 FILE *fptr; fptr= fopen ("file.dat","w"); /* Check it's open */ fprintf (fptr,"Hello World!\n");

Reading Data Using fscanf( ) FILE *fptr; fptr= fopen (“input.dat”,“r”); /* Check it's open */ if (fptr==NULL) { printf(“Error in opening file \n”); } fscanf(fptr,“%d%d”,&x,&y); Spring 2013Programming and Data Structure7 We also read data from a file using fscanf( ) input.dat x=20 y=30

Reading lines from a file using fgets( ) We can read a string using fgets ( ). Spring 2013Programming and Data Structure8 FILE *fptr; char line [1000]; /* Open file and check it is open */ while (fgets(line,1000,fptr) != NULL) { printf ("Read line %s\n",line); } fgets( ) takes 3 arguments, a string, a maximum number of characters to read and a file pointer. It returns NULL if there is an error (such as EOF).

Closing a file We can close a file simply using fclose( ) and the file pointer. Spring 2013Programming and Data Structure9 FILE *fptr; char filename[]= "myfile.dat"; fptr= fopen (filename,"w"); if (fptr == NULL) { printf ("Cannot open file to write!\n"); exit(-1); } fprintf (fptr,"Hello World of filing!\n"); fclose (fptr); Opening Access closing

Three special streams Three special file streams are defined in the header stdin reads input from the keyboard stdout send output to the screen stderr prints errors to an error device (usually also the screen) What might this do? Spring 2013Programming and Data Structure10 fprintf (stdout,"Hello World!\n");

An example program Spring 2013Programming and Data Structure11 #include main() { int i; fprintf(stdout,"Give value of i \n"); fscanf(stdin,"%d",&i); fprintf(stdout,"Value of i=%d \n",i); fprintf(stderr,"No error: But an example to show error message.\n"); } Give value of i 15 Value of i=15 No error: But an example to show error message. Display on The screen

Input File & Output File redirection One may redirect the input and output files to other files (other than stdin and stdout). Usage: Suppose the executable file is a.out Spring 2013Programming and Data Structure12 $./a.out out.dat 15 in.dat Give value of i Value of i=15 out.dat No error: But an example to show error message. Display screen

Reading and Writing a character A character reading/writing is equivalent to reading/writing a byte. int getchar( ); int fgetc(FILE *fp); int putchar(int c); int fputc(int c, FILE *fp); Example: char c; c=getchar( ); putchar(c); Spring 2013Programming and Data Structure13

Example: use of getchar() and putchar() Spring 2013Programming and Data Structure14 #include main() { int c; printf("Type text and press return to see it again \n"); printf("For exiting press \n"); while((c=getchar( ))!=EOF) putchar(c); } End of file

Command Line Arguments Command line arguments may be passed by specifying them under main( ). int main(int argc, char *argv[ ]); Spring 2013Programming and Data Structure15 Argument Count Array of Strings as command line arguments including the command itself.

Example: Reading command line arguments Spring 2013Programming and Data Structure16 #include int main(int argc,char *argv[]) { FILE *ifp,*ofp; int i,c; char src_file[100],dst_file[100]; if(argc!=3){ printf("Usage:./a.out \n"); exit(0); } else{ strcpy(src_file,argv[1]); strcpy(dst_file,argv[2]); }

Example: Contd. Spring 2013Programming and Data Structure17 if((ifp=fopen(src_file,"r"))==NULL) { printf("File does not exist.\n"); exit(0); } if((ofp=fopen(dst_file,"w"))==NULL) { printf("File not created.\n"); exit(0); } while((c=getc(ifp))!=EOF){ putc(c,ofp); } fclose(ifp); fclose(ofp); }./a.out s.dat d.dat argc=3./a.out s.dat d.dat argv

Getting numbers from strings Once we've got a string with a number in it (either from a file or from the user typing) we can use atoi or atof to convert it to a number The functions are part of stdlib.h Spring 2013Programming and Data Structure18 char numberstring[]= "3.14"; int i; double pi; pi= atof (numberstring); i= atoi ("12"); Both of these functions return 0 if they have a problem

Example: Averaging from Command Line Spring 2013Programming and Data Structure19 #include int main(int argc,char *argv[]) { float sum=0; int i,num; num=argc-1; for(i=1;i<=num;i++) sum+=atof(argv[i]); printf("Average=%f \n",sum/(float) num); } $./a.out Average=