Block I/O fread and fwrite functions are the most efficient way to read or write large amounts of data. fread() – reads a specified number of bytes from.

Slides:



Advertisements
Similar presentations
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
Advertisements

As previously noted, a byte can contain a numeric value in the range Computers don't understand Latin, Cyrillic, Hindi, Arabic character sets! Alphanumeric.
Discussion: Week 3/26. Structs: Used to hold associated data together Used to group together different types of variables under the same name struct Telephone{
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Introduction Data files –Can be created, updated,
Chapter 11 C File Processing Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Structures and Unions Chapter 6. Structure A structure is an aggregate data type  Composed of two or more related variables called member/field/element.
 Types of files  Command line arguments  File input and output functions  Binary files  Random access.
An Introduction to C Programming (assuming that you already know Java; this is not an introduction to C++)
N305: C Programming Copyright ©2006  Department of Computer & Information Science File Handling in C.
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.
Stack and Heap Memory Stack resident variables include:
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.
Text and Binary File Processing 程式設計 潘仁義 CCU COMM.
1 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University.
Intro to C Part 3: © Walter Milner 2005: Slide 1 Introduction to ANSI C Part Three  Strings  Strings in structs  File handling  Linked list example.
File I/O, Project 1: List ADT Bryce Boe 2013/07/02 CS24, Summer 2013 C.
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.
Memory Layout, File I/O Bryce Boe 2013/06/27 CS24, Summer 2013 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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 11 – File Processing Outline 11.1Introduction.
12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction.
C File Processing. Objectives To be able to create, write and update files. To become familiar with sequential access file processing. To become familiar.
CSC141- Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 29 Thanks for Lecture Slides:
1 CSC103: Introduction to Computer and Programming Lecture No 28.
Gramming An Introduction to C Programming (assuming that you already know Java; this is not an introduction to C++)
1 Dynamic Memory Allocation. 2 In everything we have done so far, our variables have been declared at compile time. In these slides, we will see how to.
 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.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
Real Numbers Device driver process within the operating system that interacts with I/O controller logical record 1 logical record 2 logical record 3.
An Introduction to C Programming (assuming that you already know Java; this is not an introduction to C++)
11 C File Processing.
Stack and Heap Memory Stack resident variables include:
CS 3305A Process – Part II Lecture 4 Sept 20, 2017.
5.13 Recursion Recursive functions Functions that call themselves
Lecture 12 CIS 208 Friday, March 3rd , 2005.
TMF1414 Introduction to Programming
An Introduction to C Programming
File Handling in C.
CS111 Computer Programming
What you need for the 1st phase of project
File Handling in C.
Introduction to Programming
Hank Childs, University of Oregon
File I/O We are used to reading from and writing to the terminal:
Chapter 11 – File Processing
Pipes A pipe provides a one-way flow of data example: who | sort| lpr
Beginning C Lecture 11 Lecturer: Dr. Zhao Qinpei
Input/Output and the Operating Systems
Text and Binary File Processing
File Input and Output.
Accessing Files in C Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
File I/O (1) Prof. Ikjun Yeom TA – Hoyoun
File Handling in C.
Fundamental of Programming (C)
Your questions from last session
Line at a time I/O with fgets() and fputs()
Chapter 12: File I/O.
Structures CSE 2031 Fall April 2019.
EPSII 59:006 Spring 2004.
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:

Block I/O fread and fwrite functions are the most efficient way to read or write large amounts of data. fread() – reads a specified number of bytes from a binary file and places them into memory at the specified location. prototype for fread ( ): int fread(void *InArea, int elementSize, int count, FILE *fp); InArea – a pointer to the input area in memory. elementSize – size of a basic data element, often specified using the sizeof operator count – number of elements fp – file pointer of an open file. fread returns the number of items read.

Block I/O fwrite – writes the specified bytes of data from memory to the output file. prototype for fwrite ( ): int fwrite(void *OutArea, int elementSize, int count, FILE *fp); OutArea – a pointer to memory holding the data to be written. elementSize – size of a basic data element, often specified using the sizeof operator count – number of data elements fp – file pointer of an open file. fwrite copies elementSize * count bytes from the address specified by OutArea to the file. fwrite returns the total number of characters written.

Block input and output Here is a more efficient implementation of the cat-like program that copies standard input to the standard output. /* p12.c */ #include <stdio.h> main() { unsigned char *buff; int len = 0; int iter = 0; buff = malloc(1024); if (buff == 0) exit(1); while ((len = fread(buff, 1, 1024, stdin)) != 0) fwrite(buff, 1, len, stdout); fprintf(stderr, "%d %d \n", iter, len); iter += 1; } 0 322

Block input and output Questions: Removing the parentheses surrounding (len = fread(buff, 1, 1024, stdin)) will break the program. Explain exactly how and why things will go wrong in this case? What will happen if len in the fwrite() is replaced by 1024?