Fundamental File Processing Operations C++

Slides:



Advertisements
Similar presentations
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
Advertisements

Folk/Zoellick/Riccardi, File Structures 1 Objectives: To get familiar with Logical files vs. physical files File processing operations File processing.
File streams Chapter , ,
I/O Streams as an Introduction to Objects and Classes
I/O and Program Control Statements Dick Steflik. Overloading C++ provides two types of overloading –function overloading the ability to use the same function.
Programming Stream Manipulators. COMP102 Prog. Fundamentals I: Stream Manipulator Slide 2 Stream Manipulators in iostream.h Library I/O stream manipulators.
CS-212 C++ I/O Dick Steflik. C++ I/O Modeled after UNIX’s concept of a “stream” –conceptionally a stream is a continuous flow of characters/bytes from.
Input/Output Main Memory istream ostream Disk Drive Keyboard Scanner Disk Drive Monitor Printer stream = sequence of bytes.
Character Arrays strlen("hello, world"); /* string constant */ strlen(array); /* char array[100]; */ strlen(ptr); /* char *ptr; */ char pmessage[] = "now.
17 File Processing. OBJECTIVES In this chapter you will learn:  To create, read, write and update files.  Sequential file processing.  Random-access.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 11 Chapter 21 - C++ Stream Input/Output Outline 21.1Introduction.
1 File I/O In C++, I/O occurs in streams. A stream is a sequence of bytes Each I/O device (e.g. keyboard, mouse, monitor, hard disk, printer, etc.) receives.
Chapter 3: Input/Output
Input/Output in C++ C++ iostream.h instead of stdio.h Why change? –Input/output routines in iostream can be extended to new types declared by the user.
Stream Handling Streams - means flow of data to and from program variables. - We declare the variables in our C++ for holding data temporarily in the memory.
Sayed Ahmed Just E.T.C Technologies Inc. Just E.T.C. Education Inc.
Dale Roberts 1 Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
You gotta be cool. Stream Stream Output Stream Input Unformatted I/O with read, gcount and write Stream Manipulators Stream Format States Stream Error.
Ch 10. Input/Output1 Ch. 10 Input/Output Oregon State University Timothy Budd.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
C++ Streams Lecture-2.
C++ Lecture 8 Monday, 25 August I/O, File, and Preprocessing l An in-depth review of stream input/output l File handling in C++ l C++ preprocessing.
Chapter 10C++ for Java Programmers1 Chapter 10 Input/Output.
File I/O ifstreams and ofstreams Sections 11.1 &
STL List // constructing lists #include int main () { // constructors used in the same order as described above: std::list first; // empty list of ints.
File handling in C++ BCA Sem III K.I.R.A.S. Using Input/Output Files Files in C++ are interpreted as a sequence of bytes stored on some storage media.
File I/O 1 ifstreams and ofstreams Sections 11.1 & 11.2.
C++ Streams Lecture-2. C++ Streams Stream  A transfer of information in the form of a sequence of bytes I/O Operations:  Input stream: A stream that.
File I/O. fstream files File: similar to vector of elements Used for input and output Elements of file can be –character (text)struct –object (non-text.
C++ FILE I/O.
FILE I/O IN C++. Using Input/Output Files A computer file  is stored on a secondary storage device (e.g., disk);  is permanent;  can be used to provide.
Student Book Input / Output in C++
CSC141- Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 31 Thanks for Lecture Slides: C How to Program by Paul Deital &
 2000 Prentice Hall, Inc. All rights reserved. Chapter 21 - C++ Stream Input/Output Basics Outline 21.1Introduction 21.2Streams Iostream Library.
DCT1063 Programming 2 CHAPTER 3 FILE INPUT AND FILE OUTPUT Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 21 Thanks for Lecture Slides:
Streams, and File I/O Review. STREAMS Review STREAMS Streams are sequences of bytes. C++ I/0 occurs in streams Input – bytes flow from device to memory.
Chapter -7 Basic function of Input/output system basics and file processing Stream classes : I/O Streams. A stream is a source or destination for collection.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 1 due Friday, 7pm. RAD due next Friday. Presentations week 6. Today: –More details on functions,
#include int main() { char *string1=”C++”; char *string2=”Program”; int m=strlen(string1); int n=strlen(string2); for(int i=1;i
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
1 COMS 261 Computer Science I Title: Functions Date: October 24, 2005 Lecture Number: 22.
Input/Output CSci 588: Data Structures, Algorithms and Software Design Fall 2011 All material not from online sources copyright © Travis Desell, 2011
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 9. Streams & Files.
File Processing Files are used for data persistance-permanent retention of large amounts of data. Computer store files on secondary storage devices,such.
Input/Output in C++ C++ iostream.h instead of stdio.h Why change? –Input/output routines in iostream can be extended to new types declared by the user.
Declaring fstream Objects An istream object named cin connects program and keyboard An ostream object named cout connects the program and the screen These.
Exploring the C++ Stream Library Copyright 2006 Oxford Consulting, Ltd1 February IO Streams  IOStreams are part of the Standard C++ library.
Learners Support Publications Working with Files.
CSCI 333 Data Structures I/O with C++ 20 October, 2003.
Lecture 14 Arguments, Classes and Files. Arguments.
CS 1430: Programming in C++ 1. File Input in VS Project Properties Debugging Command Arguments quiz8-1.out We want to know how to do it ourselves, right?
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
Streams & Files in C++:Stream Classes, stream errors, disk file I/O with streams, File Pointers, Error handling in file I/O, File I/O with member functions,
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
CS212: Object Oriented Analysis and Design
Programming with ANSI C ++
17 File Processing.
Chapter 21 - C++ Stream Input/Output Stream Manipulators
Chapter 21 - C++ Stream Input/Output
CPSC 231 D.H. C++ File Processing
CS111 Computer Programming
17 File Processing.
Chapter 13: Advanced File and I/O Operations
Topics Input and Output Streams More Detailed Error Testing
CSC 143 Stream I/O Classes and Files [A11-A15, A38-A50]
C++ Programming: chapter 6 – iostream
File I/O in C++ II.
Input/Output Streams, Part 2
Presentation transcript:

Fundamental File Processing Operations C++

C++ Standard Input and Output The stream model of text input/output. A text stream consists of a sequence of lines; A line consists of zero or more characters terminated with a newline ('\n') character. Even if the computer system does not conform to that model, the library must hide the deviations

C++ Standard Input and Output C++ has three pre-opened streams as well: cin standard input connected to keyboard cout standard output connected to screen cerr standard output for error messages to screen

File Access Introduction Writing cout (stdout) and reading from cin (stdin is) very similar to file I/O; C++ (C) treats cin (stdin) and cout (stdout) as files. The major difference is that cxxx (stdxxx) are already connected to the program (and opened) by the system

Input/Output Redirection On MS-DOS and UNIX systems, prog > outfile.dat redirects all cin (stdout) output to 'outfile.dat'. Likewise, input redirection prog1 < infile.dat reads from 'infile.dat', for every cin (stdin) input.

Input/Output Redirection prog2 < infile.dat > outfile.dat reads from ‘infile.dat’ and outputs to 'outfile.dat'.

File creation/replacement In the previous examples: if 'outfile.dat' exists, it is deleted and replaced, if it does not exist, it is created.

File Appending If you want to APPEND to the current 'outfile.dat', use prog >> outfile.dat (>> means append).

C++ header files C stdio.h - types and operations used for C streams C++ iostream.h - stream operations fstream.h - file stream operations

C++ Stream classes C++ streams are implemented as classes ostream - output stream istream - input stream cin, cout, and cerr are three predefined instances of streams cin - is an instance of istream cout and cerr are instances of ostream These streams are defined in iostream.h File manipulation methods are defined in fstream.h

C++ Stream constructors fstream (); leave the stream unopened fstream (char * filename, int mode); open the file filename using mode mode Mode integer defined as ios enumerators: ios::in - open for input ios:out - open for output ios:trunc - (for output) discard contents ios:app - (for output) append to the end of file ios:nocreate - causes open to fail if file does not exist ios:binary - opens in binary mode (text is default)

C++ Stream destructors ~fstream(); destroys the fstream object. File is closed of openned by the constructor or the open member function.

C++ Stream Methods int open(char * filename, int mode); int read(unsigned char * dest_addr, int size); int write(unsigned char * source addr, int size) Read and write allow for unformatted (raw) I/O

C++ Formatted Stream I/O operators << output to fstream defined for all built in data types >> input from fstream Also defined for all built in data types It is important to understand that these operators format based on the passes datatype Many modi

fstream subclasses ifstream ofstream for input ios:in implied ofstream for output ios:out implied All the fstream methods are inherited.

Example: C++ File listing program #include <fstream.h> void main () { char ch; fstream file; // declare fstream unattached char filename[20]; cout <<"Enter the name of the file: " <<flush; // force output cin >> filename; file.open(filename, ios::in); file.unsetf(ios::skipws); // include white space in read while (1) { file >> ch; if (file.fail()) break; cout << ch; } file.close(); }

fstream Format manipulators Stream I/O can be modified with the format manipulators setf and unsetf. These functions set and clear bits in the format flags. ios::setf(long iFlags); ios::unsetf(lond iFlags); Flags may be “or”ed together with the “|” operator Example: myfile.setf(ios::hex | ios::left);

fstream Format Flags ios::skipws - Skip white space on input. ios::left - Left align values; pad on the right with the fill character. ios::right - Right align values; pad on the left with the fill character (default alignment). ios::internal - Add fill characters after any leading sign or base indication, but before the value. ios::dec - Format numeric values as base 10 (decimal) (default radix). ios::oct - Format numeric values as base 8 (octal). ios::hex - Format numeric values as base 16 (hexadecimal).

fstream Format Flags ios::showbase - Display numeric constants in a format that can be read by the C++ compiler. ios::showpoint - Show decimal point and trailing zeros for floating-point values. ios::uppercase - Display uppercase A through F for hexadecimal values and E for scientific values. ios::showpos - Show plus signs (+) for positive values. ios::scientific - Display floating-point numbers in scientific format. ios::fixed - Display floating-point numbers in fixed format. ios::unitbuf - Cause ostream::osfx to flush the stream after each insertion. By default, cerr is unit buffered. ios::stdio - Cause ostream::osfx to flush stdout and stderr after each insertion.

Seeking with C++ stream classes Two forms: file.seekg(long position); file.seekg(long offset, origin); position - position in file offset - byte offset from origin origin ios::beg ios:::cur ios::end

C Command Line Parameters Two values passed to main: int argc - Number of command line arguments (including command) char *argv[] - Array of pointers to parameter strings. argc >=1 *argv[0] is always a string containing the file name *argv[1] is the first argument, and so on

String Handling string.h definitions char *strcat (char *dest, const char *src); concatenate src to end of dest.

String Handling char *strncat(char *dest, const char *src, size_t maxlen); concatenate maxlen chars of src onto dest.

String Handling char * strchr(const char *s, int c); return pointer to first c in s, NULL if not present. char *strrchr(const char *s, return pointer to last c in s, NULL if not present.

String Handling int strcmp(const char *s1, const char *s2); return negative if s1 lexically less than s2 return 0 if s1==s2; return positive if s1>s2.

String Handling int strncmp(const char *s1, const char *s2, size_t maxlen); same as strcmp, but only uses n chars.

String Handling char *strcpy (char *dest, const char *src); copy src to dest. char *strncpy(char *dest, const char *src, size_t maxlen); same as strcpy, only n chars.

String Handling size_t strlen (const char *s); returns length of s;

Strings example main() { char name1[12],name2[12],mixed[25]; char title[20]; strcpy(name1,"Rosalinda"); strcpy(name2,"Zeke"); strcpy(title,"This is the title."); printf(" %s\n\n",title); printf("Name 1 is %s\n",name1); printf("Name 2 is %s\n",name2); if(strcmp(name1,name2)>0) /* returns 1 if name1 > name2 */ strcpy(mixed,name1); else strcpy(mixed,name2); printf("The biggest name alpabetically is %s\n",mixed); strcat(mixed," "); strcat(mixed,name2); printf("Both names are %s\n",mixed); }