summaryrefslogtreecommitdiffstats
path: root/Bachelor/Prog2/FileIn/FileIn.cpp
blob: 6395c39858ea23ba4fa2b0e0537229f8b64892e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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;

}