STANDARD LINE IO include iostream include string using

  • Slides: 17
Download presentation
STANDARD LINE I/O #include <iostream> #include <string> using namespace std; int main () {

STANDARD LINE I/O #include <iostream> #include <string> using namespace std; int main () { string str; cout << "Enter text: "; // cin >> str; // will cut off anything after the first space in the input line getline (cin, str); // to get the full line, up to eol cout << "You entered: " << str << 'n'; } http: //www. cplus. com/doc/tutorial/control/

FILE i/o in C++ #include <iostream> #include <fstream> using namespace std; int main ()

FILE i/o in C++ #include <iostream> #include <fstream> using namespace std; int main () { ofstream myfile ("example. txt"); // output file if (myfile. is_open()) { myfile << "This is a line. n"; // acts like cout myfile << "This is another line. n"; myfile. close(); // must-do … } else cout << "Unable to open file"; } [in the example. txt file on hard disk] This is a line. This is another line. http: //www. cplus. com/doc/tutorial/files/

FILE i/o in C++ // reading a text file #include <iostream> #include <fstream> #include

FILE i/o in C++ // reading a text file #include <iostream> #include <fstream> #include <string> using namespace std; int main () { string line; ifstream myfile ("example. txt"); // input file if (myfile. is_open()) { while ( getline (myfile, line) ) { cout << line << 'n'; // just printing it out on default output device } myfile. close(); } else cout << "Unable to open file"; } http: //www. cplus. com/doc/tutorial/files/

FILE OUTPUT #include <iostream> #include <fstream> using namespace std; int main () { ofstream

FILE OUTPUT #include <iostream> #include <fstream> using namespace std; int main () { ofstream myfile ("example. txt"); if ( !myfile. is_open()) { // output file // Take care of failure in one shot cout << "Unable to open file"; return 0; // important here } //. . and then proceed myfile << "This is a line. n"; myfile << "This is another line. n"; myfile. close(); return 0; } http: //www. cplus. com/doc/tutorial/files/

FILE OPENING Opening with explicit flags: ofstream myfile; myfile. open ("example. bin", ios: :

FILE OPENING Opening with explicit flags: ofstream myfile; myfile. open ("example. bin", ios: : out | ios: : app | ios: : binary); The format is: open (filename, mode); http: //www. cplus. com/doc/tutorial/files/

FILE OPENING Explicit flags are: ios: : in Open for input operations. ios: :

FILE OPENING Explicit flags are: ios: : in Open for input operations. ios: : out Open for output operations. ios: : binary Open in binary mode. ios: : ate Set the initial position at the end of the file. If this flag is not set, the initial position is the beginning of the file. ios: : app All output operations are performed at the end of the file, appending the content to the current content of the file. ios: : trunc If the file is opened for output operations and it already existed, its previous content is deleted and replaced by the new one. http: //www. cplus. com/doc/tutorial/files/

FILE OPENING Hence: … class default mode parameter ofstream ios: : out ifstream ios:

FILE OPENING Hence: … class default mode parameter ofstream ios: : out ifstream ios: : in | ios: : out http: //www. cplus. com/doc/tutorial/files/

FILE OPENING Two types of files: File streams opened in binary mode perform input

FILE OPENING Two types of files: File streams opened in binary mode perform input and output operations independently of any format considerations. Non-binary files are known as text files, and some translations may occur due to formatting of some special characters (like newline and carriage return characters). http: //www. cplus. com/doc/tutorial/files/

CHECKING STATE FLAGS Four types checks: bad() Returns true if a reading or writing

CHECKING STATE FLAGS Four types checks: bad() Returns true if a reading or writing operation fails. For example, in the case that we try to write to a file that is not open for writing or if the device where we try to write has no space left. fail() Returns true in the same cases as bad(), but also in the case that a format error happens, like when an alphabetical character is extracted when we are trying to read an integer number. eof() Returns true if a file open for reading has reached the end. good() It is the most generic state flag: it returns false in the same cases in which calling any of the previous functions would return true. Note that good and bad are not exact opposites (good checks more state flags at once). clear() can be used to reset the state flags. http: //www. cplus. com/doc/tutorial/files/

NON-SEQUENTIAL FILE i/o IS POSSIBLE // obtaining file size #include <iostream> #include <fstream> using

NON-SEQUENTIAL FILE i/o IS POSSIBLE // obtaining file size #include <iostream> #include <fstream> using namespace std; int main () { streampos begin, end; // “size” type ifstream myfile ("example. bin", ios: : binary); begin = myfile. tellg(); // get position, tellp() = put position for writing output myfile. seekg (0, ios: : end); // seekg ( offset, direction ); // also absolute positioning is possible seekg(position) end = myfile. tellg(); myfile. close(); cout << "size is: " << (end-begin) << " bytes. n"; // size type is similar to int return 0; } http: //www. cplus. com/doc/tutorial/files/

NON-SEQUENTIAL FILE i/o IS POSSIBLE • Types of flags for position: ios: : beg

NON-SEQUENTIAL FILE i/o IS POSSIBLE • Types of flags for position: ios: : beg offset counted from the beginning of the stream ios: : cur offset counted from the current position ios: : end offset counted from the end of the stream • Offset is always toward the end direction • However, -ve offset moves backward – toward beginning http: //www. cplus. com/doc/tutorial/files/

BINARY FILE // reading an entire binary file from disk to memory #include <iostream>

BINARY FILE // reading an entire binary file from disk to memory #include <iostream> #include <fstream> using namespace std; int main () { streampos size; char * memblock; ifstream file ("example. bin", ios: : in|ios: : binary|ios: : ate); // ios flags: input binary file, point at end if (file. is_open()) { size = file. tellg(); // size of the file is same as the pointer pointing to end-of-file memblock = new char [size]; // char, for 1 byte file. seekg (0, ios: : beg); // reposition pointer at the beginning file. read (memblock, size); // as much as can fit in the memblock file. close(); cout << "the entire file content is in memory"; … // do something with what is read in memblock delete[] memblock; } else cout << "Unable to open file"; return 0; } http: //www. cplus. com/doc/tutorial/files/

WRITING ON DISK IS NOT NECESSARILY IMMEDIATE: Stream is flushed out time to time:

WRITING ON DISK IS NOT NECESSARILY IMMEDIATE: Stream is flushed out time to time: • When the file is closed • When the buffer is full • Explicitly, with manipulators: flush and endl • Explicitly, with member function sync() http: //www. cplus. com/doc/tutorial/files/

SAMPLE FILE I/O, C-STYLE #include <cstdio> #include <cstdlib> int main() { FILE* fp =

SAMPLE FILE I/O, C-STYLE #include <cstdio> #include <cstdlib> int main() { FILE* fp = std: : fopen("test. txt", "r"); if(!fp) { std: : perror("File opening failed"); return EXIT_FAILURE; } int c; // note: int, not char, required to handle EOF while ((c = std: : fgetc(fp)) != EOF) { // C-style character reading from a file in loop std: : putchar(c); // goes to the standard output device, e. g. , terminal, for file output fput() } if (std: : ferror(fp)) std: : puts("I/O error when reading"); else if (std: : feof(fp)) std: : puts("End of file reached successfully"); std: : fclose(fp); } // see http: //www. cplus. com/reference/cstdio/fopen/ http: //en. cppreference. com/w/cpp/io/c/fopen

FILE I/O, C-STYLE fcloses a file (function) fflush synchronizes an output stream with the

FILE I/O, C-STYLE fcloses a file (function) fflush synchronizes an output stream with the actual file (function) freopen an existing stream with a different name (function)

FILE I/O, IN C: a sample of reading compressed binary file with multiple data

FILE I/O, IN C: a sample of reading compressed binary file with multiple data types in it strcpy(basis_fname, argv[5]); if((fid = fopen(basis_fname, "rb"))==NULL) // open for reading binary file { fprintf (stderr, "Could not open basis file %s for reading. n", basis_fname); exit(1); } fread (&tb. Num, sizeof(int), 1, fid); // Two items of meta data read first fread (&tb. Len, sizeof(int), 1, fid); basis = (float **) malloc(sizeof(float *) * tb. Num); // create appropriate data structure for(index=0; index<tb. Num; index++) basis[index] = (float *) malloc(sizeof(float) * tb. Len); for(index=0; index<tb. Num; index++) if ( fread (&basis[index][0], sizeof(float), tb. Len, fid) != tb. Len) // main reading part { fprintf (stderr, "Basis file %s is corrupt, not long enough. n", basis_fname); exit(1); } fclose(fid); © code from our research

https: //stackoverflow. com/questions/34915157/c-cross-platform-newline-character-in-string … if you need read file in Unix saved in Windows

https: //stackoverflow. com/questions/34915157/c-cross-platform-newline-character-in-string … if you need read file in Unix saved in Windows and vice-versa you may use this std: : getline(file. Name, input. Str); input. Str. erase( std: : remove( input. Str. begin(), input. Str. end(), 'r' ), input. Str. end() ); input. Str. erase( std: : remove( input. Str. begin(), input. Str. end(), 'n' ), input. Str. end() ); It will delete all 'r' and 'n'