diff options
Diffstat (limited to 'Bachelor/Prog2/FileIn')
| -rw-r--r-- | Bachelor/Prog2/FileIn/Demo.txt | 16 | ||||
| -rw-r--r-- | Bachelor/Prog2/FileIn/FileIn.cpp | 67 |
2 files changed, 83 insertions, 0 deletions
diff --git a/Bachelor/Prog2/FileIn/Demo.txt b/Bachelor/Prog2/FileIn/Demo.txt new file mode 100644 index 0000000..cd18069 --- /dev/null +++ b/Bachelor/Prog2/FileIn/Demo.txt @@ -0,0 +1,16 @@ +String Integer1 Integer2
+Eins 2 3
+Zwei 3 4
+Drei 4 ?
+Vier 5 6
+Fuenf ? 7
+sechs 7 8
+Sieben 8 9
+Sieben,Fuenf 9 9
+Sieben,Sechs ? ?
+Sieben Drei Viertel 9 10
+Acht 9 10
+9 10 11
+Kurz vor 10 11 12
+10 11 !
+
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;
+}
|
