File Processing - File Operations MVNC1 Fundamental File Processing Operations C++ File Processing.

29
essing - File Operations MVNC 1 Fundamental File Processing Operations C++ File Processing

Transcript of File Processing - File Operations MVNC1 Fundamental File Processing Operations C++ File Processing.

Page 1: File Processing - File Operations MVNC1 Fundamental File Processing Operations C++ File Processing.

File Processing - File Operations MVNC 1

Fundamental File Processing Operations

C++

File Processing

Page 2: File Processing - File Operations MVNC1 Fundamental File Processing Operations C++ File Processing.

File Processing - File Operations MVNC 2

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

Page 3: File Processing - File Operations MVNC1 Fundamental File Processing Operations C++ File Processing.

File Processing - File Operations MVNC 3

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

Page 4: File Processing - File Operations MVNC1 Fundamental File Processing Operations C++ File Processing.

File Processing - File Operations MVNC 4

File Access

IntroductionWriting 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

Page 5: File Processing - File Operations MVNC1 Fundamental File Processing Operations C++ File Processing.

File Processing - File Operations MVNC 5

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.

Page 6: File Processing - File Operations MVNC1 Fundamental File Processing Operations C++ File Processing.

File Processing - File Operations MVNC 6

Input/Output Redirection

prog2 < infile.dat > outfile.dat reads from ‘infile.dat’ and outputs to

'outfile.dat'.

Page 7: File Processing - File Operations MVNC1 Fundamental File Processing Operations C++ File Processing.

File Processing - File Operations MVNC 7

File creation/replacement

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

Page 8: File Processing - File Operations MVNC1 Fundamental File Processing Operations C++ File Processing.

File Processing - File Operations MVNC 8

File Appending

If you want to APPEND to the current 'outfile.dat', useprog >> outfile.dat

(>> means append).

Page 9: File Processing - File Operations MVNC1 Fundamental File Processing Operations C++ File Processing.

File Processing - File Operations MVNC 9

C++ header files

Cstdio.h - types and operations used for C streams

C++ iostream.h - stream operationsfstream.h - file stream operations

Page 10: File Processing - File Operations MVNC1 Fundamental File Processing Operations C++ File Processing.

File Processing - File Operations MVNC 10

C++ Stream classes

C++ streams are implemented as classesostream - output streamistream - input stream

cin, cout, and cerr are three predefined instances of streamscin - is an instance of istreamcout and cerr are instances of ostream

These streams are defined in iostream.h File manipulation methods are defined in

fstream.h

Page 11: File Processing - File Operations MVNC1 Fundamental File Processing Operations C++ File Processing.

File Processing - File Operations MVNC 11

C++ Stream constructors

fstream ();leave the stream unopened

fstream (char * filename, int mode);open the file filename using mode modeMode 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:app - (for output) append to the end of file– ios:binary - opens in binary mode (text is default)

Page 12: File Processing - File Operations MVNC1 Fundamental File Processing Operations C++ File Processing.

File Processing - File Operations MVNC 12

C++ Stream destructors

~fstream();destroys the fstream object. File is closed of

openned by the constructor or the open member function.

Page 13: File Processing - File Operations MVNC1 Fundamental File Processing Operations C++ File Processing.

File Processing - File Operations MVNC 13

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

Page 14: File Processing - File Operations MVNC1 Fundamental File Processing Operations C++ File Processing.

File Processing - File Operations MVNC 14

C++ Formatted Stream I/O operators

<<output to fstreamdefined for all built in data types

>>input from fstreamAlso defined for all built in data types

It is important to understand that these operators format based on the passes datatype

Many modi

Page 15: File Processing - File Operations MVNC1 Fundamental File Processing Operations C++ File Processing.

File Processing - File Operations MVNC 15

fstream subclasses

ifstreamfor inputios:in implied

ofstreamfor outputios:out implied

All the fstream methods are inherited.

Page 16: File Processing - File Operations MVNC1 Fundamental File Processing Operations C++ File Processing.

File Processing - File Operations MVNC 16

Example: C++ File listing program

#include <fstream.h>void main (){ char ch;

fstream file; // declare fstream unattachedchar filename[20];cout <<"Enter the name of the file: "

<<flush; // force outputcin >> 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();}

Page 17: File Processing - File Operations MVNC1 Fundamental File Processing Operations C++ File Processing.

File Processing - File Operations MVNC 17

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);

Page 18: File Processing - File Operations MVNC1 Fundamental File Processing Operations C++ File Processing.

File Processing - File Operations MVNC 18

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).

Page 19: File Processing - File Operations MVNC1 Fundamental File Processing Operations C++ File Processing.

File Processing - File Operations MVNC 19

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.

Page 20: File Processing - File Operations MVNC1 Fundamental File Processing Operations C++ File Processing.

File Processing - File Operations MVNC 20

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::begios:::curios::end

Page 21: File Processing - File Operations MVNC1 Fundamental File Processing Operations C++ File Processing.

File Processing - File Operations MVNC 21

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

Page 22: File Processing - File Operations MVNC1 Fundamental File Processing Operations C++ File Processing.

File Processing - File Operations MVNC 22

String Handling

string.h definitions char *strcat (char *dest,

const char *src); concatenate src to end of dest.

Page 23: File Processing - File Operations MVNC1 Fundamental File Processing Operations C++ File Processing.

File Processing - File Operations MVNC 23

String Handling

char *strncat(char *dest, const char *src, size_t maxlen);

concatenate maxlen chars of src onto dest.

Page 24: File Processing - File Operations MVNC1 Fundamental File Processing Operations C++ File Processing.

File Processing - File Operations MVNC 24

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, int c); return pointer to last c in s, NULL if not

present.

Page 25: File Processing - File Operations MVNC1 Fundamental File Processing Operations C++ File Processing.

File Processing - File Operations MVNC 25

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.

Page 26: File Processing - File Operations MVNC1 Fundamental File Processing Operations C++ File Processing.

File Processing - File Operations MVNC 26

String Handling

int strncmp(const char *s1, const char *s2, size_t maxlen);

same as strcmp, but only uses n chars.

Page 27: File Processing - File Operations MVNC1 Fundamental File Processing Operations C++ File Processing.

File Processing - File Operations MVNC 27

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.

Page 28: File Processing - File Operations MVNC1 Fundamental File Processing Operations C++ File Processing.

File Processing - File Operations MVNC 28

String Handling

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

Page 29: File Processing - File Operations MVNC1 Fundamental File Processing Operations C++ File Processing.

File Processing - File Operations MVNC 29

Strings examplemain(){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); strcpy(mixed,name1); strcat(mixed," "); strcat(mixed,name2); printf("Both names are %s\n",mixed);}