IO = Input & Output 2

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

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

Programming Fundamentals

File IO and command line input CSE 2451

Standard File Pointers

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)

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

Darshan Institute of Engineering & Technology for Diploma Studies Unit 6

CS240: Programming in C

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

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

Standard C Library Functions

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

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

PROGRAMMAZIONE I A.A. 2017/2018

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

C programming basics T3-1 -

Input/Output and the Operating Systems

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

Organization of a file

Input / Output Functions

ENG120. Misc. Topics

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

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.

CE Lecture 11

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

25.2 Opening and Closing a File

Fundamentals of Programming. Lecture 10 Hamed Rasifard

System Software Experiment 1 Lecture 7

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

String constants. /* Demo: string constant */ #include <stdio.h> int main() {

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

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

Introduction to file management

Data File and File Handling

8. Characters, Strings and Files

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

M.CS201 Programming language

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

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

CS246 Spring14 Programming Paradigm Files, Pipes and Redirection

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

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

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

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

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

C Basics And Concepts Input And Output

File I/O. Preprocessor Macros

2009 S2 COMP File Operations

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

Chapter 10: File Input / Output

Naked C Lecture 6. File Operations and System Calls

Ch 11. C File Processing (review)

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

CSC209H Lecture 3. Dan Zingaro. January 21, 2015

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

C File Processing: One-Page Summary

C Programming Language

C-Refresher: Session 10 Disk IO

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

CSCI 171 Chapter Outlines

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

Lecture 9: File Processing. Quazi Rahman

C mini reference. 5 Binary numbers 12

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

Advanced C Programming Topics

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

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

Continued from previous lecture

Programming & Data Structure

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

Input/Output: Advanced Concepts

C PROGRAMMING. Characters and Strings File Processing Exercise

Intermediate Programming, Spring 2017*

Text Output and Input; Redirection

Intermediate Programming / C and C++ CSCI 6610

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

Computer Programming Unit v

CSE 12 Spring 2018 Week One, Lecture Two

Hello World. It is traditional to start with a Hello World program, though this version is a bit different:

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

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

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

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

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

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

Simple Output and Input. see chapter 7

Chapter 11 File Processing

CSC 270 Survey of Programming Languages. Input and Output

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

gcc o driver std=c99 -Wall driver.c everynth.c

The Design of C: A Rational Reconstruction: Part 2

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

CSI 402 Systems Programming LECTURE 4 FILES AND FILE OPERATIONS

Lecture 8: Structs & File I/O

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

Fundamentals of Programming. Lecture 15: C File Processing

Lecture 7: file I/O, more Unix

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

C for C++ Programmers

Transcription:

Input & Output 1

IO = Input & Output 2

Idioms 3 Input and output in C are simple, in theory, because everything is handled by function calls, and you just have to look up the documentation of each function When the I/O library for C was written, it was a masterpiece because, for the first time, all I/O used the same functions, regardless of device But in practice, there are a lot of details, and a lot of pitfalls, and online tutorials tell you rubbish If you don't understand the details, then just stick to the idioms provided in this chapter

Summary 4 We are going to cover: operation using functions command lines main read chars from file fopen, fgetc, feof, fclose read bytes from file fopen, fgetc, feof, fclose read lines from file fopen, fgets, feof, fclose read chars from stdin getchar, feof read lines from stdin fgets, feof write chars to file fopen, fputc, fclose write bytes to file fopen, fputc, fclose write lines to file fopen, fprintf, fclose never use these getline, readline, gets, scanf, fscanf

Command line arguments 5 One way to provide a small amount of input to a program is to put it on the program's command line For example, an echo program could be given some items to print out like this:./echo one two

Writing echo 6 To access these items, define main with two arguments: /* Print out command line arguments */ #include <stdio.h> echo.c int main(int n, char *args[n]) { for (int i=0; i<n; i++) { printf("arg %d is %s\n", i, args[i]); } }

Running echo 7 Let's try running the program $ gcc -std=c99 -Wall echo.c -o echo $./echo one two Arg 0 is c:\users\ian\echo.exe Arg 1 is one Arg 2 is two

Conventions 8 The first argument to main is the number of words on the command line, including the name of the program at the beginning, often declared as int argc, ("argument count") but n seems a better name The second argument to main is an array of strings representing the words on the command line, usually declared as char *argv[argc] or char *argv[] or char **argv, ("argument vector") though args seems a better name (it is actually an array of pointers to character arrays)

Program path 9 The first argument, the program name, is expanded to the full file-system path of the compiled program file This provides the only simple, reasonably platformindependent way of finding the installation directory of the program - a program can be distributed as a zip file The main alternatives involve re-packaging the program for installation, separately for each platform

Command line processing 10 The command line is chopped up into words by the operating system (terminal window program) before it reaches the program This makes the details independent of programming language, but dependent on the platform The basics are very similar on all platforms, though

Special characters 11 You should assume that any character typed on the command line will have some special effect, except for letters, digits, and decimal points Spaces form word-boundaries Single or double quotes allow phrases with spaces or other special characters in to be treated as single words./echo one "two words" three./echo "one*" "two?"

Read chars from file 12 To read a text file one character at a time, try this: /* Print character codes from a file */ #include <stdio.h> #include <stdbool.h> codes.c int main() { FILE *in = fopen("in.txt", "r"); char ch = fgetc(in); while (! feof(in)) { printf("%d\n", ch); ch = fgetc(in); } fclose(in); }

End of file 13 Note that feof doesn't give true until after unsuccessfully reading a character beyond the end This helps with interactive or network streams, which mustn't be read too far ahead But it is logically awkward, because you need one more call to fgetc than there are characters in the file

The break style 14 Many programmers use break, like this: while (true) { char ch = fgetc(in); if (! feof(in)) break; printf("%d\n", ch); } It avoids repetition of the fgetc line, and avoiding repetition is good But the logic is obscure (it looks like an infinite loop, but it is actually an n+1/2 loop) so it is not as good

The EOF style 15 In most tutorials, you see this traditional variation int ch; while ((ch = fgetc(in))!= EOF) {... } it unintuitively needs int it needs too many brackets it has poor structure: statement inside expression it has poor logic: side effect inside test it risks confusion between = and ==

Initial f 16 Many I/O functions are paired up, e.g. fgetc and getc, but the relationship varies Generally, the version without the f is less safe, e.g. getc is a macro, so fails if the argument has a side effect, and gets has different conventions which cause security loopholes, so it should never be used

Closing files 17 You should close a file when you have finished with it If you don't, the file isn't closed until the program ends, and you may reach the (small) limit on the number of open files on your system Or, if it is an output file, the last part of the output won't get written out to the file (because of buffering) Or, you may develop a bad habit which will bite you later

Read bytes from file 18 To read bytes from a binary file, use this FILE *in = fopen("in.exe", "rb"); unsigned char b = fgetc(in); while (! feof(in)) {... b = fgetc(in); } fclose(in); Depending on your application, you might choose signed char, but avoid just char which is sometimes signed and sometimes not The "b" (binary) switches off newline handling

Read lines from file 19 To read a text file one line at a time, assuming it has short lines, use this FILE *in = fopen("in.txt", "r"); fgets(line, max, in); while (! feof(in)) { printf("line %s", line); fgets(line, max, in); } fclose(in); lines.c Note: the printf has no \n, because the string in the line array contains a \n or \r\n

Numbers and names 20 /* Read a number and name per line. Note the limits on lengths. */ #include <stdio.h> phone.c int main() { const int max = 100; char line[max], name[50]; int n; FILE *in = fopen("in.txt", "r"); fgets(line, max, in); while (! feof(in)) { sscanf(line, "%d %s", &n, name); printf("number %d name %s\n", n, name); fgets(line, max, in); } fclose(in); }

Using sscanf 21 To read in numbers, one per line, you could do this: sscanf(line, "%d %s", &n, name); The number argument is &n ('address of n') because otherwise sscanf would just be passed a copy of n and couldn't change the original The name argument has no & in front, because arrays are passed by reference, not by value - what is passed is a pointer to the start of the array

Removing newlines 22 If you want to get rid of the newline at the end of a string you've just read in, this is a compact technique: line[strcspn(line, "\r\n")] = '\0'; This is cross-platform (it handles \n and \r\n) and it works on strings which don't have a newline It's a bit obscure, but you'll find it quickly enough if you Google C remove newline string As with other tricks, you could wrap it in a function with a comment

Libraries 23 Rule: never use functions like getline or readline That's because they are not part of the standard libraries So when you change platforms, you find that the function doesn't exist, or behaves completely differently

sscanf 24 Use the sscanf function to scan a string, chopping it up and converting parts into numbers etc. Beware: %s reads in a word up to the next space, instead use %[...] for strings with spaces, e.g. %[^,]%*c to read everything up to a comma and then discard the comma Beware: phone numbers can start with 0 which mustn't be thrown away, so it is better to treat phone numbers as strings with %s, not numbers

fscanf problems 25 Advice: don't use fscanf, use fgets and sscanf Never use it with unlimited %s or %[...], there is no check that the array is big enough, so there is a bug, and over the net it becomes a security loophole Other problems are: a space means any white space including newlines, %s means a 'word' (up to the next white space), and the newline at the end of a line doesn't get read in (technically it gets read in and pushed back, so not suitable for interaction) and there is no validation (so not suitable for user input)

Proper fscanf 26 It is possible to use fscanf for fixed format files, though most programmers get it wrong For phones: fscanf("%50s %50[^\n]%*c"...) (50 limits the strings, %*c discards the newline) But the max size has to be explicit, not a variable, which is very inflexible, and the rest of a line that's too long is not discarded, and there is no validation, so it is still not recommended when the text file is provided by a user

Efficiency 27 Often, physical I/O is very slow, so the efficiency of the code is irrelevant When it matters, reading a line at a time is faster than reading a character at a time For maximum efficiency, read the whole file in FILE *fp = fopen(path, "rb"); fseek(fp, 0, SEEK_END); long length = ftell(fp); fseek(fp, 0, SEEK_SET); fread(s, length, 1, fp); fclose(fp);

Read chars from stdin 28 To read characters of standard input, e.g. to echo everything in upper case, use: char ch = getchar(); while (! feof(stdin)) { printf("%c", toupper(ch)); ch = getchar(); } upper.c However, this should be rare, because you almost always want to read the standard input a line at a time

End of stream 29 while (! feof(stdin))... It is good practice to recognise the end of the stream (in case the user pipes a file into the standard input) You can end the standard input by typing CTRL/D (usually), but the effects are a bit platform dependent You could end with CTRL/C, but that causes a program 'crash', not a clean shut down Normally, your program should recognise something, e.g. typing quit, as well as feof to end the program

Read lines from stdin 30 To read a line of standard input, use this printf("...prompt..."); fgets(line, max, stdin); type.c To extract numbers etc. from a string such as a line that you have read in, use sscanf

gets 31 Rule: never use the gets function You give it an array to read the line into, but there is no check that it is big enough You cannot prevent the user from giving a line which is too long, so your program contains a bug Worse, if your program can be used over the net, it has a security loophole for hackers to use

scanf problems 32 Advice: don't use scanf, use fgets and sscanf As with fscanf, never use it with unlimited %s or %[...], because of the security loophole Other problems are that a space means any white space including newlines, %s means a 'word' (text up to the next white space), and the newline at the end of a line doesn't get read in (so related print statements are often too soon or too late)

Proper scanf 33 It is possible to use fscanf, though most programmers get it wrong To read in a line: scanf("%100[^\n]%*c"...) But it is hard to get everything right, having to specify the available space explicitly is inflexible, and there is no validation, so it is still not recommended As with files, to get rid of the newline at the end of a string you've just read in with fgets, try this: line[strcspn(line, "\r\n")] = '\0';

Write chars to a file 34 To write characters to a text file, use this FILE *out = fopen("out.txt", "w");... fputc(ch, out);... fclose(out); On Windows, fputc('\n'...) will write out \r\n to match the Windows newline convention Don't forget the fclose, or the file will be incomplete

Write bytes to a file 35 Writing bytes is almost identical to writing characters: FILE *out = fopen("out.txt", "wb");... fputc(ch, out);... fclose(out); The only difference is "wb", which prevents special processing of newlines

Write lines to a file 36 To write lines to a text file, use this FILE *out = fopen("out.txt", "w");... fprintf(out, "Line\n");... fclose(out); fprintf will output \n as the 'correct' newline You could also use fputs for plain strings, but beware - it is less flexible and it has a different argument order

Writing partial lines 37 To write a line to a text file in two parts: fprintf(out, "Hello");... fprintf(out, " world!\n"); If the output stream is interactive, and you want to make sure the user sees the partial line before the second call, either call setbuf, or add fflush(out); between the calls

Checking errors 38 Strictly speaking, after every I/O call, you should check whether it failed for some reason It is common to leave this out for prototype programs, but tighten everything up for production programs or libraries The fopen function is the one that most needs checking

Checking fopen 39 Here's an approach to checking fopen: FILE *fopencheck(char *file, char *mode) { FILE *p = fopen(file, mode); if (p!= NULL) return p; fprintf(stderr, "Can't open %s: ", file); fflush(stderr); perror(""); exit(1); }... FILE *in = fopencheck("in.txt", "r");