Intermediate Programming, Spring 2017*

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

System Software Experiment 1 Lecture 7

CSci 4061 Introduction to Operating Systems. Input/Output: High-level

Introduction to Computer Programming Lecture 18 Binary Files

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

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

Intermediate Programming, Spring 2017*

CS240: Programming in C

Darshan Institute of Engineering & Technology for Diploma Studies Unit 6

Intermediate Programming, Spring 2017*

C-Refresher: Session 10 Disk IO

PROGRAMMAZIONE I A.A. 2017/2018

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

25.2 Opening and Closing a File

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

CSI 402 Systems Programming LECTURE 4 FILES AND FILE OPERATIONS

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

Week 9 Lecture 3. Binary Files. Week 9

ENG120. Misc. Topics

Input/Output and the Operating Systems

Computer programming

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

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

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

Lecture 9: File Processing. Quazi Rahman

Computer System and programming in C

Ch 11. C File Processing (review)

C Syntax Arrays and Loops Math Strings Structures Pointers File I/O. Final Review CS Prof. Jonathan Ventura. Prof. Jonathan Ventura Final Review

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

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

Standard C Library Functions

Intermediate Programming, Spring 2017*

Chapter 11 File Processing

Introduction to file management

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

File Handling. Reference:

C Programming Language

CS240: Programming in C

C File Processing: One-Page Summary

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

UNIX System Programming

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

Slide Set 8. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng

Intermediate Programming, Spring 2017*

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

MARKS: Q1 /20 /15 /15 /15 / 5 /30 TOTAL: /100

C PROGRAMMING. Characters and Strings File Processing Exercise

Naked C Lecture 6. File Operations and System Calls

CS 261 Fall Mike Lam, Professor. Structs and I/O

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

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

Lecture 03 Bits, Bytes and Data Types

Systems Programming. 08. Standard I/O Library. Alexander Holupirek

Standard File Pointers

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

Lecture6 File Processing

CSC209H Lecture 3. Dan Zingaro. January 21, 2015

Accessing Files in C. Professor Hugh C. Lauer CS-2303, System Programming Concepts

File I/O. Preprocessor Macros

2009 S2 COMP File Operations

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

Computer Programming Unit v

CSCI 171 Chapter Outlines

C Basics And Concepts Input And Output

Fundamentals of Programming. Lecture 10 Hamed Rasifard

HIGH LEVEL FILE PROCESSING

Lecture 8: Structs & File I/O

ECE264 Spring 2014 Exam 2, March 11, 2014

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

Intermediate Programming, Spring 2017*

File I/O. Last updated 10/30/18

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

File and Console I/O. CS449 Spring 2016

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

Organization of a file

CAAM 420 Notes Chapter 2: The C Programming Language

CSE 410: Systems Programming

UNIT- 2. Binary File

EM108 Software Development for Engineers

CS246 Spring14 Programming Paradigm Files, Pipes and Redirection

TEXT FILE I/O. printf("this data will be written to the screen.\n"); printf("x = %d, y = %f\n", x, y);

Input / Output Functions

Intermediate Programming, Spring 2017*

Advanced C Programming Topics

Input/Output: Advanced Concepts

Lab # 4. Files & Queues in C

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

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

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

UNIX input and output

C: How to Program. Week /June/18

Files. Eric McCreath

C File System File Functions EXPERIMENT 1.2

CSE 333 SECTION 3. POSIX I/O Functions

STRUCTURES & FILE IO

Programming & Data Structure

File IO and command line input CSE 2451

LANGUAGE OF THE C. C: Part 6. Listing 1 1 #include <stdio.h> 2 3 int main(int argc, char *argv[]) PROGRAMMING

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #47. File Handling

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

Transcription:

600.120 Intermediate Programming, Spring 2017* Misha Kazhdan *Much of the code in these examples is not commented because it would otherwise not fit on the slides. This is bad coding practice in general and you should not follow my lead on this.

Outline Binary File I/O

Recall When working with file handles we: 1. Create a file handle 2. Access the file s contents 3. Close the handle for( int i=0 ;i<100 ; i++ ) values[i] = rand(); FILE* fp = fopen( "foo.txt", "w" ); for( int i=0 ; i<100 ; i++ ) fprintf( fp, "%u\n", values[i] );

Recall When working with file handles we: 1. Create a file handle fopen with the file-name and mode "w" for (ASCII) write "r" for (ASCII) read for( int i=0 ;i<100 ; i++ ) values[i] = rand(); FILE* fp = fopen( "foo.txt", "w" ); for( int i=0 ; i<100 ; i++ ) fprintf( fp, "%u\n", values[i] );

Recall When working with file handles we: 2. Access the file s contents fprintf with file handle, format string, and values for (ASCII) write fscanf with file handle, format string, and addresses for (ASCII read) for( int i=0 ;i<100 ; i++ ) values[i] = rand(); FILE* fp = fopen( "foo.txt", "w" ); for( int i=0 ; i<100 ; i++ ) fprintf( fp, "%u\n", values[i] );

Recall When working with file handles we: 3. Close the handle fclose with file handle for( int i=0 ;i<100 ; i++ ) values[i] = rand(); FILE* fp = fopen( "foo.txt", "w" ); for( int i=0 ; i<100 ; i++ ) fprintf( fp, "%u\n", values[i] );

Limitations If we write out a list of 100 ints to a file Q: How big would the file be? Q: How would we get the 7 th value? for( int i=0 ;i<100 ; i++ ) values[i] = rand(); FILE* fp = fopen( "foo.txt", "w" ); for( int i=0 ; i<100 ; i++ ) fprintf( fp, "%u\n", values[i] );

Limitations If we write out a list of 100 ints to a file Q: How big would the file be? A: 1056 bytes We have 32 bits per int: range of [0,4,000,000,000] 9-10 decimal places (average) +1 for the "\n" 10 100 11 100 bytes Size is not fixed But the values always require 400 bytes in memory!!! >>./a.out >> ls -l foo.txt -rw-------. 1 misha users 1056 Mar 30 23:17 foo.txt >> for( int i=0 ;i<100 ; i++ ) values[i] = rand(); FILE* fp = fopen( "foo.txt", "w" ); for( int i=0 ; i<100 ; i++ ) fprintf( fp, "%u\n", values[i] );

Limitations If we write out a list of 100 ints to a file Q: How would we get the 7 th value? A: 64 characters in We have 32 bits per int 10 6 11 6 bytes Offset is not fixed We cannot jump to the 7 th value since we don't know the sizes of the values that come before fscanf one int at a time until we get the 7 th value >>./a.out >> more foo.txt 1804289383 846930886 1681692777 1714636915 1957747793 424238335 719885386... >> for( int i=0 ;i<100 ; i++ ) values[i] = rand(); FILE* fp = fopen( "foo.txt", "w" ); for( int i=0 ; i<100 ; i++ ) fprintf( fp, "%u\n", values[i] );

Until now, all files we ve accessed in C have been plain text files Write: Convert everything to a string of characters that is written to the file. Read: Convert everything from a string of characters that is read from the file Non-text files are known as binary files in C Write: perform a bit-by-bit copy from memory to the file Read: perform a bit-by-bit copy from the file to memory

As with text files, we: 1. Open the file 2. Access the file s contents 3. Close the file for( int i=0 ; i<100 ; i++ ) values[i] = rand(); FILE* fp = fopen( "foo.dat", "wb" ); fwrite( values, sizeof(int), 100, fp );

FILE* fopen( const char* filename, const char* mode ); 1. Open the file Use fopen to create a file handle: filename: name of the file mode: mode of I/O To open a file in binary mode, add the "b" flag in the string of mode characters * Returns a file pointer (or NULL if the fopen failed) * The file extension does not affect how the file is opened. for( int i=0 ; i<100 ; i++ ) values[i] = rand(); FILE* fp = fopen( "foo.dat", "wb" ); fwrite( values, sizeof(int), 100, fp );

size_t fwrite( const void* ptr, size_t sz, size_t count, FILE* fp ); 2. Access the file s contents Use fwrite to write to a binary file: ptr: starting address of data to write out sz: size of a data element count: number of a data element fp: file handle to write to Returns the number of elements written for( int i=0 ; i<100 ; i++ ) values[i] = rand(); FILE* fp = fopen( "foo.dat", "wb" ); fwrite( values, sizeof(int), 100, fp );

size_t fread( void* ptr, size_t sz, size_t count, FILE* fp ); 2. Access the file s contents Use fread to read from a binary file: ptr: starting address of data to read into sz: size of a data element count: number of a data element fp: file handle to read from Returns the number of elements read for( int i=0 ; i<100 ; i++ ) values[i] = rand(); FILE* fp = fopen( "foo.dat", "wb" ); fwrite( values, sizeof(int), 100, fp );

int fclose( FILE* fp ); 3. Close the file Use fclose to close the file handle fp: file handle Returns 0 if the stream was closed for( int i=0 ; i<100 ; i++ ) values[i] = rand(); FILE* fp = fopen( "foo.dat", "wb" ); fwrite( values, sizeof(int), 100, fp );

If we write out a list of 100 ints to a file Q: How big would the file be? A: 400 bytes. Always! >>./a.out >> ls -l foo.dat -rw-------. 1 misha users 400 Mar 30 23:17 foo.dat >> for( int i=0 ; i<100 ; i++ ) values[i] = rand(); FILE* fp = fopen( "foo.dat", "wb" ); fwrite( values, sizeof(int), 100, fp );

As we read/write data, the FILE pointer tracks our position in the file: In some cases, we would like to change our position in the file: Writing: To over-write something that was previously written Reading: To jump to where the data we are interested in resides

int fseek( FILE* fp, long int offset, int whence ); Use fseek to change the position of the file pointer fp: file pointer offset: number of bytes to move Could be positive or negative, depending on whether we move forward or back whence: where we move from: SEEK_SET: beginning of the file SEEK_CUR: current position SEEK_END: end of the file Returns zero if the change succeeded

(part 1) unsigned int getvalue( FILE* fp, size_t idx ) if( fseek( fp, sizeof(unsigned int)*idx, SEEK_SET ) ) fprintf( stderr, "Failed to seek\n" ); return -1; unsigned int v; if( fread( &v, sizeof( int ), 1, fp )!=1 ) fprintf( stderr, "Failed to read\n" ); return -1; return v; (part 2) FILE* fp = fopen( "foo.dat", "rb" ); if(!fp ) fprintf( stderr, "Failed to open\n" ); return -1; printf( "%u\n", getvalue( fp, 7 ) );

size_t fwrite( const void* ptr, size_t sz, size_t count, FILE* fp ); size_t fread( void* ptr, size_t sz, size_t count, FILE* fp ); Note: The fread/fwrite functions only need to be able to read the bits/bytes from memory, they don't need to know the data-type stored. We are not limited to reading/writing integers and numbers typedef struct... MyStruct; unsigned MyStruct values[100]; FILE* fp = fopen( "foo.dat", "rb" ); fread( values, sizeof(mystruct), 100, fp );

size_t fwrite( const void* ptr, size_t sz, size_t count, FILE* fp ); size_t fread( void* ptr, size_t sz, size_t count, FILE* fp ); Note: If the Note: struct contains pointers, the address is written out, not the contents at the address!!! The fread/fwrite functions only need to be able to read the bits/bytes from memory, they don't need to know the data-type stored. We are not limited to reading/writing integers and numbers typedef struct... MyStruct; unsigned MyStruct values[100]; FILE* fp = fopen( "foo.dat", "rb" ); fread( values, sizeof(mystruct), 100, fp );

Piazza Resources section Resources tab Exercise 8-1