CSC 270 Survey of Programming Languages. Input and Output

Similar documents
Input/Output and the Operating Systems

CSC 1107: Structured Programming

CSC 1107: Structured Programming

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

25.2 Opening and Closing a File

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

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

CSC209H Lecture 3. Dan Zingaro. January 21, 2015

Programming & Data Structure

Fundamentals of Programming. Lecture 10 Hamed Rasifard

CS240: Programming in C

Character Arrays. strlen("hello, world"); /* string constant */ strlen(array); /* char array[100]; */ strlen(ptr); /* char *ptr; */

Course organization. Course introduction ( Week 1)

PROGRAMMAZIONE I A.A. 2017/2018

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

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

2009 S2 COMP File Operations

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

M.CS201 Programming language

Input/Output: Advanced Concepts

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

Input / Output Functions

File IO and command line input CSE 2451

Goals of this Lecture

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

I/O Management! Goals of this Lecture!

I/O Management! Goals of this Lecture!

Standard C Library Functions

System Software Experiment 1 Lecture 7

File Handling. 21 July 2009 Programming and Data Structure 1

Pointers and File Handling

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

Darshan Institute of Engineering & Technology for Diploma Studies Unit 6

10/6/2015. C Input/Output. stdin, stdout, stderr. C Language II. CMSC 313 Sections 01, 02. C opens three input/output devices automatically:

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

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

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

8. Characters, Strings and Files

C Programming Language

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

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

C-Refresher: Session 10 Disk IO

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

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

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

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

Introduction. Files. 3. UNIX provides a simple and consistent interface to operating system services and to devices. Directories

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

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

IO = Input & Output 2

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

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

Lecture 8: Structs & File I/O

Computational Methods of Scientific Programming Fall 2007

upper and lower case English letters: A-Z and a-z digits: 0-9 common punctuation symbols special non-printing characters: e.g newline and space.

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 6 Files. putchar(ch); ch = getc (fp); //Reads single character from file and advances position to next character

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

Pointers cause EVERYBODY problems at some time or another. char x[10] or char y[8][10] or char z[9][9][9] etc.

Basic and Practice in Programming Lab 10

Standard File Pointers

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

Computer programming

Should you know scanf and printf?

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

CE Lecture 11

Array Initialization

File I/O. Preprocessor Macros

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

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

High Performance Programming Programming in C part 1

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

Introduction to C. CS2023 Winter 2004

Today s Learning Objectives

C PROGRAMMING. Characters and Strings File Processing Exercise

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

Lecture 7: file I/O, more Unix

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

Fundamentals of Programming

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

The detail of sea.c [1] #include <stdio.h> The preprocessor inserts the header file stdio.h at the point.

Fundamental of Programming (C)

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

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

Introduction to Computing Lecture 03: Basic input / output operations

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

Process Management! Goals of this Lecture!

C programming basics T3-1 -

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

Computer Programming Unit v

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

structs as arguments

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

CSC 270 Survey of Programming Languages. C-String Values

CS240: Programming in C

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

Binghamton University. CS-220 Spring Includes & Streams

ENG120. Misc. Topics

Programming Language B

Transcription:

CSC 270 Survey of Programming Languages C Lecture 8 Input and Output Input and Output C supports 2 different I/O libraries: buffered (higher level functions supported by ANSI standards) unbuffered (lower-level functions corresponding to UNIX systems calls) We will work with the buffered I/O funcitons only. 1

Standard Input, Output and Error All C programs running under UNIX have three standard files opened without any special commands: stdin standard input (usually the keyboard) stdout standard output (usually the monitor) stderr standard error (usually the monitor) All of these can be redirected to a file under Linux. Redirection and Piping All three standard files can be redirected: The command prog > outfile redirects output to a file named outfile, replacing it if it already exists. prog >> outfile redirects output to a file named outfile, appending to the end if it already exists. prog < infile redirect input to a file named infile. prog prog2 makes prog's standard output prog2's standard input. This is called piping. 2

tolower.c <stdio.h> <ctype.h> /* * main() - Convert input to lower case */ int main(void) { int c; while ((c = getchar())!= EOF) putchar(tolower(c)); return(0); Formatted I/O printf() and scanf() both use a control string as its first argument. The can be either a constant or a variable: char s[] = "hello, world\n"; printf(s); It matches specifications beginning with % (except for %%) with an argument in the list of parameters that follows the control string. 3

Specifiers All specifiers are of the form: %±w.pt w = minimum width p = precision (decimal places) t = data type Specifiers An Example If our output is "Hello, world" Control String Printed as (output appears between the colons) %s :Hello, world: %10s :Hello, world: %15s : Hello, world: %-15s :Hello, world : %15.10s : Hello, wor: %-15.10s :Hello, wor : %.10s :Hello, wor: 4

Specifiers Specifier types: d, i integer in decimal format o integer in octal format x integer in hexadecimal format u unsigned integer in decimal format c integer in character format s char *; prints all characters until the '\0' f double [-]m.dddddd by default 6 decimal places e, E double [-]m.dddddde±xx or mdddddde±xx g, G double; uses (e, E) if exponent is less than -4 or greater than or equal to precision; otherwise uses f format. scanf() scanf() uses the same control string as printf but for different purposes. It seeks all characters and skips them before matching argument to a specifier. 5

scan.c <stdio.h> int { main(void) int x; scanf("10%d", &x); /* don't forget the & */ printf("x = %d\n", x); return(0); If I input the line 10 1 It will print 1 sprintf() sprintf(char *string, char *format, arg1, arg2,..) places formatted output into string instead of into standard output. e.g., sprintf(s, "X = %d\n", x = 8); /* s now contains "x = 8\n" 6

sscanf() Also, sscanf() take its input from a string <stdio.h> #define MAXCHAR 80 int main(void) { int x, y, z; char s[maxchar], t[maxchar]; sscanf("x = 654y = 489z = 22", "x = %dy = %dz = %d", &x, &y, &z); sprintf(t, "i = %d\tj = %d\tk = %d\n", y, z, x); printf(t); return(0); Files Files are accessed by pointer to them; files pointers are defined by FILE *fp; We open a file by writing fp = fopen(filename, t); /* Both arguments are strings */ t is a string containing the mode for which the file is opened. 7

Opening Files Legal modes include: "r" "w" "a" "rb" "wb" "ab" open file for reading text open file for writing text older version of the file is destroyed open file for appending text output is appended to the end of the existing file open for reading a binary file open for writing a binary file open for appending a binary file We close with fclose(fp); Closing Files For text files, we can replace stream I/O functions with file function: putchar(c); getchar(); putc(c, fp); getc(fp); printf(" ", ); fprintf(fp, " ", ); scanf(" ", ); fscanf(fp, " ", ); gets(s); fgets(fp, s) puts(s); fputs(fp, s); 8

tolower2.c <stdio.h> <stdlib.h> <string.h> <ctype.h> #define MAXCHAR 80 /* * main() - Convert input to lower case */ int main(int argc, char *argv[]) { FILE *ifp, *ofp; int c; char ifilename[maxchar], ofilename[maxchar]; /* * The input file's name or both file names may * be given as command line arguments */ switch(argc) { case 3: /* Just copy the names */ strcpy(ofilename, argv[2]); strcpy(ifilename, argv[1]); break; case 2: /* Copy input file name - get output file name */ strcpy(ifilename, argv[1]); printf("enter output file name\t?"); scanf("%s", &ofilename); break; 9

case 1: /* Get both file names */ printf("enter input file name\t?"); scanf("%s", &ifilename); printf("enter output file name\t?"); scanf("%s", &ofilename); break; default: fprintf(stderr, "usage: tolower <input file> <output file>\n"); exit(1); /* Open the input file */ if ((ifp = fopen(ifilename, "r")) == NULL) { fprintf(stderr, "Cannot open %s\n", ifilename); exit(2); /* Open the output file */ if ((ofp = fopen(ofilename, "w")) == NULL) { fprintf(stderr, "Cannot open %s\n", ofilename); exit(2); /* Copy the file in lower case */ while ((c = getc(ifp))!= EOF) putc(tolower(c), ofp); /* Close both files and terminate */ fclose(ifp); fclose(ofp); return(0); 10

Reading Binary Files Binary files are stored in internal representations. We use structures in part to structure file records. size_t fread(void *ptr, size_t object_size, size_t n_objects, FILE *fp) reads n data objects from the file to which fp points and stores them at the location to which ptr points. fread returns the number of objects read. Writing Binary Files size_t fwrite(void *ptr, size_t object_size, size_t n_objects, FILE *fp) writes n data objects in the file to which fp points and takes them from the location to which ptr points. fwrite returns the number of objects written 11

writerec.c <stdio.h> <stdlib.h> #define MAXLINE 100 typedef struct { char first[11], initial, last[16]; char address[31], city[13], state[3], zip[6]; int balance; personrec; int getline(file *ifp, char s[], int lim); FILE *ifp, *ofp; int main(void) { personrec person; char filename[26], line[maxline]; printf("enter input file name\t?"); scanf("%s", &filename); if ((ifp = fopen(filename, "r")) == NULL) { printf("cannot open %s\n"); exit(1); printf("enter output file name\t?"); scanf("%s", &filename); if ((ofp = fopen(filename, "wb")) == NULL) { printf("cannot open %s\n", filename); exit(1); 12

/* Keep reading records and display them */ while (getline(ifp, line, MAXLINE) > 0) { /* Because address and city can contain a * space read up until the! and then skip it */ sscanf(line, "%s %c%s %[^!]!%[^!]!%s%s%d", &person.first, &person.initial, &person.last, &person.address, &person.city, &person.state, &person.zip, &person.balance); /* Display the record read */ printf("\\%s\\%c\\%s\\%s\\%s\\%s\\%s\\%d\\\n", person.first, person.initial, person.last, person.address, person.city, person.state, person.zip, person.balance); fwrite(&person, sizeof(person), 1, ofp); return(1); /* * getline() - Get a line of input, read it into s * and return its length */ int getline(file *ifp, char s[], int lim) { int c, i; /* * We will read as long as we haven't exceeded * the maximum characters, reached the end of * the line or ran out of input */ 13

for (i = 0; i < lim - 1 && (c = getc(ifp))!= EOF && c!= '\n'; ) s[i++] = c; if (c == '\n') s[i++] = c; s[i] = '\0'; return(i); readrec.c <stdio.h> <stdlib.h> #define MAXLINE 100 typedef struct { char first[11], initial, last[16]; char address[31], city[13], state[3], zip[6]; int balance; personrec; FILE *ifp, *ofp; 14

/* * main() - Convert a binary file into a text * file */ int main(void) { personrec person; char filename[26], line[maxline]; /* * Get the names of the input and output files * and open them */ printf("enter input file name\t?"); scanf("%s", &filename); if ((ifp = fopen(filename, "rb")) == NULL) { printf("cannot open %s\n"); exit(1); 15