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

Similar documents
DATA STRUCTURES USING C

Stream Model of I/O. Basic I/O in C

Standard File Pointers

Arrays. C Types. Derived. Function Array Pointer Structure Union Enumerated. EE 1910 Winter 2017/18

INTRODUCTION TO C++ C FORMATTED INPUT/OUTPUT. Dept. of Electronic Engineering, NCHU. Original slides are from

C Concepts - I/O. Lecture 19 COP 3014 Fall November 29, 2017

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

Basic I/O. COSC Software Tools. Streams. Standard I/O. Standard I/O. Formatted Output

Standard C Library Functions

C Programming. Unit 9. Manipulating Strings File Processing.

Input/Output: Advanced Concepts

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

Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

PROGRAMMAZIONE I A.A. 2017/2018

C Input/Output. Before we discuss I/O in C, let's review how C++ I/O works. int i; double x;

Fundamentals of Programming

Fundamental of Programming (C)

Intermediate Programming, Spring 2017*

File IO and command line input CSE 2451

Lecture 9: File Processing. Quazi Rahman

CS102: Standard I/O. %<flag(s)><width><precision><size>conversion-code

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

Input/Output and the Operating Systems

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

CSCI 171 Chapter Outlines

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

25.2 Opening and Closing a File

Quick review of previous lecture Ch6 Structure Ch7 I/O. EECS2031 Software Tools. C - Structures, Unions, Enums & Typedef (K&R Ch.

CSC209H Lecture 3. Dan Zingaro. January 21, 2015

C programming basics T3-1 -

Pointers and File Handling

System Software Experiment 1 Lecture 7

Input / Output Functions

Should you know scanf and printf?

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

CS240: Programming in C

C mini reference. 5 Binary numbers 12

C Basics And Concepts Input And Output

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

More Arrays. Last updated 2/6/19

211: Computer Architecture Summer 2016

CMPT 102 Introduction to Scientific Computer Programming. Input and Output. Your first program

Introduction to Computer Programming Lecture 18 Binary Files

C-Refresher: Session 10 Disk IO

CSE 230 Intermediate Programming in C and C++ Input/Output and Operating System

C Programming Language

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

printf("%c\n", character); printf("%s\n", "This is a string"); printf("%s\n", string); printf("%s\n",stringptr); return 0;

Goals of this Lecture

M.CS201 Programming language

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

AN OVERVIEW OF C. CSE 130: Introduction to Programming in C Stony Brook University

I/O Management! Goals of this Lecture!

I/O Management! Goals of this Lecture!

Type Definition. C Types. Derived. Function Array Pointer Structure Union Enumerated. EE 1910 Winter 2017/18

Programming & Data Structure

File I/O, Project 1: List ADT. Bryce Boe 2013/07/02 CS24, Summer 2013 C

Memory Layout, File I/O. Bryce Boe 2013/06/27 CS24, Summer 2013 C

7/21/ FILE INPUT / OUTPUT. Dong-Chul Kim BioMeCIS UTA

Type Conversion. and. Statements

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

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

Computer Programming: Skills & Concepts (CP) Files in C

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

Programming Language B

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

The C Programming Language Part 2. (with material from Dr. Bin Ren, William & Mary Computer Science)

C: How to Program. Week /Mar/05

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

Darshan Institute of Engineering & Technology for Diploma Studies Unit 6

Advanced C Programming Topics

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

Computer Programming Unit v

8. Characters, Strings and Files

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

Ch 11. C File Processing (review)

CS246 Spring14 Programming Paradigm Files, Pipes and Redirection

Lecture 03 Bits, Bytes and Data Types

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

Files. Programs and data are stored on disk in structures called files Examples. a.out binary file lab1.c - text file term-paper.

Chapter 2 - Introduction to C Programming

C FILE Type. Basic I/O in C. Accessing a stream requires a pointer of type FILE.

CS113: Lecture 7. Topics: The C Preprocessor. I/O, Streams, Files

Course organization. Course introduction ( Week 1)

Binghamton University. CS-211 Fall Input and Output. Streams and Stream IO

Lecture 8: Structs & File I/O

CS240: Programming in C

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

This code has a bug that allows a hacker to take control of its execution and run evilfunc().

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

Function I/O. Last Updated 10/30/18

CSC 270 Survey of Programming Languages. Input and Output

Console Input and Output

C for Engineers and Scientists: An Interpretive Approach. Chapter 14: File Processing

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

Fundamentals of C. Structure of a C Program

Function I/O. Function Input and Output. Input through actual parameters. Output through return value. Input/Output through side effects

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

THE FUNDAMENTAL DATA TYPES

Fundamentals of Programming. Lecture 10 Hamed Rasifard

Transcription:

Last updated 10/30/18

Input/Output Streams Information flow between entities is done with streams Keyboard Text input stream data stdin Data Text output stream Monitor stdout stderr printf formats data for the text output stream scanf formats data for the text input stream #include <stdio.h> 2 tj

Input/Output Stream I/O formatting src: Forouzan 3 tj

Input/Output Stream I/O formatting 4 src: Forouzan tj

Input/Output printf Combines text and data and inserts it into the output stream text and data conversion is contained in double quotes data is comma separated data conversion is identified as %xxx % [flag] [min width] [precision] [size] code %+6.3Lf +12.345 sign, 6 total, 3 fractional, Long float 5 tj

Input/Output printf printf( %d%c%f, 12, a, 5.3); 12a5.300000 printf( %d %c %f, 12, a, 5.3); 12 a 5.300000 int z = 51; printf( %d %f %c %x, z, z, z, z); 51 51.00000 3 33 printf( the value of z is: %d, z); the value of z is: 51 printf{ I think %d is the value of z, z); I think 51 is the value of z Special characters are preceded by \ \n new line, \t tab, \% -%, \ printf{ I think \ %d\ is the value of z, z); I think 51 is the value of z 6 tj

Input/Output scanf Extracts data from an input stream and formats it lots of options requires a pointer (addresses) for any variables text and data conversion is contained in double quotes variable pointers are comma separated whitespace is ignored except for characters data conversion is identified as %xxx % [flag] [max width] [size] code 7 tj

Input/Output scanf 123 456 7a scanf( %d%d%d%c, &a, &b, &c, &d); a = 123, b=456, c=7, d= a 123 456 7 a scanf( %d%d%d %c, &a, &b, &c, &d); a = 123, b=456, c=7, d= a 14/15 16/17 scanf( %2d/%2d %2d/%2d, &num1, &den1, &num2, &den2); num1 = 14, den1 = 15, num2 = 16, den2 = 17 8 tj

File Formats Files can contain information in 2 different formats text Text Characters Line terminated by a newline (\n) Binary Raw bytes File terminated by end of file EOF short int 257 257A char A 0011 0010 0011 0101 0011 0111 0100 0001 0000 0001 0000 0001 0100 0001 asci 2 5 7 A binary hex 257 257A A 9 tj

File I/O - text Need to create a stream to transfer the data to/from the file from/to our program Identify the stream by name Use a pointer FILE* pointer_name; FILE* spstudentdata; 10 tj

File I/O - text Need to identify the file we are creating the stream to/from open the file assign the pointer to the opened file file_pointer = fopen( filename, mode ); spstudentdata = fopen( ee1910.dat, w ); spstudentdata = fopen( C:\\users\\tim\\winter\\ee1910.dat, r ); 11 tj

File I/O - text Open file modes r w a read only, start at beginning if does not exist error write only, start at beginning (erase all contents) if does not exist creates it append only, start at end of current data if does not exist creates it Returns address of file or NULL if an error occurs 12 tj

File I/O - text Close a file fclose(file_pointer); fclose(spstudent); 13 tj

File I/O - text #include <stdio.h> int main{ FILE* spaddress; if((spaddress = fopen( addresses.dat, r )) == NULL){ printf( Error opening file addresses.dat\n ); exit (100); // terminate program } // end if } // end main 14 tj

File I/O - text Formatting stream data fprintf(stream_pointer, control_string, vars) fscanf(stream_pointer, control_string, &vars) 15 tj

File I/O - text Read 3 integers from my_data_file.dat 16 tj

File I/O - text Read 3 integers from my_data_file.dat int j,k,l; FILE* spdata; spdata = fopen( my_data_file.dat, r ); fscanf(spdata, %d %d %d, &j, &k, &l); fclose(spdata); 17 tj

File I/O - text Append a date to my_data_file.dat in m-d-y format 18 tj

File I/O - text Append a date to my_data_file.dat in m-d-y format FILE* spdata; spdata = fopen( my_data_file.dat, a ); fprintf(spdata, %d-%d-%d, m, d, y); fclose(spdata); 19 tj

File I/O - text Read the date from my_data_file.dat m-d-y format 20 tj

File I/O - text Read the date from my_data_file.dat m-d-y format int m,d,y; FILE* spdata; spdata = fopen( my_data_file.dat, r ); fscanf(spdata, %d-%d-%d, &m, &d, &y); fclose(spdata); 21 tj

/* text_file_example.c Created by johnsontimoj Rev 0, 11/15/17 */ // read and write to a text file /////////////////////////////////////////////// // no need to recreate the stream pointer // open existing file spmydata = fopen("mydatafile.dat", "r"); #include <stdio.h> int main(void){ setbuf(stdout, NULL); // disable buffering int foo = 24; char mystring[] = "hello"; // create a stream pointer for the file FILE * spmydata; //create a new file spmydata = fopen("mydatafile.dat", "w"); // write to the file fprintf(spmydata, "%c ", 'A'); fprintf(spmydata, "%i ", foo); fprintf(spmydata, "%x ", foo); fprintf(spmydata, "%s ", mystring); // close the file fclose(spmydata); // read the data char noo; int boo; int loo; char mynewstring[6]; fscanf(spmydata, "%c", &noo); fscanf(spmydata, "%i", &boo); fscanf(spmydata, "%x", &loo); fscanf(spmydata, "%s", mynewstring); // print results printf("%c\n", noo); printf("%i\n", boo); printf("%x\n", loo); printf("%s\n", mynewstring); fclose(spmydata); return 0; } // end main 22 tj

File I/O - binary Need to identify the file we are creating the stream to/from open the file assign the pointer to the opened file file_pointer = fopen( filename, mode ); spstudentdata = fopen( ee1910.bin, rb ); spstudentdata = fopen( C:\\users\\tim\\winter\\ee1910.bin, ab ); 23 tj

File I/O - binary Open file modes rb wb ab read binary only, start at beginning if does not exist error write binary only, start at beginning (erase all contents) if does not exist creates it append binary only, start at end of current data if does not exist creates it Returns address of file or NULL if an error occurs 24 tj

File I/O - binary Open file modes r+b w+b a+b read binary (can write), start at beginning if does not exist error write binary (can read), start at beginning (erase all contents) if does not exist creates it append binary (can read), start at end of current data if does not exist creates it Returns address of file or NULL if an error occurs 25 tj

File I/O - binary Close a file fclose(file_pointer); fclose(spstudent); 26 tj

File I/O - binary #include <stdio.h> int main{ FILE* spaddress; if((spaddress = fopen( addresses.bin, rb )) == NULL){ printf( Error opening file addresses.bin\n ); exit (100); // terminate program } // end if } // end main 27 tj

File I/O - binary Formatting stream data Block format no conversions, raw bytes int fread( void* in_location_ptr, int element_size, int count, FILE* stream_ptr); returns the # of items read 28 tj

File I/O - binary read 3 integers at a time until all read FILE* spdata; int numread; int myarray[3]; spdata = fopen( my_int_file.bin, rb ); while((numread = fread(myarray, sizeof(int), 3, spdata))!= 0){ do something with the array } // end while 29 tj

File I/O - binary read 3 integers FILE* spdata; int myarray[3]; spdata = fopen( my_int_file.bin, rb ); fread(myarray, sizeof(int), 3, spdata); 30 tj

File I/O - binary Formatting stream data Block format no conversions, raw bytes int fwrite( void* out_location_ptr, int element_size, int count, FILE* stream_ptr); returns the # of items written 31 tj

File I/O - binary write 1 integer FILE* spdata; int foo; spdata = fopen( my_int_file.bin, wb ); fwrite(&foo, sizeof(int), 1, spdata); 32 tj

File I/O binary write out a student record (structure) typedef struct{ int birthyear; int id; char[40] name } STUDENT; STUDENT onestudent = {1990, 1234, { f, o, o }}; STUDENT* onestudent_ptr = &onestudent int numout; FILE* spdata; numout = fwrite(onestudent_ptr, sizeof(student), 1, spdata); 33 tj

/* data_file_example.c Created by johnsontimoj Rev 0, 11/15/17 */ // read and write to a data file #include <stdio.h> int main(void){ setbuf(stdout, NULL); // disable buffering // create a stream pointer for the file FILE * spmydata; //create a new file spmydata = fopen("mydatafile.bin", "wb"); // write to the file int i; for(i = 0; i < 20; i++){ fwrite(&i, sizeof(int), 1, spmydata); } // close the file fclose(spmydata); /////////////////////////////////////////////// // no need to recreate the stream pointer // open existing file spmydata = fopen("mydatafile.bin", "rb"); // read the data int mynewdata[20]; for(i = 0; i < 20; i++){ fread(&mynewdata[i], sizeof(int), 1, spmydata); } // print results for(i = 0; i < 20; i++){ printf("%i ", mynewdata[i]); } fclose(spmydata); return 0; } // end main 34 tj