Files Chapter 8.

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

Week 5 Part I Kyle Dewey. Overview Exam will be back Thursday New office hour More on functions File I/O Project #2.
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,
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
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.
C File Processing - I Math 130 B Smith: Eval: 3. Energy and excitement helped for avg graphics. Gen interest. A bit wordy, henced redundant in some parts.
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.
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.
1 CSE1301 Computer Programming: Lecture 21 File I/O.
C Language Summary HTML version. I/O Data Types Expressions Functions Loops and Decisions Preprocessor Statements.
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
CSE1301 Computer Programming: Lecture 19 File I/O
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students C File Input and Output Checking input for errors.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 7P. 1Winter Quarter File I/O in C Lecture.
C Programming – Part 6 File Input and Output
1. Introduction File Declaration and Initialization Creating and Opening File Closing File EOF Reading from and Writing into a File Extra : Random Access.
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.
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:
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.
CECS 130 EXAM 1.  int main() { printf (“%c %c \n", 'a', 65); printf ("%d %ld\n", 1977, L); printf (" %10d \n", 1977); printf ("%010d \n", 1977);
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:
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Input and Output.
GAME203 – C Files stdio.h C standard Input/Output “getchar()”
1 CSC103: Introduction to Computer and Programming Lecture No 27.
Files A collection of related data treated as a unit. Two types Text
CSCI N305: C Programming Copyright ©2006  Department of Computer & Information Science File Handling in C.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the basic properties and characteristics of external files ❏ To.
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.
ECE 103 Engineering Programming Chapter 29 C Strings, Part 2 Herbert G. Mayer, PSU CS Status 7/30/2014 Initial content copied verbatim from ECE 103 material.
UniMAP SemI-11/12EKT120: Computer Programming1 Files.
User-Written Functions
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
Structured Programming II
EKT120: Computer Programming
File Access (7.5) CSE 2031 Fall July 2018.
Chapter 18 I/O in C.
File I/O.
Plan for the Day: I/O (beyond scanf and printf)
Strings A string is a sequence of characters treated as a group
CS111 Computer Programming
CSE1320 Files in C Dr. Sajib Datta
File Input/Output.
Computer Programming Lecture 15 Text File I/O
Programming in C Input / Output.
Programming in C Input / Output.
Files I/O, Streams, I/O Redirection, Reading with fscanf
CSE1320 Strings Dr. Sajib Datta
بنام خدا زبان برنامه نویسی C (21814( Lecture 11 Pointers
IPC144 Week 10 – Lesson 2 Working with Files
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
FILE HANDLING IN C.
File Input and Output.
File Handling.
File Access (7.5) CSE 2031 Fall February 2019.
Programming in C Input / Output.
Files.
File Handling in C.
Arrays.
Chapter 11 Files chap8.
EECE.2160 ECE Application Programming
ICS103: Programming in C 6: Pointers and Modular Programming
Professor Jodi Neely-Ritz University of Florida
Presentation transcript:

Files Chapter 8

Files Information can be stored on disk in the form of a data FILE Data files can be read by programs, written to by programs and updated by programs A file is similar to a table with rows and columns A row is referred to as a record A column is referred to as a field

Use of Files in C Program Declare file (once, at beginning of program) Open file (once or more, normally at beginning of program) Read from or write to file (or both) Close file (once or more, normally at end of pgm)

Step 1: Declare file(s) Need to declare a pointer which will hold the address of the file. Example: FILE *fp_name, *fp_rept;  the pointers fp_name and fp_rept will each hold the starting disk address of a file

Step 2: Open file using fopen fopen command establishes a connection with a file and assigns the address of the file to a pointer. 1st parameter: name of physical file used by operating system 2nd parameter: access mode where: r: read the file (if file not found by OS, then open statement will fail) w: write to the file (if file already exists its contents will be overwritten) a: append, i.e. add at end of existing file (if file not found by OS, file will be created)

Step 2 (ctd): fopen Example: fp_name = fopen(“names.dat”, “r”);   fp_rept = fopen(“report.dat”, “w”); Opens OS file names.dat in read mode and puts the address of the file in fp_name. Opens OS file report.dat in write mode and puts the address of the file in fp_rept. NOTE: A file must be opened before it can be used!

Step 2 (ctd): fopen Value returned by fopen should always be checked because if a file cannot be opened, ‘NULL’ is returned by fopen function Example: fp_name = fopen(“names.dat”, “r”); if (fp_name == NULL) printf(“names.dat file not opened!”);

Step 3: Read data from file fscanf is used to read from a file. Format is almost exactly the same as the scanf statement except must provide file pointer as 1st parameter. Example: fscanf(fp_name,“%s;%s;%d\n”,lname, fname, &studno);  This will read 2 strings separated by a semi-colon and assign them to the arrays fname and lname, will then skip over a semicolon and then assign an integer value to studno

Step 3: Write data to file fprintf writes to a file. Example: fprintf(fp_rept, “%s %s:%d\n”, fname, lname, studno); Writes a record to the file report.dat containing 2 strings separated by a space, followed by a colon and an integer value for studno and a newline (each record in a file must be ended with a newline character)

Step 4: Close file using fclose Close file to ensure that all data sent to the file is written and saved and that file is left in stable state by OS. Example: fclose(fp_name); fclose(pf_rept);  

End-of-file Condition fscanf returns an integer which indicates the number of variables returned. When there is no more data, fscanf returns -1. This is equal to the standard constant EOF. You should check for this when reading through a file. Normally want to read file until fscanf returns –1 and then close file.

Sample file program #include <stdio.h> #include <stdlib.h> void main () { char first[31], last[31]; /* first and last name */ int studno, i; FILE *fp_name, *fp_rept; fp_name = fopen("names.dat","r"); fp_rept = fopen("report.dat","w"); if ( (fp_name == NULL) || (fp_rept == NULL)) { printf ("Cannot open file\n"); exit(1); } while (fscanf(fp_name,"%s %s %d\n", last, first, &studno)!=-1) fprintf (fp_rept, "%d: %s %s\n", studno, first, last); fclose (fp_name); fclose (fp_rept); }