The fopen command can open an external file in C language. If the file is exist, the file pointer is not NULL. Otherwise, the file pointer is NULL.

Similar documents
Darshan Institute of Engineering & Technology for Diploma Studies Unit 6

C-Refresher: Session 10 Disk IO

Mode Meaning r Opens the file for reading. If the file doesn't exist, fopen() returns NULL.

Computer Programming Unit v

C Programming Language

EM108 Software Development for Engineers

25.2 Opening and Closing a File

Unit 6 Files. putchar(ch); ch = getc (fp); //Reads single character from file and advances position to next character

File I/O. Arash Rafiey. November 7, 2017

Fundamentals of Programming. Lecture 10 Hamed Rasifard

Laboratory: USING FILES I. THEORETICAL ASPECTS

CSI 402 Systems Programming LECTURE 4 FILES AND FILE OPERATIONS

Lecture 7: Files. opening/closing files reading/writing strings reading/writing numbers (conversion to ASCII) command line arguments

File IO and command line input CSE 2451

Computer programming

CSI 402 Lecture 2 Working with Files (Text and Binary)

PROGRAMMAZIONE I A.A. 2017/2018

CSE2301. Introduction. Streams and Files. File Access Random Numbers Testing and Debugging. In this part, we introduce

Data File and File Handling

Standard File Pointers

CMPE-013/L. File I/O. File Processing. Gabriel Hugh Elkaim Winter File Processing. Files and Streams. Outline.

System Software Experiment 1 Lecture 7

7/21/ FILE INPUT / OUTPUT. Dong-Chul Kim BioMeCIS UTA

Input/Output and the Operating Systems

C for Engineers and Scientists: An Interpretive Approach. Chapter 14: File Processing

M.CS201 Programming language

UNIT- 2. Binary File

Week 9 Lecture 3. Binary Files. Week 9

CE Lecture 11

File (1A) Young Won Lim 11/25/16

COMP1917: 15 File IO

Intermediate Programming, Spring 2017*

HIGH LEVEL FILE PROCESSING

UNIX System Programming

Programming Language B

Introduction to Computer Programming Lecture 18 Binary Files

UNIT-V CONSOLE I/O. This section examines in detail the console I/O functions.

File Handling. Reference:

Chapter 5, Standard I/O. Not UNIX... C standard (library) Why? UNIX programmed in C stdio is very UNIX based

Preprocessing directives are lines in your program that start with `#'. The `#' is followed by an identifier that is the directive name.

Lecture 9: File Processing. Quazi Rahman

File I/O BJ Furman 23OCT2010

Programming & Data Structure

C Programming 1. File Access. Goutam Biswas. Lect 29

EECS2031. Modifiers. Data Types. Lecture 2 Data types. signed (unsigned) int long int long long int int may be omitted sizeof()

A Crash Course in C. Steven Reeves

Data Files. Computer Basics

File I/O. Preprocessor Macros

Lab # 4. Files & Queues in C

CS113: Lecture 7. Topics: The C Preprocessor. I/O, Streams, Files

DS: CS Computer Sc & Engg: IIT Kharagpur 1. File Access. Goutam Biswas. ect 29

Chapter 12. Text and Binary File Processing. Instructor: Öğr. Gör. Okan Vardarlı. Copyright 2004 Pearson Addison-Wesley. All rights reserved.

File Handling. 21 July 2009 Programming and Data Structure 1

CSI 402 Lecture 2 (More on Files) 2 1 / 20

Files. Programs and data are stored on disk in structures called files Examples. a.out binary file lab1.c - text file term-paper.

C File Processing: One-Page Summary

Procedural Programming & Fundamentals of Programming

CP2 Revision. theme: file access and unix programs

Programming Fundamentals

Content. Input Output Devices File access Function of File I/O Redirection Command-line arguments

Standard I/O in C, Computer System and programming in C

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 12

System Programming. Standard Input/Output Library (Cont d)

2009 S2 COMP File Operations

Introduction to file management

Computer System and programming in C

CSC209H Lecture 3. Dan Zingaro. January 21, 2015

CS246 Spring14 Programming Paradigm Files, Pipes and Redirection

Files and Streams Opening and Closing a File Reading/Writing Text Reading/Writing Raw Data Random Access Files. C File Processing CS 2060

Input / Output Functions

Engineering program development 7. Edited by Péter Vass

Input/Output: Advanced Concepts

Quick review of previous lecture Ch6 Structure Ch7 I/O. EECS2031 Software Tools. C - Structures, Unions, Enums & Typedef (K&R Ch.

ECE 264 Exam 2. 6:30-7:30PM, March 9, You must sign here. Otherwise you will receive a 1-point penalty.

CS240: Programming in C

UNIT IV-2. The I/O library functions can be classified into two broad categories:

8. Structures, File I/O, Recursion. 18 th October IIT Kanpur

a = ^ ^ ^ ^ ^ ^ ^ b = c = a^b =

SAE1A Programming in C. Unit : I - V

Chapter 10: File Input / Output

Standard C Library Functions

CS240: Programming in C

Ch 11. C File Processing (review)

Introduction to Computer and Program Design. Lesson 6. File I/O. James C.C. Cheng Department of Computer Science National Chiao Tung University

Programming in C. Session 8. Seema Sirpal Delhi University Computer Centre

File Processing. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

A stream is infinite. File access methods. File I/O in C++ 4. File input/output David Keil CS II 2/03. The extractor and inserter form expressions

Organization of a file

fopen() fclose() fgetc() fputc() fread() fwrite()

C PROGRAMMING. Characters and Strings File Processing Exercise

Week 8: Arrays and File I/O. BJ Furman 21OCT2009

Chapter 12. Files (reference: Deitel s chap 11) chap8

Procedural Programming

Lecture 8. Dr M Kasim A Jalil. Faculty of Mechanical Engineering UTM (source: Deitel Associates & Pearson)

Structured programming

C PROGRAMMING Lecture 6. 1st semester

Today s Learning Objectives

Computer Programming: Skills & Concepts (CP1) Files in C. 18th November, 2010

UNIX Shell. The shell sits between you and the operating system, acting as a command interpreter

CSCI 171 Chapter Outlines

Transcription:

File Input / Output File open FILE *fopen( filename, mode ); The fopen command can open an external file in C language. If the file is exist, the file pointer is not NULL. Otherwise, the file pointer is NULL. { FILE *fp; if ( (fp=fopen("test.txt","r")) == NULL) printf("\nopen file error!"); else printf("\nopen file success!"); Lecture 9, C Language,Page 1/13

File open mode r Open a text file for reading w Open a text file for writing, if the file does not exist then create a new one a Append to the text file r+ Read and Write w+ Read and Write, if the file does not exist then create a new one a+ Append and Write rb Open a binary file for reading wb Open a binary file for writing ab Append to a binary file Text file Reading and Closing After you finish processing the file, you need to close it. int fclose(file *); /* success return 0, error return 1 */ Get a character form file char fgetc(file *); Lecture 9, C Language,Page 2/13

#include <stdlib.h> { FILE *fp; char ch; if ( (fp=fopen("test.txt","r")) == NULL) { printf("\nopen file error!"); exit(1); else printf("\nopen file success!\n"); while ( (ch=fgetc(fp))!= EOF ) /* Read until EOF */ printf("%c",ch); fclose(fp); Output open file success! line 1 line 2 line 3 Lecture 9, C Language,Page 3/13

Text file Reading, Writing and Closing #include <stdlib.h> { FILE *fp1,*fp2; /* fp1 source file */ char ch; /* fp2 target file */ /* open file for reading */ if ( (fp1=fopen("test.txt","r")) == NULL) { printf("\nopen file error!"); exit(1); else printf("\nopen file success!\n"); fp2=fopen("copy.txt","w"); /* open file for writing */ while ( (ch=getc(fp1))!= EOF ) { putchar(ch); /* Output to screen */ fputc(ch,fp2); /* Output to file */ fclose(fp1); fclose(fp2); Lecture 9, C Language,Page 4/13

fprintf for file writing printf(file *, %d.%f.., var1, var2); #include <stdlib.h> { FILE *fp1; /* fp1 source file */ char ch; /* fp2 target file */ fp1=fopen("test.txt","w"); /* open file for writing */ fprintf(fp1,"this is message %d\n",1); fprintf(fp1,"this is message %d\n",2); fclose(fp1); Output This is message 1 This is message 2 fscanf for file reading End of file fscanf(..%d..%f,&var1,&var2); int feof(file *) Lecture 9, C Language,Page 5/13

#include <stdlib.h> { FILE *fp1; /* fp1 source file */ char ch; /* fp2 target file */ char ivalue[50]; fp1=fopen("test.txt","r"); /* open file for reading */ while (!feof(fp1)) { fscanf(fp1,"%s",ivalue); printf("\n%s",ivalue); fclose(fp1); Input This is message 1 This is message 2 Output This is message 1 This is message 2 Lecture 9, C Language,Page 6/13

Exercise 9.1 Write a program that can encrypt and decrypt a document. Use command line input e for encryption and d for decryption. Ex1 e source.txt enc.txt Ex1 d enc.txt dec.txt Source.txt this is message 1 this is message 2 enc.txt Encryption function unsigned char encrypt(unsigned char ch) { ch=ch<<1; return ch; Decryption function unsigned char decrypt(unsigned char ch) { ch=ch>>1; return ch; Lecture 9, C Language,Page 7/13

Output: C:\My Documents\notes\C\lecture9>ex1 e source.txt enc.txt open file success! C:\My Documents\notes\C\lecture9>ex1 d enc.txt dec.txt open file success! this is message 1 this is message 2 C:\My Documents\notes\C\lecture9> Binary file writing int fwrite(data pointer, size of data, count, FILE *); #include <conio.h> struct score { char name[20]; int marks; ; { FILE *fp; /* fp1 source file */ score var; fp=fopen("test.bin","wb"); /* open file for writing */ do { printf("\nplease enter name :"); fflush(stdin); Lecture 9, C Language,Page 8/13

gets(var.name); printf("please enter marks :"); fflush(stdin); scanf("%d",&var.marks); fwrite(&var,sizeof(var),1,fp); printf("do you wish to continue?"); while (getche()=='y'); fclose(fp); Output Please enter name :name 1 Please enter marks :10 Do you wish to continue?y Please enter name :name 2 Please enter marks :20 Do you wish to continue?y Please enter name :name 3 Please enter marks :30 Do you wish to continue?y Please enter name :name 4 Please enter marks :40 Do you wish to continue?n Lecture 9, C Language,Page 9/13

Binary file reading int fread(data pointer, size of data, count, FILE *); #include <conio.h> struct score { char name[20]; int marks; ; { FILE *fp; /* fp1 source file */ score var; fp=fopen("test.bin","rb"); /* open file for reading */ while (fread(&var,sizeof(var),1,fp)!= 0) { printf("name : %s\n",var.name); printf("marks : %d\n",var.marks); fclose(fp); Output Name : name 1 marks : 10 Name : name 2 marks : 20 Name : name 3 marks : 30 Name : name 4 marks : 40 Lecture 9, C Language,Page 10/13

fseek int fseek(file *stream, long offset, int origin); origin 0 From the beginning of the file 1 From the current position 2 From the end of the file #include <conio.h> struct score { char name[20]; int marks; ; { FILE *fp; /* fp1 source file */ score var; fp=fopen("test.bin","rb"); /* open file for writing */ if (fseek(fp,sizeof(var)*2,2)!=0) { printf("random access file pointer error"); if (fread(&var,sizeof(var),1,fp)!= 0) { printf("name : %s\n",var.name); printf("marks : %d\n",var.marks); else printf("file reading error"); fclose(fp); Lecture 9, C Language,Page 11/13

Return the current position int ftell(file *); Reset the position to zero void rewind(file *); #include <conio.h> struct score { char name[20]; int marks; ; { FILE *fp; /* fp file pointer */ score var; fp=fopen("test.bin","rb"); /* open file for writing */ clrscr(); printf("\nfp=%d",ftell(fp)); /* return current position */ if (fseek(fp,sizeof(var)*2,1)!=0) /* go to somewhere */ printf("random access file pointer error"); printf("\nfp=%d",ftell(fp)); /* return current position */ if (fread(&var,sizeof(var),1,fp)!= 0) { printf("\nname : %s\n",var.name); printf("marks : %d\n",var.marks); else printf("file reading error"); printf("\nfp=%d",ftell(fp)); /* return current position */ Lecture 9, C Language,Page 12/13

rewind(fp); /* reset to position zero */ printf("\nfp=%d",ftell(fp)); /* return current position */ fclose(fp); Output : FP=0 FP=44 Name : test3 marks : 30 FP=66 FP=0 Lecture 9, C Language,Page 13/13