Console and File I/O - Basics Rudra Dutta CSC 230 - Spring 2007, Section 001.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

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.
Week 5 Part I Kyle Dewey. Overview Exam will be back Thursday New office hour More on functions File I/O Project #2.
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.
Files in C Rohit Khokher. Files in C Real life situations involve large volume of data and in such cases, the console oriented I/O operations pose two.
Chapter 11: Data Files & File Processing In this chapter, you will learn about Files and streams Creating a sequential access file Reading data from a.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Introduction Data files –Can be created, updated,
 2000 Prentice Hall, Inc. All rights reserved. Chapter 11 – File Processing Outline 11.1Introduction 11.2The Data Hierarchy 11.3Files and Streams 11.4Creating.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
Chapter 11 C File Processing Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 6P. 1Winter Quarter I/O in C Lecture 6.
Guide To UNIX Using Linux Third Edition
Programming in C #2 Input / Output.
V-1 University of Washington Computer Programming I File Input/Output © 2000 UW CSE.
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.
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.
File Handling Spring 2013Programming and Data Structure1.
File IO and command line input CSE 2451 Rong Shi.
1 File Handling. 2 Storage seen so far All variables stored in memory Problem: the contents of memory are wiped out when the computer is powered off Example:
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.
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 11 File Processing. Objectives In this chapter, you will learn: –To be able to create, read, write and update files. –To become familiar with.
 2000 Prentice Hall, Inc. All rights reserved Introduction Data files –Can be created, updated, and processed by C programs –Are used for permanent.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 11 – File Processing Outline 11.1Introduction.
Chapter 12 Files (reference: Deitel ’ s chap 11) chap8.
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:
chap8 Chapter 12 Files (reference: Deitel ’ s chap 11)
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()”
ME-2221 COMPUTER PROGRAMMING Lecture 18 FILE OPERATIONS Department of Mechanical Engineering A.H.M Fazle Elahi Khulna University of engineering & Technology.
Files A collection of related data treated as a unit. Two types Text
CSCI N305: C Programming Copyright ©2006  Department of Computer & Information Science File Handling in C.
 2007 Pearson Education, Inc. All rights reserved. 1 C File Processing.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the basic properties and characteristics of external files ❏ To.
Lecture 20: C File Processing. Why Using Files? Storage of data in variables and arrays is temporary Data lost when a program terminates. Files are used.
IO revisited CSE 2451 Rong Shi. stdio.h Functions – printf – scanf(normally stops at whitespace) – fgets – sscanf Standard streams – stdin(defaults to.
Connecting to Files In order to read or write to a file, we need to make a connection to it. There are several functions for doing this. fopen() – makes.
C Programming Day 2. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Union –mechanism to create user defined data types.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
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 Text Input/Output Objectives
Chapter 7 Text Input/Output Objectives
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.
TMF1414 Introduction to Programming
Chapter 7 Text Input/Output Objectives
Plan for the Day: I/O (beyond scanf and printf)
CS111 Computer Programming
CSE1320 Files in C Dr. Sajib Datta
Programming in C Input / Output.
Programming in C Input / Output.
Files I/O, Streams, I/O Redirection, Reading with fscanf
Chapter 11 – File Processing
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
Programming and Data Structure
Text and Binary File Processing
File Input and Output.
File Handling.
Accessing Files in C Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Fundamental of Programming (C)
Programming in C Input / Output.
EPSII 59:006 Spring 2004.
FILE handeling.
Module 12 Input and Output
CSc 352 File I/O Saumya Debray Dept. of Computer Science
Rudra Dutta CSC Spring 2007, Section 001
Presentation transcript:

Console and File I/O - Basics Rudra Dutta CSC Spring 2007, Section 001

Copyright Rudra Dutta, NCSU, Spring, Input and Output User interaction: reading and writing data – Reading from keyboard or file – Writing to console or file C provides NO language primitives for I/O ! – Library functions provide mechanisms – Standard libraries Complex functions build on simple ones, but… What do simple functions build on? – Direct interaction with OS – “System” functions, provided as library

Copyright Rudra Dutta, NCSU, Spring, Character I/O Standard library treats all I/O as occurring in text streams int getchar() – Reads single next character from the input stream represented by the keyboard – Returns as … int !! int putchar(int c) – Takes the single character c (passed as int !) – Prints it on the output stream which is the console

Copyright Rudra Dutta, NCSU, Spring, Copy Input to Output #include int main() { int c; while ((c = getchar ()) != EOF) { putchar (c); }

Copyright Rudra Dutta, NCSU, Spring, Files General meaning - not console or keyboard, but something on disk Console and keyboard can be seen as special cases – Then all stream input and output can be represented as files Three special files – stdin - keyboard – stdout - default display, console – stderr - usually also default display, logically different Functions actually defined on files – File versions: fgetc(), fputc() – See manual pages for getchar() and putchar() On keyboard, what is End Of File? – A Ctl-D can by typed to indicate EOF

Copyright Rudra Dutta, NCSU, Spring, Shell Redirections Input or output of a program can be redirected from the command line –, | Meaning of redirecting input: – “Provide the program to be run with the contents of this file as if it is coming from stdin” – Similar meaning in redirecting output Piping – “Run both these programs, piping the stdout of the first one into the stdin of the second one”

Copyright Rudra Dutta, NCSU, Spring, Using Text Files stdio.h contains a structure called FILE – Can be used by thinking of it as a type – Indicates a stream Can be opened by fopen(), closed by fclose() – All opened files effectively close when program terminates (usually, this is an OS and not C issue) – But leaving out an fclose() can cause many errors and subsequent headaches Opening a file returns a file pointer – Like a handle, but actually a pointer in the C sense – Must be used to read/write/close

Copyright Rudra Dutta, NCSU, Spring, Formatted I/O printf() and scanf() – Exclusively for stdout and stdin, respectively Consider printf() – Allows programmer to pass a “format string”, and indicate placeholders – Remaining arguments specify values to use for placeholders – Variable number of argument ! scanf() – Also uses format string – Must pass locations of variables whose values will be read into – Use of pointers - we shall revisit appropriately Using text files – fprintf() and fscanf() – Exactly the same, except addition first argument is file pointer

Copyright Rudra Dutta, NCSU, Spring, printf() Conversions Format string needs its own “language” to specify types of variable values – Why? Name of variable indicates only location where variable can be found – Does not indicate type – Therefore, how many bytes it occupies Coversions are listed in manual pages – More accesibly, in text