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.

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

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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Introduction Data files –Can be created, updated,
 2007 Pearson Education, Inc. All rights reserved C File Processing.
Chapter 11 C File Processing Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
File I/O.
CS1061 C Programming Lecture 18: Sequential File Processing A. O’Riordan, 2004, 2007 updated.
Console and File I/O - Basics Rudra Dutta CSC Spring 2007, Section 001.
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.
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.
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.
Programming Languages -1 (Introduction to C) files Instructor: M.Fatih AMASYALI
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.
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.
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 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 11: Data Files and File Processing Files and streams Creating a sequential access file Reading data from a sequential access file Using fgetc()
Chapter 11 File Processing. Objectives In this chapter, you will learn: –To be able to create, read, write and update files. –To become familiar with.
C Program Design C File Processing 主講人:虞台文. Content Introduction Data Hierarchy Files and Streams Creating a Sequential-Access File Reading Data from.
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:
chap8 Chapter 12 Files (reference: Deitel ’ s chap 11)
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()”
ME-2221 COMPUTER PROGRAMMING Lecture 18 FILE OPERATIONS Department of Mechanical Engineering A.H.M Fazle Elahi Khulna University of engineering & Technology.
1 CSC103: Introduction to Computer and Programming Lecture No 28.
Adv. UNIX:fp/101 Advanced UNIX v Objectives of these slides: –a more detailed look at file processing in C Special Topics in Comp. Eng.
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.
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.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
Silberschatz and Galvin  C Programming Language Kingdom of Saudi Arabia Ministry of Higher Education Al-Majma’ah University College of Education.
Chapter 4 File Processing
Lecture 11 File input/output
TMF1414 Introduction to Programming
File Access (7.5) CSE 2031 Fall July 2018.
File I/O.
Plan for the Day: I/O (beyond scanf and printf)
CS111 Computer Programming
CSE1320 Files in C Dr. Sajib Datta
File Input/Output.
What you need for the 1st phase of project
CSE1320 Files in C Dr. Sajib Datta
File I/O We are used to reading from and writing to the terminal:
Beginning C Lecture 11 Lecturer: Dr. Zhao Qinpei
Programming and Data Structure
Text and Binary File Processing
File Input and Output.
File Handling.
Accessing Files in C Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Fundamental of Programming (C)
C Preprocessing File I/O
FILE handeling.
Module 12 Input and Output
CSc 352 File I/O Saumya Debray Dept. of Computer Science
Professor Jodi Neely-Ritz University of Florida
File I/O We are used to reading from and writing to the terminal:
Presentation transcript:

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 of characters Each line has 0 – 255 characters ending in a carriage return Carriage return is converted to operating systems end of line character Used for text files –Binary stream – data read and written unchanged (in binary format) Used for binary files

File names Names must adhere to operating system rules –8.3 for DOS –256 characters for Windows 95, 98, NT –256 for most UNIX systems –Letters, Numbers, and Special characters are allowed in names

File names Strings are used for file names Recall the slash (\) has special meaning to C Examples: char * fileName = “c:\\winnt\\myfile.txt”; char * fileName = “c:/winnt/myfile.txt”;

Opening files You cannot use a file until it has been opened The fopen function is located in stdio.h –Accepts 2 argument: filename (string) mode (string) –Returns a file pointer

Available modes What mode a file is opened in dictates how the file is opened –Files may be opened for reading or writing –Files may be opened in text mode or binary mode

Available Text Modes ModeMeaningSpecial Conditions rReadingReturns NULL if file does not exist wWritingIf file does not exist, it is created If file exists, data is erased aAppendingIf file does not exist, it is created If file exists, data appended to end *Modes are available for to open a file for read and write

Available Binary Modes ModeMeaningSpecial Conditions rbReadingReturns NULL if file does not exist wbWritingIf file does not exist, it is created If file exists, data is erased abAppendingIf file does not exist, it is created If file exists, data appended to end *Modes are available for to open a file for read and write

The fopen function #include void main( void ) { FILE * fptr; char * fileName = "c:/temp/test.txt"; char * mode = "r"; if ((fptr = fopen(fileName, mode)) == NULL) { printf("Opening file %s caused an error.", fileName); exit(1); } //Processing continues }

Types of files Two main types –Text files Can be viewed by user Read by other applications (db, spreadsheet, other programs in other languages, etc.) –Binary files Cannot be viewed by user Read by other programs

Types of files Three main structures to data files –Specified number of records Usually specified in first record for loops –Trailer or sentinel signal Usually specified in last record while loops –End-of-file indicator Every file contains a system-readable end-of-file feof function in conjunction with while loops

Text file output Functions in stdio.h –fprintf most common –fputs –putc no fputc must include file name

fprintf fprintf works like the printf function –fprintf allows you to tell the compiler what file you are using –printf automatically uses stdout prototype comparison: int fprintf(FILE *fp, char *fmt, …); int printf(char *fmt, …);

fprintf example #include int main( void ) { FILE * fp; int x = 3; if ((fp = fopen("c:/test.txt", "w")) == NULL) { printf("Error opening file!"); exit(1); } fprintf(fp, "The value of x is: %d", x); return 0; }

Text file input Functions in stdio.h –fscanf most common –fgets –getc and fgetc identical must include file name

fscanf fscanf works like the scanf function –fscanf allows you to tell the compiler what file you are using –scanf automatically uses stdout prototype comparison: int fscanf(FILE *fp, char *fmt, …); int scanf(char *fmt, …);

fscanf example //Assume test.txt is a text file with an integer in it: #include int main( void ) { FILE * fp; int x = 0; if ((fp = fopen("c:/test.txt", "r")) == NULL) { printf("Error opening file!"); exit(1); } fscanf(fp, "%d", &x); printf("The value of x is: %d", x); return 0; }

Binary file output fwrite() function prototype: –fwrite(void *buf, int size, int count, FILE *fp) buf points to the data size tells the size of the objects to be written count tells how many objects are to be written fp tells which file is being written to

fwrite example #include int main( void ) { FILE * fp; int x[5] = {1, 2, 3, 4, 5}; if ((fp = fopen("c:/test.dat", "wb")) == NULL) { printf("Error opening file!"); exit(1); } fwrite(x, sizeof(int), 5, fp); return 0; }

Binary file input fread() function prototype: –fread(void *buf, int size, int count, FILE *fp) buf points to the data size tells the size of the objects to be read count tells how many objects are to be read fp tells which file is being read from

fread example #include int main( void ) { FILE * fp; int x[5], i; if ((fp = fopen("c:/test.dat", "rb")) == NULL) { printf("Error opening file!"); exit(1); } fread(x, sizeof(int), 5, fp); for (i = 0; i < 5; i++) printf("%d ", x[i]); return 0; }

Closing and flushing files All files should be flushed and closed prior to program termination –flushing writes the data from the buffer to the actual file files written in blocks, streams written in characters do data is buffered prior to being written –closing literally closes the file – needs to be reopened to be used again –all files are closed by the compiler at program termination, but this process should be handled by the programmer

fclose fclose will flush and close a file –prototype: int fclose(FILE *fp); fcloseall will flush and close all but the standard files –Prototype int fcloseall( ); –Not ANSI standard –will not close stdout, stdin, etc.