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

Similar documents
Standard File Pointers

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

Computational Methods of Scientific Programming Fall 2007

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

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

Input / Output Functions

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)

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

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

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.

Standard C Library Functions

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

File IO and command line input CSE 2451

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

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

Input/Output: Advanced Concepts

EE458 - Embedded Systems Lecture 4 Embedded Devel.

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

CS240: Programming in C

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

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

C Basics And Concepts Input And Output

Organization of a file

CE Lecture 11

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

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

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

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

C programming basics T3-1 -

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

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

25.2 Opening and Closing a File

Darshan Institute of Engineering & Technology for Diploma Studies Unit 6

Advanced C Programming Topics

Basic and Practice in Programming Lab 10

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

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

CS246 Spring14 Programming Paradigm Files, Pipes and Redirection

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

Programming Fundamentals

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

CSC 1107: Structured Programming

CSC 1107: Structured Programming

File I/O. Preprocessor Macros

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

Computer Programming Unit v

File Handling. 21 July 2009 Programming and Data Structure 1

2009 S2 COMP File Operations

Heap Arrays. Steven R. Bagley

CSC209H Lecture 3. Dan Zingaro. January 21, 2015

today cs3157-fall2002-sklar-lect05 1

CSCI-243 Exam 2 Review February 22, 2015 Presented by the RIT Computer Science Community

SWEN-250 Personal SE. Introduction to C

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

Binghamton University. CS-220 Spring Includes & Streams

Lecture 03 Bits, Bytes and Data Types

M.CS201 Programming language

Introduction to file management

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

Printable View of: Week 13: Miscelaneous cool features. Returns from standard functions. returns from standard functions: scanf(), fopen()

IO = Input & Output 2

Programming & Data Structure

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

Pointers and File Handling

CAAM 420 Notes Chapter 2: The C Programming Language

8. Characters, Strings and Files

Today s Learning Objectives

CS 220: Introduction to Parallel Computing. Input/Output II. Lecture 8

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

CSE 303: Concepts and Tools for Software Development

The Design of C: A Rational Reconstruction: Part 2

Writing to and reading from files

A Crash Course in C. Steven Reeves

PROGRAMMAZIONE I A.A. 2017/2018

Fundamentals of Programming. Lecture 11: C Characters and Strings

Recitation 2/18/2012

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

C for C++ Programmers

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

Unit 4 Preprocessor Directives

Input/Output and the Operating Systems

LESSON 4. The DATA TYPE char

DATA STRUCTURES USING C

Floating-point lab deadline moved until Wednesday Today: characters, strings, scanf Characters, strings, scanf questions clicker questions

CSCI 171 Chapter Outlines

Lecture 9 Assertions and Error Handling CS240

Goals of this Lecture

Have examined process Creating program Have developed program Written in C Source code

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

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

I/O Management! Goals of this Lecture!

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

I/O Management! Goals of this Lecture!

ENG120. Misc. Topics

File I/O Lesson Outline

are all acceptable. With the right compiler flags, Java/C++ style comments are also acceptable.

Preprocessing directives are lines in your program that start with `#'. The `#' is followed by an identifier that is the directive name.

Lectures 5-6: Introduction to C

Continued from previous lecture

A Fast Review of C Essentials Part I

Transcription:

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

Remember the name: Pre-processor Most commonly used features: #include, #define. Think of the preprocessor as processing the file so as to remove all #whatevers. #include inserts the entire contents of a file, verbatim, exactly where the #include appeared. Two ways to use: #include "filename" - typically searches for filename in the directory where the source program is #include <filename> - searches for filename in an implementation-defined way. Typically, use angle brackets for standard library header files. 2

#define: a macro facility #define SIZE 10 #define CENT_TO_INCHES 2.54 void main() { int i; int inches[size], centimeters[size]; /* read in inches[]... */ } for( i = 0; i < SIZE; i++ ) centimeters[i] = inches[i] * CENT_TO_INCHES; Why use? Making global changes is easier. Makes programs easier to read (assuming descriptive names) Some will go so far as to say that no constants should appear in your code (other than perhaps some zeroes and ones) - they should all be #defined. 3

#define: more details Possible to define macros with arguments: #define sum(a,b) ((A)+(B)) x = sum( 3 * x, 1 ); becomes x = (( 3 * x ) + ( 1 )); Put brackets around anything that could be a more complex expression! For example, this doesn t work: #define square(x) x * x /* Doh! */ y = 2 * square(z + 1); One use of #define is to speed up programs by avoiding the overhead of function calls. Another use is to improve readability and make programs self-documenting : #define max(x,y) ((x) > (y)? (x) : (y)) 4

More on the preprocessor Conditional compilation: #define DEBUG void main() { int x; /* blah blah blah */ } #ifdef DEBUG printf( "current value of x is %d\n", x ); #endif There are other features: #if, #else, etc. #if SYSTEM == SYSV #define HDR "sysv.h" Use conditional compilation for testing! With a single file you can have testing and ship versions of your program, where in the testing code you can include lots of tests to check for null pointers, etc., that would be unnecessary and slow in the ship version. (Such tests are often called assertions.) 5

Files The library stdio.h defines a structure called FILE. (We ll learn more about structures next time.) You don t need to know any of the details of how a FILE works, you just need to know how to point to them. A pointer to a file is called a file handle. Here s the paradigm: void main() { } FILE *fp; /* pointer to a file */ /* open WRITEME.txt for reading */ fp = fopen("writeme.txt", "w") /* do stuff with the file */ fprintf(fp,"this goes in the file"); /* close the file -- it s a good habit */ fclose(fp); The function fopen accepts a string corresponding to the name of the filename, and another string the says whether we want to open the file to read ("r"), write ("w"), or append ("a"). If there is any error, fopen returns the pointer value NULL. 6

Printf and Scanf with Files Up to now, we ve been using printf to print messages (to standard output ), and scanf to receive input from the keyboard (from standard input ). These can be generalized to the functions fprintf and fscanf, which write to and read from a particular file: Given a file pointer fp, the statement: fprintf(fp,"this goes in the file"); writes to the file handled by fp. The statement: fscanf(fp,"%d",&intvar); reads an integer from the file. C treats the standard input and output as files Their file handles are defined as the constants stdin and stdout in stdio.h. So, printf and scanf are just special cases of fprintf and fscanf, applied to the file handles stdout and stdin. printf("hello, world!"); /* is equivalent to */ fprintf(stdout,"hello, world!"); 7

Other ways to read and write There are more rudimentary ways to read from files and write to files. Suppose that fp is a file handle. The function getc(fp) returns a single character read from the file. If you want to put a character back in the stream, use the function ungetc(c,fp). putc(c,fp) writes the character c to the file. It returns the character c if successful, or the special character EOF (a constant defined in stdio.h, for end of file ) if the write fails. Here s how to use the preprocessor to write new functions that read characters from standard input and write to standard output: #define getchar() #define putchar(c) getc(stdin) putc((c),stdout) (Actually, getchar and putchar are already defined, and they work exactly as defined here.) 8

Reading/writing strings We can read strings from files, too: fgets(char_array,max_characters,fp) copies at most max_characters characters from the file handled by fp. The standard usage is this: fgets(my_char_array,sizeof(my_char_array),fp) The sizeof function returns the number of bytes in the structure you pass to it. Since a char takes up a single byte, you can use it to find the number of elements in a char array! It s MUCH BETTER to use fgets than scanf to read strings from standard input. Why? If the string read by scanf is longer than the buffer allocated for it, scanf will crash, probably through some ugly memory error. If the string you type doesn t match the format string you use as an argument to scanf, the function will probably crash. Like, if you re trying to read an integer and the user types a letter. 9

How fgets works Blocks the program and reads from the file (or standard input) until a newline character. The resulting string includes the newline character (unless it fills up the whole buffer). How would you trim the newline character from the string? Stores up to max_characters-1 characters in the buffer. (All strings are terminated by 0!) If more than that number of characters is encountered before a newline, they ll be in the stream the next time you read from that file (using fgetc, fgets, fscanf, etc.). But what about the useful string parsing that scanf does? 10

Using sscanf and sprintf It turns out that fscanf and fprintf have versions that apply to strings. The function sscanf works exactly like fscanf, only instead of passing it a file handle, you pass it a string to read from. The function sprintf works the same instead of passing it a file handle to write to, you pass it a string buffer (in this example, s) to write to. sprintf( s, format_string, arg1, arg2,... ) It doesn t return a string, like you might see in Java. Remember: you always have to allocate space for strings, and the assignment operator doesn t work for arrays. 11

The safe string-reading paradigm To read a string from standard input: char *string_buffer[100]; int a; printf( "Enter a number: " ); fgets( string_buffer, sizeof(string_buffer), stdin ); /* do stuff with the string */ printf( "String length: %d\n", strlen(string_buffer) ); printf( "String entered: %s", string_buffer ); /* parse the integer */ sscanf( string_buffer, "%d", &a ); /* etc. */ This paradigm won t crash your program. Users may still enter bad data, but you can determine that and deal with it within the program, rather than finding out when the whole program crashes. 12

More on sscanf The function sscanf returns an int equal to the number of tokens that were matched. Example: using sscanf to parse a date string if( sscanf( buffer, "%d %s %d", &day, monthname, &year ) == 3 ) { /* 25 Dec 1988 form */ printf( "valid: %s\n", buffer ); } else if( sscanf( buffer, "%d/%d/%d", &month, &day, &year ) == 3 ) { /* mm/dd/yy form */ printf( "valid: %s\n", buffer ); } else { printf( "invalid: %s\n", buffer ); } For more information, see Chapter 7 of K&R. 13