W
sten schrieb:
Hallo ich hoffe jemand kann mir helfen.
Ich habe eine Textdatei die ich auslesen muss und zwar in ein Objekt....
als Beispiel habe ich hier den Inhalt bzw Auszug der text datei:
Energie Cottbus,10,4,3,3,14,13,15
Hannover 96,10,1,4,5,9,20,7
1. FC Nuernberg,11,2,7,2,13,11,13
Hertha BSC,10,4,4,2,16,12,16
Hallo Sten,
ich schlage Dir folgendes Verfahren vor:
#pragma once
#include <iosfwd>
#include <string>
// using namespace std; NIE im Header!
class Mannschaft
{
private:
std::string mannschaftsName; // besser std::string
int punkte, spieleGespielt, spieleGewonnen, spieleUnentschieden, spieleVerloren, toreErzielt, toreKassiert;
public:
//friend ostream& operator<<(ostream& out, const Mannschaft &m);
friend std::istream& operator>>( std::istream& is, Mannschaft &team); //richtig?? genau so! .. nur std:: fehlte
friend class Tabelle;
};
// File: Mannschaft.cpp
#include "Mannschaft.h"
#include <iostream>
std::istream& komma( std::istream& in )
{
char k;
if( in >> k && k != ',' )
in.setstate( std::ios_base::failbit );
return in;
}
std::istream& operator>>( std::istream& is, Mannschaft &team )
{
Mannschaft t;
if( getline( is, t.mannschaftsName, ',' )
&& is >> t.punkte >> komma >> t.spieleGespielt >> komma >> t.spieleGewonnen >> komma >> t.spieleUnentschieden
>> komma >> t.spieleVerloren >> komma >> t.toreErzielt >> komma >> t.toreKassiert
&& is.ignore( 9999, std::istream::traits_type::to_int_type( '\n' ) ) )
team = t;
return is;
}
// File: main.cpp
#include "Mannschaft.h"
#include <fstream>
#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>
int main()
{
using namespace std;
vector< Mannschaft > teams;
ifstream datei( "input.txt" );
copy( istream_iterator< Mannschaft >( datei ), istream_iterator< Mannschaft >(), back_inserter( teams ) );
// usw.
return 0;
}
Gruß
Werner