CS115_ 2006-2007SENEM KUMOVA METIN 1 FILE OPERATIONS.

Slides:



Advertisements
Similar presentations
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.
Advertisements

By Senem Kumova Metin 1 POINTERS + ARRAYS + STRINGS REVIEW.
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.
Programming In C++ Spring Semester 2013 Lecture 10 Programming In C++, Lecture 10 By Umer Rana.
Chapter 11 C File Processing Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Kernel File Interface operating systems (or programming I/O in Unix)
File I/O.
CS1061 C Programming Lecture 18: Sequential File Processing A. O’Riordan, 2004, 2007 updated.
 Types of files  Command line arguments  File input and output functions  Binary files  Random access.
N305: C Programming Copyright ©2006  Department of Computer & Information Science File Handling in C.
Lone Leth Thomsen Input / Output and Files. April 2006Basis-C-8/LL2 sprintf() and sscanf() The functions sprintf() and sscanf() are string versions of.
1 Lecture09: Global Variables, File I/O, and Variable-Length Arguments 5/16/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
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.
File Handling In C By - AJAY SHARMA. We will learn about- FFile/Stream TText vs Binary Files FFILE Structure DDisk I/O function OOperations.
 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.
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.
Structured Programming Instructor: Prof. K. T. Tsang
CSEB114: Principle of programming Chapter 11: Data Files & File Processing.
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 Lecture09: File I/O 11/19/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
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 Files By C. Shing ITEC Dept Radford University.
FILE IO in ‘C’ by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
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:
C Programming Lecture 12 : File Processing
Structured Programming Approach Module VIII - Additional C Data Types File Handling Prof: Muhammed Salman Shamsi.
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 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.
Files. FILE * u In C, we use a FILE * data type to access files. u FILE * is defined in /usr/include/stdio.h u An example: #include int main() { FILE.
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.
 Dynamic Memory Allocation  Linked Lists  void main() {{ ◦ FILE *file; ◦ char file_name[] = “file.txt”; ◦ if ((file = fopen(file_name, "w")) ==
By C. Shing ITEC Dept Radford University
Chapter 4 File Processing
Lecture 11 File input/output
File I/O.
Session #5 File I/O Bit Masks, Fields & Bit Manipulations
File Handling in C.
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
What you need for the 1st phase of project
File Handling in C.
C: Primer and Advanced Topics
CSE1320 Files in C Dr. Sajib Datta
File I/O We are used to reading from and writing to the terminal:
Lecture 15 Files.
CSC215 Lecture Input and Output.
תכנות מערכות בשפת C עבודה עם קבצים מכללת אורט כפר-סבא אורי וולטמן
Text and Binary File Processing
File Input and Output.
File Access (7.5) CSE 2031 Fall January 2019.
Accessing Files in C Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
File Handling in C.
File Access (7.5) CSE 2031 Fall February 2019.
Files.
File Handling in C.
FILE handeling.
Module 12 Input and Output
ETE 132 Lecture 8 By Munirul Haque.
CSc 352 File I/O Saumya Debray Dept. of Computer Science
File I/O.
Professor Jodi Neely-Ritz University of Florida
File I/O We are used to reading from and writing to the terminal:
Files Chapter 8.
Presentation transcript:

CS115_ SENEM KUMOVA METIN 1 FILE OPERATIONS

CS115_ SENEM KUMOVA METIN 2 FILE OPERATIONS Create and open the file Close the file Write a character to the file Read a character from the file Check if you are at the end of file Read a string from the file Write a string to the file Write to the file in formatted form Read from the file in formatted form Write an array to the file Read an array from the file Reach the data from a specified place in the file (access the file randomly)

CS115_ SENEM KUMOVA METIN 3 OPEN A FILE Each file to be opened must have a file pointer “FILE” is the structure used to declare a file pointer (in stdio.h) fopen() function is used to open the file. –will return a file pointer or a NULL –if it returns a NULL, then it means that file cannot be opened FILE *fopen(char *filename, char *mode) EXAMPLE : #include int main() { FILE *f;// FILE * fp,x, filepointer; f = fopen("test.txt","w"); /* opens test.txt in write (w) mode */ return 0; } mode in double quotas filename

CS115_ SENEM KUMOVA METIN 4 OPEN A FILE :MODES fp= fopen(“x.txt”, “…”) rOpen for reading r+Open for reading and writing wOpen for writing and create the file if it does not exist. If the file exists then make it blank. w+Open for reading and writing and create the file if it does not exist. If the file exists then make it blank. aOpen for appending(writing at the end of file) and create the file if it does not exist. a+Open for reading and appending and create the file if it does not exist.

CS115_ SENEM KUMOVA METIN 5 CLOSE A FILE : fclose() #include main() { FILE * di; if( (di=fopen(“test.txt”,”r+”))==NULL) { puts(“FILE CANNOT BE OPENED!”); exit();} ……. fclose(di); // fclose(filepointer) }

CS115_ SENEM KUMOVA METIN 6 PUTC() && GETC() putc(character, filepointer) // puts a character to the file character=getc(filepointer) // gets a character from the file EXAMPLE : #include #include // for exit() function main() {FILE * di; char kr; if( (di=fopen("test.txt", "a"))==NULL) { printf("cannot open file\n"); exit(0); } while(1) {kr=getc(stdin); if(kr!='q') { putc(kr,di); putc(kr,stdout);} else { fclose(di); exit(0); }} fclose(di); }

CS115_ SENEM KUMOVA METIN 7 fputs() && fgets() fputs(string, filepointer) // puts the string to file EXAMPLE: #include #include // for exit main() { FILE * di; char filename[80], string[256]; printf("Please write the file name:\n"); gets(filename); if( (di=fopen(filename, "w"))==NULL) { printf("cannot open file\n"); exit(0); } while(1) { printf("NAME and SURNAME: "); gets(string); if(string[0]== '\0') break; else {fputs(string, di); fputs("\n",di);} } fclose(di); } fgets(string,sizeof string, filepointer) // gets the string from file EXAMPLE: #include #include // for exit main() { FILE * di; char filename[80], string[256]; printf("Please write the file name:\n"); gets(filename); if( (di=fopen(filename, “r"))==NULL) { printf("cannot open file\n"); exit(0); } while(!feof(di)) { fgets(string, 256, di); fputs(string, stdout); // if(!feof(di)) fputs(string,stdout); } fclose(di); }

CS115_ SENEM KUMOVA METIN 8 fprintf && fscanf() int fprintf(FILE *stream, char *format, args..) /*writes formatted data to the file */ int fscanf(FILE *stream, char *format, args..) /*reads formatted data from the file */ EXAMPLE: main() {FILE * di; char str [100]; if( (di=fopen("tt.txt", "w"))==NULL) { printf("cannot open file\n"); exit(0); } fprintf(di,"%s world \n","hello"); fclose(di); if( (di=fopen("tt.txt", "r"))==NULL) { printf("cannot open file\n"); exit(0); } while(!feof(di)) {fscanf(di,"%s", str); printf("%s", str);} fclose(di);}

CS115_ SENEM KUMOVA METIN 9fprintf int fprintf(FILE *stream, char *format, args..) /*writes formatted data to the file */ int fscanf(FILE *stream, char *format, args..) /*reads formatted data from the file */ EXAMPLE: CALCULATE THE RADIAN and COSINUS #include main() {FILE * di; int angle; float radian, h; if( (di=fopen(“cosinus.aci”, "w"))==NULL) { printf("cannot open file\n"); exit(0); } for (angle=0; angle<=360; angle++) {radian=(float) angle*3.14/180; h=cos(radian); fprintf(di,”%d, %f, %f\n”,angle,radian,h); } fclose(di);}

CS115_ SENEM KUMOVA METIN 10fscanf() int fprintf(FILE *stream, char *format, args..) /*writes formatted data to the file */ int fscanf(FILE *stream, char *format, args..) /*reads formatted data from the file */ EXAMPLE: READ cosinus.aci FILE #include main() {FILE * di; int angle; float radian, h; char str [100]; if( (di=fopen(“cosinus.aci”, “r"))==NULL) { printf("cannot open file\n"); exit(0); } while(!feof(di)) { fscanf(di,”%d %f %f”, &angle, &radian, &h); printf(”%d, %f, %f\n”, angle, radian, h); } fclose(di); }

CS115_ SENEM KUMOVA METIN 11 fread() && fwrite() // fread(array_name,sizeof array element,total data size,filepointer) // fwrite (array_name,sizeof array element,total data size,filepointer) int main() { FILE *f; int buf1[5]={'a', 'b', 'c', 'd', 'e'}; int buf2[3]; if(( f = fopen("test.txt","w")) ==NULL) exit(0); fwrite(buf1,sizeof(buf1[0]),5,f); fwrite(buf1,sizeof(buf1[0]),5,stdout); fclose(f); printf("\n"); if(( f = fopen("test.txt","r")) ==NULL) exit(0); fread(buf2,sizeof(buf2[0]),3,f); fwrite(buf2,sizeof(buf2[0]),3,stdout); fclose(f); }

CS115_ SENEM KUMOVA METIN 12 Accessing a File Randomly ftell(file_pointer) –returns the current value of file position indicator –returned value represents the number of bytes from the beginning of the file fseek(file_pointer,offset, place) –sets the file position indicator to a value that represents offset bytes from place – place can be SEEK_SET:beginning of the file SEEK_CUR :current position SEEK_END:end of the file

CS115_ SENEM KUMOVA METIN 13 Accessing a File Randomly :EXAMPLE #include main() {char name[100] ="sample code"; FILE * fp; fp= fopen("text.tx","w+"); fprintf(fp,"%s", &name[0]); printf(“%d\n", ftell(fp)); fseek(fp,-1,SEEK_CUR); printf(“%d\n", ftell(fp)); printf("%c\n",getc(fp)); printf("%d\n", ftell(fp)); fseek(fp,-2,SEEK_CUR); printf("%d\n", ftell(fp)); printf("%c\n",getc(fp)); printf("%d\n", ftell(fp)); fseek(fp,0,SEEK_SET); printf("%d\n", ftell(fp)); printf("%c\n",getc(fp)); printf("%d\n", ftell(fp));} OUTPUT : e 11 9 d 10 0 s 1

CS115_ SENEM KUMOVA METIN 14 FILE OPERATIONS fopen(): create and open files fclose(): closes files putc(): writes a character to file getc(): reads a character from file feof(): checks if pointer has arrived to the end of file fgets(): reads a string from file fputs(): writes a string to file fprintf(): writes to file in a formatted form fscanf(): reads from file in a formatted form fwrite(): writes an array to a file fread(): reads an array from a file fseek(): reaches the data from a specified place in file