From 33613a85afc4b1481367fbe92a17ee59c240250b Mon Sep 17 00:00:00 2001 From: Sven Eisenhauer Date: Fri, 10 Nov 2023 15:11:48 +0100 Subject: add new repo --- Bachelor/Prog2/FileIn/Demo.txt | 16 ++++++++++ Bachelor/Prog2/FileIn/FileIn.cpp | 67 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 Bachelor/Prog2/FileIn/Demo.txt create mode 100644 Bachelor/Prog2/FileIn/FileIn.cpp (limited to 'Bachelor/Prog2/FileIn') 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 +using std::cout; +using std::cerr; +using std::endl; +using std::ios; +using std::left; + +#include +using std::setw; + +#include +using std::ifstream; + +#include +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; +} -- cgit v1.2.3