File IO and command line input CSE 2451

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

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

Standard File Pointers

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

Input/Output and the Operating Systems

System Software Experiment 1 Lecture 7

Input / Output Functions

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

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

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

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

CSC209H Lecture 3. Dan Zingaro. January 21, 2015

File I/O. Preprocessor Macros

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

Organization of a file

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

Introduction to file management

C programming basics T3-1 -

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

25.2 Opening and Closing a File

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

Programming & Data Structure

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

2009 S2 COMP File Operations

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

C-Refresher: Session 10 Disk IO

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

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

Standard C Library Functions

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

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

CS246 Spring14 Programming Paradigm Files, Pipes and Redirection

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

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

Darshan Institute of Engineering & Technology for Diploma Studies Unit 6

CSC 1107: Structured Programming

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

PROGRAMMAZIONE I A.A. 2017/2018

CSC 1107: Structured Programming

Engineering program development 7. Edited by Péter Vass

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

Simple Output and Input. see chapter 7

ENG120. Misc. Topics

UNIX System Programming

C Basics And Concepts Input And Output

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)

Lecture 12 CSE July Today we ll cover the things that you still don t know that you need to know in order to do the assignment.

File Handling. 21 July 2009 Programming and Data Structure 1

Fundamentals of Programming. Lecture 10 Hamed Rasifard

CS240: Programming in C

Text Output and Input; Redirection

Student Number: Instructor: Reid Section: L0101 (10:10-11:00am)

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

HIGH LEVEL FILE PROCESSING

Lab # 4. Files & Queues in C

8. Characters, Strings and Files

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

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

Computer Programming Unit v

structs as arguments

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

CP2 Revision. theme: file access and unix programs

Data File and File Handling

211: Computer Architecture Summer 2016

Standard I/O in C, Computer System and programming in C

UNIX Shell. The shell sits between you and the operating system, acting as a command interpreter

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

Input/Output: Advanced Concepts

Computer programming

CSE 12 Spring 2016 Week One, Lecture Two

C mini reference. 5 Binary numbers 12

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

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

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

CS1003: Intro to CS, Summer 2008

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

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

CSE 303 Lecture 15. C File Input/Output (I/O) reading: Programming in C Ch. 16; Appendix B pp

Procedural Programming

Fundamentals of Programming & Procedural Programming

Arrays and Strings. Antonio Carzaniga. February 23, Faculty of Informatics Università della Svizzera italiana Antonio Carzaniga

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

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

de facto standard C library Contains a bunch of header files and APIs to do various tasks

Help Session 2. Programming Assignment 2

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

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

EXAMINATION REGULATIONS and REFERENCE MATERIAL for ENCM 339 Fall 2017 Section 01 Final Examination

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

Princeton University. Computer Science 217: Introduction to Programming Systems. I/O Management

DATA STRUCTURES USING C

SAE1A Programming in C. Unit : I - V

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

Student Number: Instructor: Reid Section: L5101 (6:10-7:00pm)

EM108 Software Development for Engineers

C PROGRAMMING. Characters and Strings File Processing Exercise

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

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

Course organization. Course introduction ( Week 1)

CSE 12 Spring 2018 Week One, Lecture Two

Transcription:

File IO and command line input CSE 2451

File functions Open/Close files fopen() open a stream for a file fclose() closes a stream One character at a time: fgetc() similar to getchar() fputc() similar to putchar() One line at a time: fgets() fprintf() similar to printf() fscanf() similar to scanf()

Some definitions for files <stdio.h> FILE data structure (type) used to access a stream NULL value of a null pointer constant EOF negative integer which indicates end-of-file has been reached FOPEN_MAX integer which represents the maximum number of files that the system can guarantee that can be opened simultaneously FILENAME_MAX integer which represents the longest length of a char array for valid filename stderr/stdin/stdout pointers to FILE types which correspond to the standard streams

File Usage Files are associated with streams and must be opened to be used The point of I/O within a file is determined by the file position When a file is opened, the file position points to the beginning of the file (unless the file is opened for an append operation in which case the position points to the end of the file) The file position follows read and write operations to indicate where the next operation will occur When a file is closed, no more actions can be taken on it until it is opened again Exiting from the main function causes all open files to be closed

Opening and closing files FILE *fopen(const char *filename, const char *mode); If successful, returns pointer to the FILE On error it returns a NULL pointer, errno is set Modes (partial list) r read text ( r ) w write text a append text http://www.cplusplus.com/reference/cstdio/fopen/ int fclose(file *stream); If successful, returns zero On error it returns EOF void perror(const char *str); //fprintf(stderr, %s: %s, s, error msg ); Prints a descriptive error message to stderr. First the string str is printed followed by a colon then a space (your error message). Then an error message based on the current setting of the variable errno is printed (system error message).

Single Character File IO int fgetc(file *stream); Gets the next character (an unsigned char) from the specified stream and advances the position indicator for the stream. On success the character is returned If the end-of-file is encountered, then EOF is returned and the end-of-file indicator is set. On failure, error indicator is set and EOF is returned http://www.cplusplus.com/reference/cstdio/fgetc/ int fputc(int char, FILE *stream); Writes a character (an unsigned char) specified by the argument char to the specified stream and advances the position indicator for the stream. On success the character is returned On failure, error indicator is set and EOF is returned

Example See io1.c

Multi-character file input char *fgets(char *str, int n, FILE *stream); Reads a line from the specified stream and stores it into the string pointed to by str. It stops when either (n-1) characters are read, the newline character is read, or the end-of-file is reached, whichever comes first. The newline character is copied to the string. A null character is appended to the end of the string. On error a null pointer is returned. If the end-of-file occurs before any characters have been read, the string remains unchanged. int fscanf(file *stream, const char *format,...); Reading an input field (designated with a conversion specifier) ends when an incompatible character is met, or the width field is satisfied. On success the number of input fields converted and stored are returned. If an input failure occurred, then EOF is returned. Returns EOF in case of errors or if it reaches eof

File output and EOF check int fprintf(file *stream, const char *format,...); Sends formatted output to a stream. Like printf, but specify a FILE instead of stdout. int feof(file *stream); Tests the end-of-file indicator for the given stream If the stream is at the end-of-file, returns nonzero value. If it is not at the end of the file, then it returns zero. http://www.cplusplus.com/reference/cstdio/feof/

Examples See io2.c and io3.c

Command Line Arguments int main(void); int main(); int main(int argc, char **argv); int main(int argc, char *argv[]); The parameters given on a command line are passed to a C program with two variables argc count of the command-line arguments argv pointer array to individual arguments as character strings

Additional details argc is non-negative and that argv[argc] is a null pointer By convention, the command-line arguments specified by argc and argv include the name of the program as the first element Ex: "rm file The shell will initialize the rm process with argc = 2 and argv = ["rm", "file", NULL]

Example See cl.c