Standard File Pointers

Similar documents
File IO and command line input CSE 2451

Input / Output Functions

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

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

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

System Software Experiment 1 Lecture 7

DATA STRUCTURES USING C

CS246 Spring14 Programming Paradigm Files, Pipes and Redirection

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

Topic 8: I/O. Reading: Chapter 7 in Kernighan & Ritchie more details in Appendix B (optional) even more details in GNU C Library manual (optional)

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

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

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

Standard C Library Functions

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

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

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

Advanced C Programming Topics

PROGRAMMAZIONE I A.A. 2017/2018

2009 S2 COMP File Operations

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

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

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

EM108 Software Development for Engineers

C Basics And Concepts Input And Output

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

Fundamentals of Programming. Lecture 15: C File Processing

Introduction to file management

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

8. Characters, Strings and Files

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

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

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

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

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

Input/Output and the Operating Systems

Input/Output: Advanced Concepts

CS240: Programming in C

Programming Fundamentals

Darshan Institute of Engineering & Technology for Diploma Studies Unit 6

File I/O. Preprocessor Macros

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

File Access. FILE * fopen(const char *name, const char * mode);

Programming in C. Session 8. Seema Sirpal Delhi University Computer Centre

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

25.2 Opening and Closing a File

Programming & Data Structure

Computer Programming: Skills & Concepts (CP1) Files in C. 18th November, 2010

C File Processing: One-Page Summary

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

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

Contents. A Review of C language. Visual C Visual C++ 6.0

C programming basics T3-1 -

Organization of a file

COMP1917: 15 File IO

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

Day14 A. Young W. Lim Tue. Young W. Lim Day14 A Tue 1 / 15

The Design of C: A Rational Reconstruction (cont.)

File Handling in C. EECS 2031 Fall October 27, 2014

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

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

ENG120. Misc. Topics

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

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

Binghamton University. CS-220 Spring Includes & Streams

Ch 11. C File Processing (review)

Lecture 9: File Processing. Quazi Rahman

C Programming 1. File Access. Goutam Biswas. Lect 29

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

C mini reference. 5 Binary numbers 12

The Design of C: A Rational Reconstruction (cont.)" Jennifer Rexford!

Computer Programming Unit v

File Handling. 21 July 2009 Programming and Data Structure 1

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

Data File and File Handling

DS: CS Computer Sc & Engg: IIT Kharagpur 1. File Access. Goutam Biswas. ect 29

Day14 A. Young W. Lim Thr. Young W. Lim Day14 A Thr 1 / 14

Goals of C "" The Goals of C (cont.) "" Goals of this Lecture"" The Design of C: A Rational Reconstruction"

Engineering program development 7. Edited by Péter Vass

Continued from previous lecture

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

Lecture6 File Processing

Goals of this Lecture

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

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

HIGH LEVEL FILE PROCESSING

I/O Management! Goals of this Lecture!

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

I/O Management! Goals of this Lecture!

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

Lab # 4. Files & Queues in C

Physical Files and Logical Files. Opening Files. Chap 2. Fundamental File Processing Operations. File Structures. Physical file.

Announcements. Strings and Pointers. Strings. Initializing Strings. Character I/O. Lab 4. Quiz. July 18, Special character arrays

Naked C Lecture 6. File Operations and System Calls

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

File I/O Lesson Outline

Chapter 11 File Processing

C-Refresher: Session 10 Disk IO

Chapter 10. File Processing 248 FILE PROCESSING

File I/O BJ Furman 23OCT2010

Transcription:

1 Programming in C

Standard File Pointers Assigned to console unless redirected Standard input = stdin Used by scan function Can be redirected: cmd < input-file Standard output = stdout Used by printf function Can be redirected: cmd > output-file Standard error = stderr Can be specified in fputs function Can be redirected: cmd 2> output-file 2

Files A collection of related data treated as a unit Two types Text Binary Stored in secondary storage devices Buffer Temporary storage area that holds data while they are being transferred to or from memory. 3

Text Files Data is mainly stored as human-readable characters Each line of data ends with a newline character = \n C666666666 20 8.55 A222222222 50 12.5 F333333333 45 8.5 B444444444 50 9 G555555555 30 6 E111111111 40 10 H777777777 40 12 D888888888 40 11.11 I999999999 45 15 4

User File Steps 1. Declare a file pointer variable Program connection to external user file 2. Open the file Creates a structure to store information needed for processing file and buffer area(s) Makes file pointer connection to structure 3. Use functions for input and/or output Handles movement of data between program and buffer and between buffer and external device 4. Close the file Writes the buffer to file if necessary Frees up memory associated with file 5

1. File Pointer Declaration FILE * variable-name-list; Defines variables of type FILE*, file pointer Pointer is undefined unless initialized If not initialized to another value, initialize to NULL Examples: Following slides will use fp for file pointer 6

2. fopen FILE * fopen(char * filename, char * mode) Parameters filename string that supplies the name of the file as known to the external world Default path is current directory 7 mode Meaning r w a Open file for reading If file exists, the marker is positioned at beginning If file does not exist, error returned Open text file for writing If file exists, it is emptied If file does not exist, it is created Open text file for append If file exists, the marker is positioned at the end If file does not exist, it is created

fopen FILE * fopen(char * filename, char * mode) Return If successful, file pointer If not successful, NULL Always check return If not successful, print error message and exit 8

fopen FILE * fopen(char * filename, char * mode) Examples 9

4. fclose int fclose(file *fp) Used to close a file when no longer needed Prevents associated file from being accessed again Guarantees that data stored in the stream buffer is written to the file Releases the FILE structure so that it can be used with another file Frees system resources, such as buffer space Returns zero on success, or EOF on failure 10

fclose Examples: 11

3. Input/Output Functions Formatted Input fscanf Formatted Output fprintf String Input fgets String Output fputs 12

Formatted Input Functions Read and convert a stream of characters and store the converted values in a list of variables found in the address list scanf scanf ("format string", address list); Reads text data from standard input fscanf fscanf(fp, "format string", address list); Reads input from the specified file 13

Formatted Output Functions Displays output in human readable form printf printf ("format string", value list); Writes to standard output or standard error file fprintf fprintf (fp, "format string", value list); Writes to the specified file. 14

Formatting Input/Output Functions Format strings Consist of three types of data 1. Whitespace 2. Text characters 3. Field specification These may be repeated 15

Field Specification Consists of % character, a conversion code, and other formatting instructions With one exception (*), each field specification must have a matching parameter in the parameter list that follows the string The type in the field specification and the type of the parameter must match Can have up to six elements for output Can have up to five elements for input; precision is not allowed 16

Field Specification Elements scanf/fscanf % flag Max width size conversion code * do not store data - left justify + sign (+ or -) space if positive 0 zero padding h short l long int l double (scan only) L long double d, f, c, s, e, E, x, X, i, u, g, G % flag Max width precision size conversion code printf / fprintf 17

Field Specification Examples %d %7d %+d %lf %7.2lf 18

String Input Watch size of string Must be large enough to hold entered string! + \n perhaps + \0 C gives no warnings of such issues! 19

Character Input Functions getchar: Read one character at a time from a text stream int getchar( ) Reads the next character from the standard input stream and returns its value Return type is int! 20

Character Output Functions putchar : Write one character at a time int putchar (int outchar) Writes the parameter to standard output If successful, returns the character written 21

String Input - gets char *gets (char *strptr) Inputs a line (terminated by a newline) from standard input Converts newline to \0 If successful, returns the string 22

String Input - fgets char *fgets (char * strptr, int size, FILE *fp) Inputs characters from the specified file pointer through \n or until specifed size is reached Puts newline (\n) in the string if size not reached!!! Appends \0 at the end of the string If successful, returns the string Example for stdin: 23

String Output - puts int puts (const char *strptr) Takes a null-terminated string from memory and writes it to standard output Writes \n in place of \0 24

String Output - fputs int fputs (const char *strptr, FILE *fp) Takes a null-terminated string from memory and writes it to the specified file pointer Drops \0 Programmer's responsibility: Make sure the newline is present at the appropriate place(s) Example for stdout: 25

End of File Controlled Loops feof int feof(file *fp) Function to check if end of file has been reached. For an end of file controlled loop Read before the loop Test for end of file: while (!feof(ptr)) Inside loop: Process Read at the bottom of the loop 26

Programming in C T H E E N D 27