diff options
Diffstat (limited to 'Bachelor/Prog2/FileIn/FileIn.cpp')
| -rw-r--r-- | Bachelor/Prog2/FileIn/FileIn.cpp | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/Bachelor/Prog2/FileIn/FileIn.cpp b/Bachelor/Prog2/FileIn/FileIn.cpp new file mode 100644 index 0000000..6395c39 --- /dev/null +++ b/Bachelor/Prog2/FileIn/FileIn.cpp @@ -0,0 +1,67 @@ +// reading a file that contains data in wrong format
+// Author: H.P.Weber
+// Date: 26.06.05
+
+#include <iostream>
+using std::cout;
+using std::cerr;
+using std::endl;
+using std::ios;
+using std::left;
+
+#include <iomanip>
+using std::setw;
+
+#include <fstream>
+using std::ifstream;
+
+#include <string>
+using std::string;
+using std::getline;
+
+int main() {
+
+ ifstream inFile( "Demo.txt" );
+ if( !inFile ) {
+ cerr << "Datei kann nicht geoeffnet werden.\n";
+ exit( 1 );
+ }
+
+ string headline;
+ getline( inFile, headline ); // read header
+
+ // input records from file
+ string stringVariable;
+ int intVariable1, intVariable2;
+
+ while( true ) {
+
+ std::ws( inFile ); // remove leading whitespace
+ getline( inFile, stringVariable, '\t');
+ inFile >> intVariable1;
+ inFile >> intVariable2;
+
+ if( !inFile.good() ) {
+ // if not complete, read complete line from buffer
+ inFile.clear();
+ string str;
+ getline( inFile, str, '\n' );
+ inFile.setstate( ios::failbit );
+ }
+
+ if( inFile.eof() ) break;
+
+ if( !inFile.good() ) { // record not complete
+ cout << "Unvollstaendiger Datensatz" << endl;
+ inFile.clear();
+ }
+
+ else // record complete
+ cout << left << setw( 30 ) << stringVariable << setw( 10 ) << intVariable1
+ << setw( 10 ) << intVariable2 << endl;
+ }
+
+ inFile.close();
+
+ return 0;
+}
|
