1 / 29

Fundamental File Processing Operations C++

Fundamental File Processing Operations C++. File Processing. 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 ('<br>') character.

lahela
Download Presentation

Fundamental File Processing Operations C++

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Fundamental File Processing OperationsC++ File Processing

  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

  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

  4. File Access • Introduction • Writing cout (stdout) and reading from cin (stdinis) 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

  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.

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

  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.

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

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

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

  11. 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:app - (for output) append to the end of file • ios:binary - opens in binary mode (text is default)

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

  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

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

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

  16. 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(); }

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

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

  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.

  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::beg • ios:::cur • ios::end

  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

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

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

  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.

  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.

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

  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.

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

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

More Related