CS1061 C Programming Lecture 17: Steams and Character I/O A. O’Riordan, 2004.

Slides:



Advertisements
Similar presentations
File Management in C. What is a File? A file is a collection of related data that a computers treats as a single unit. Computers store files to secondary.
Advertisements

I/O means Input and Output. One way: use standard input and standard output. To read in data, use scanf() (or a few other functions) To write out data,
UNIT 15 File Processing.
BITS Pilani, Pilani Campus TA C252 Computer Programming - II Vikas Singh File Handling.
File Management in C. A file is a collection of related data that a computers treats as a single unit. File is a collection of data stored permanently.
Chapter 7 Process Environment Chien-Chung Shen CIS, UD
File AccessCS-2301, B-Term File Access CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming Language, 2 nd.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 6P. 1Winter Quarter I/O in C Lecture 6.
Character Input and Output
Console and File I/O - Basics Rudra Dutta CSC Spring 2007, Section 001.
C - Input & Output When we are saying Input that means to feed some data into program. This can be given in the form of file or from command line. C programming.
Spring 2005, Gülcihan Özdemir Dağ Lecture 4, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 4 Outline 4.0 Reading.
Files Programs and data are stored on disk in structures called files Examples Turbo C++ - binary file Word binary file lab1.c - text file lab1.data.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students C File Input and Output Checking input for errors.
Lecture 13. Outline Standard Input and Output Standard Input and Output (I/O)– Review & more Buffered/unbuffered input Character I/O Formatted I/O Redirecting.
1 Homework Introduction to HW7 –Complexity similar to HW6 –Don’t wait until last minute to start on it File Access will be needed in HW8.
CP104 Introduction to Programming File I/O Lecture 33 __ 1 File Input/Output Text file and binary files File Input/output File input / output functions.
1 Week 2 The Crunchy Shell to the Soft and Chewy Kernel… Sarah Diesburg 8/3/2010 COP4610 / CGS5765.
File Handling Spring 2013Programming and Data Structure1.
C Programming Lecture 10 Instructor: Wen, Chih-Yu Department of Electrical Engineering National Chung Hsing University.
File IO and command line input CSE 2451 Rong Shi.
Shell (Addendum). Example r What if we want to support something like this: m ps –le | sort r One process should execute ps –le and another should execute.
Lecture 8a: File I/O BJ Furman 21MAR2011. Learning Objectives Explain what is meant by a data stream Explain the concept of a ‘file’ Open and close files.
Operating Systems Lecture 10. Agenda for Today Review of previous lecture Input, output, and error redirection in UNIX/Linux FIFOs in UNIX/Linux Use of.
Chapter 11: Data Files and File Processing Files and streams Creating a sequential access file Reading data from a sequential access file Using fgetc()
Chapter 7 : File Processing1 File-Oriented Input & Output CHAPTER 7.
1 CHAPTER6 CHAPTER 6. Objectives: You’ll learn about;  Introduction  Files and streams  Creating a sequential access file  Reading data from a sequential.
Lecture 11: Files & Arrays B Burlingame 18 November 2015.
Using Files Declare a variable, called file pointer, of type FILE * Use function fopen to open a named file and associate this file with the file pointer.
CNG 140 C Programming (Lecture set 10) Spring Chapter 10 Data Files.
Input/output streams. Character Input / Output and Input Validation Input and output devices: keyboards, disk drives, mouse, monitors, printers, network.
24-2 Perform File I/O using file pointers FILE * data-type Opening and closing files Character Input and Output String Input and Output Related Chapter:
CS 261 – Recitation 7 Spring 2015 Oregon State University School of Electrical Engineering and Computer Science.
GAME203 – C Files stdio.h C standard Input/Output “getchar()”
CHARACTER INPUT / OUTPUT AND INPUT VALIDATION. Introduction Input and output devices: keyboards, disk drives, mouse, monitors, printers. I/O functions.
IO revisited CSE 2451 Rong Shi. stdio.h Functions – printf – scanf(normally stops at whitespace) – fgets – sscanf Standard streams – stdin(defaults to.
1 Computer Programming Lecture 15 Text File I/O Assist. Prof Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering
BIL 104E Introduction to Scientific and Engineering Computing Lecture 2.
Real Numbers Device driver process within the operating system that interacts with I/O controller logical record 1 logical record 2 logical record 3.
Chapter 7 Process Environment Chien-Chung Shen CIS/UD
Chapter 22 – part a Stream refer to any source of input or any destination for output. Many small programs, obtain all their input from one stream usually.
ECE Application Programming
File I/O.
Introduction to C CSE 2031 Fall /3/ :33 AM.
CS 261 – Recitation 7 Fall 2013 Oregon State University
CSC215 Lecture Input and Output.
Plan for the Day: I/O (beyond scanf and printf)
CSE1320 Files in C Dr. Sajib Datta
CSE1320 Files in C Dr. Sajib Datta
Computer Programming Lecture 15 Text File I/O
Files I/O, Streams, I/O Redirection, Reading with fscanf
CSE1320 Files in C Dr. Sajib Datta
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
CSC215 Lecture Input and Output.
CSC215 Lecture Input and Output.
Beginning C Lecture 11 Lecturer: Dr. Zhao Qinpei
Programming and Data Structure
File Input and Output.
EECE.2160 ECE Application Programming
Line at a time I/O with fgets() and fputs()
Homework Applied for cs240? (If not, keep at it!) 8/10 Done with HW1?
Module 12 Input and Output
Introduction to C EECS May 2019.
Standard I/O Library Implementation
File I/O & UNIX System Interface
EECE.2160 ECE Application Programming
CS1100 Computational Engineering
Introduction to C CSE 2031 Fall /15/2019 8:26 AM.
EECE.2160 ECE Application Programming
Presentation transcript:

CS1061 C Programming Lecture 17: Steams and Character I/O A. O’Riordan, 2004

Creating Streams Streams are an abstraction used in C for input and output operations through devices like files, keyboard, printer, screen and I/O ports. A stream is represented by a pointer to a FILE structure that contains internal info about properties. Normally data contained in these structures are not referred to directly. You can declare streams by creating a pointer to a FILE structure. There are three streams open by default when you include stdio.h, stdin, stdout, and strerr. On my computer these are declared using FILE _iob[] (in stdio.h): #define stdin(&_iob[0]) #define stdout(&_iob[1]) #define stderr(&_iob[2])

stdin, stdout, stderr stdin - standard input stream. By default stdin corresponds to the keyboard, but this can be redirected. stdout - standard output stream. By default stdout is directed to the screen, but the operating system can redirect it to a file or any other output device. stderr - standard error stream. This is an output stream specifically intendend to receive error messages. By default is directed to the standard output (like stdout), but it can be redirected to a log file or any other output device.

getchar() A very useful function for character input. Prototype: int getchar(void); int getc(FILE *stream); getchar() gets the next character from stdin. Returns the next character from the standard input (stdin). Corresponds to: getc(stdin) If the end of file is reached or there has been an error, the function returns EOF.

getchar() example /* getchar example : typewriter */ #include int main(){ char c; printf("Enter line (dot '.' in sentence to exit)"); do{ c = getchar(); putchar(c); }while (c != '.'); return 0; }

putchar() A very useful function for writing a character. Prototype: int putchar(int character); int putc(int character, FILE * stream); putchar() writes character to stdout. Writes to the current position in the standard output (stdout) and increments the file pointer to point to next character. This routine corresponds to: putc(character,stdout). The value returned is the written character. If an error occurs, EOF is returned.

putchar() example /* putchar example: printing alphabet */ #include int main(){ char c; for (c = 'A' ; c <= 'Z' ; c++){ putchar(c); } return 0; } This program writes ABCDEFGHIJKLMNOPQRSTUVWXYZ to the standard output (screen).

ungetc() Sometimes it is handy to put a character back on the stream. This gives you a read- ahead facility. int ungetc(int character, FILE *stream); A character is pushed onto an input stream and the file pointer is reset to that previous position. This character will be returned by the next call to getc(). char c; c = getc(stdio); ungetc(c, stdio); /* put it back again */

system() Prototype: int system(const char * command); Invokes command shell to execute a command. Once terminated, the interpreter gives back control to the program returning an int value. Need to include stdlib.h. If a command was successfully executed the command interpreter returns 0. A return value of -1 indicates an error. Following is a program to display a Unix directory listing.

system() example #include int main(){ int i; printf("Trying to execute ls (directory listing"); i = system("ls"); if (i == -1) printf("Error executing ls\n"); else printf("Command successfully executed\n"); return 0; }

getenv() You can also get system environment variables. Prototype: char *getenv(const char * varname); Gets string from environment - a pointer to the null-terminated string containing the value of the environment variable varname. If the requested variable is not defined in the environment the function returns a NULL pointer. Depending on system this function may or not be case sensitive.

getenv() example /* getenv example: getting path */ #include int main (){ char * buffer; buffer = getenv("PATH"); if (buffer != NULL) printf("Current path is: %s",buffer); return 0; }