?
Hi Leute,
Danke für eure Bemühungen. findfirst/findnext klingt ja vielversprechend, aber ich weiss nicht, wie ich das in ein zeilenweise einlesendes Programm einbauen kann. Ich gebe euch mal den kompletten Code meines jetzigen Programmes! Vielleicht kann mir ja jemand einen Tipp geben wo und wie ich mein Programm ändern muss damit es mehrere Dateinen eines Ordners einliest.
#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
#include <sys/stat.h>
#include <errno.h>
using namespace std;
/****************************************************************************
* FileExists checks the existence of a file
* 1: File exists
* 0: File doesn't exist
* -1: Error
****************************************************************************/
long FileExists(string File)
{
int nRet = -1;
struct _stat buf;
// todo: _stat kein ANSI-Standard
// File exists ?
if( _stat( File.c_str(), &buf ) != 0 )
{
if(errno == ENOENT)
{
// File doesn't exist
nRet = 0;
}
}
else
{
// File exists
nRet = 1;
}
return nRet;
}
//****************************************************************************
string GetMid(string puffer, long startpos, long len)
{
string tempstring = "";
for(int i = 0; i < len; i++)
{
tempstring += puffer[startpos + i];
}
return tempstring;
}
int main(void)
{
char *daten, buffer [1025];
int dateigroesse, i=0, nCountLines=0, nCountMatch=0;
string quellpf, ant;
char ans;
do{
ifstream datein;
quellpf = "d:\\erik\\einlesen.txt";
cout <<"Der voreingetragene Quellpfad lautet: ";
cout << quellpf<<endl;
cout <<"Diesen Quellpfad benutzen?"<<endl;
cout <<"[j/n]: ";
char ant1;
cin >>ant1;
//******Existiert die Quelldatei überhaupt?**************************************
// 0: File doesn't exist
if(ant1 == 'j'||ant1 == 'J')
{
if(FileExists("d:\\erik\\einlesen.txt") == 0)
{
cout<<"Der voreingestellte Quellpfad besteht nicht!"<<endl;
cout<<"Bitte geben Sie einen neuen Quellpfad ein!"<<endl;
cin >> quellpf;
datein.open(quellpf.c_str(), ios_base::in);
if(FileExists(quellpf) == 0)
{
cout<<"Diese Quelldatei gibt es nicht!"<<endl;
return 0;
}
}
// -1: Fehler beim Einlesen
if(FileExists("d:\\erik\\out.txt") == -1)
{
cout<<"Es ist ein Fehler beim Einlesen der Datei aufgetreten!";
}
cout <<"Benutzter Quellpfad: ";
cout << quellpf<<"\n"<<endl;
datein.open(quellpf.c_str(), ios_base::in);
}
else if(ant1 != 'j'||ant1 != 'J')
{
cout <<"Bitte neuen Quellpfad eingeben!"<<endl;
cout <<"Quellpfad: ";
string quellpf2;
cin >> quellpf2;
if(FileExists(quellpf2) == 0)
{
cout<<"Diese Quelldatei gibt es nicht!"<<endl;
return 0;
}
cout <<"Benutzter Quellpfad: ";
cout << quellpf2<<"\n"<<endl;
datein.open(quellpf2.c_str(), ios_base::in);
}
//**************************************************************************
//**************************************************************************
ofstream dataus;
string zielpf1, zielpf2, Puffer;
zielpf1 = "d:\\erik\\out.txt";
cout <<"Der voreingestellte Zielpfad lautet: ";
cout << zielpf1<<endl;
cout <<"Diesen Zielpfad benutzen?"<<endl;
cout <<"[j/n]: ";
char ant2, ant3;
cin >> ant2;
if(ant2 == 'j' || ant2 == 'J')
{
/*cout <<"Benutzter Zielpfad: ";
cout <<zielpf1<<"\n"<<endl;
dataus.open(zielpf1.c_str(), ios_base::app);*/
//******Existiert die Zeildatei schon?**************************************
// 1: exists
if(FileExists("d:\\erik\\out.txt") == 1)
{
cout<<"Dieser Zielpfad besteht bereits!"<<endl;
cout<<"Diesen Pfad benutzen, Daten anhängen! [a]"<<endl;
cout<<"Neuen Pfad, neue Datei anlegen! [b]"<<endl;
cout<<"[a/b]: ";
cin >> ant3;
if(ant3 == 'a' || ant3 == 'A')
{
cout <<"Benutzter Zielpfad: ";
cout <<zielpf1<<"\n"<<endl;
dataus.open(zielpf1.c_str(), ios_base::app);
}
else if(ant3 == 'b' || ant3 == 'B')
{
cout <<"Bitte neuen Zielpfad eingeben!"<<endl;
cout <<"Zielpfad: ";
//string zielpf2;
cin >> zielpf2;
if(FileExists(zielpf2) == 1)
{
cout<<"Auch diese Datei gibt es schon!"<<endl;
cout<<"Datei trotzdem verwenden?"<<endl;
cout<<"[j/n]: ";
char ant4;
cin >>ant4;
if (ant4 == 'j'||ant4 == 'J')
{
ofstream dataus;
dataus.open(zielpf2.c_str(), ios_base::app);
}
else if (ant4 == 'n'||ant4 == 'N')
{
return 0;
}
}
cout<<"Die benutzte Zieldatei lautet: ";
cout<<zielpf2<<endl;
dataus.open(zielpf2.c_str(), ios_base::app);
}
}
// -1: Fehler beim Einlesen
if(FileExists("d:\\erik\\out.txt") == -1)
{
cout<<"Es ist ein Fehler beim Einlesen der Datei aufgetreten!";
return 0;
}
}
else if(ant2 != 'j' || ant2 != 'J')
{
cout <<"Bitte neuen Zielpfad eingeben!"<<endl;
cout <<"Zielpfad: ";
string zielpf3;
cin >> zielpf3;
dataus.open(zielpf3.c_str(), ios_base::app);
}
//**************************************************************************
//dataus.open(zielpf1.c_str(), ios_base::app);
if (datein.good())
{
i=0;
datein.seekg(0L,ios::end);
dateigroesse = datein.tellg();
datein.seekg(0L,ios::beg);
daten=new char[dateigroesse+1];
string Line_prev=string("");
string Line_out=string("");
bool bPrevLoop=false;
bool bCurrLoop=false;
string szCurrLoopNr = "";
while(datein.getline, datein.good())
{
datein.getline(buffer, 1025);
Puffer=string(buffer);
//LoopCycle-No
long pos = Puffer.find("LoopCycle");
if(pos>=0)
{
bCurrLoop=true; //
long linelen = Puffer.length(); //Findet die gesamte Zeilenlänge heraus
long startpos = Puffer.find_last_of(" ") + 1; //Position des ersten Leerzeichens
long LoopNrLen = linelen - startpos; //Differenz
szCurrLoopNr = GetMid(Puffer, startpos, LoopNrLen);
int stop = 1;
}
pos = Puffer.find("Provider");
if(pos>=0 && Puffer.find("Provider"))
{
//nCountMatch++;
bCurrLoop=true;
string szCurrProvNr = "";
long linelen = Puffer.length(); //Findet die gesamte Zeilenlänge heraus
long startpos = Puffer.find_last_of(" ") + 1; //Position des ersten Leerzeichens
long ProvNrLen = linelen - startpos; //Differenz
szCurrProvNr = GetMid(Puffer, startpos, ProvNrLen);
cout<< buffer [0] << buffer [1] <<"."<< buffer [2]<< buffer [3]<<".2004"<<" "
<< buffer [5] << buffer [6] << buffer [7] << buffer [8] << buffer [9]
<< buffer [10] << buffer [11] << buffer [12]<<";";
cout<< szCurrLoopNr<<";";
dataus<< buffer [0] << buffer [1] <<"."<< buffer [2]<< buffer [3]<<".2004"<<" "
<< buffer [5] << buffer [6] << buffer [7] << buffer [8] << buffer [9]
<< buffer [10] << buffer [11] << buffer [12]<<";";
dataus<< szCurrLoopNr<<";";
if (buffer[49]==' ')
{
cout<<"\n";
dataus<<"\n";
}
else if (buffer[49]>=0 && buffer[50]==' ')
{
cout<<buffer[49]<<";";
dataus<<buffer[49]<<";";
}
else if (buffer[49]>=0 && buffer[50]>=0 && buffer[51]==' ')
{
cout<<buffer[49]<<buffer[50]<<";";
dataus<<buffer[49]<<buffer[50]<<";";
}
else if (buffer[49]>=0 && buffer[50]>=0 && buffer[51]>=0)
{
cout<< buffer [49] << buffer [50] << buffer [51]<<";";
dataus<< buffer [49] << buffer [50] << buffer [51]<<";";
}
cout<< szCurrProvNr<<"\n";
dataus<< szCurrProvNr<<"\n";
}
Line_prev=Puffer;
bPrevLoop=bCurrLoop;
nCountLines++;
}
cout<<"\nSumme aller verglichenen Zeilen: "<<nCountLines<<"\n"<<endl;
}
cout<<"Möchten Sie mit dem Programm fortfahren?"<<endl;
cout<<"[j/n]: ";
cin>>ans;
datein.close();
dataus.close();
}
while(ans=='j'||ans=='J');
cout<<endl<<"Ende des Programms!\n"<<endl;
delete[] daten;
return 0;
}
Okay, wie jeder sehen kann, leist das Programm also zeilenweise ganz bestimmte Daten nach einem bestimmten Muster ein.
Nun gut.....Danke für eure Hilfe!
Gruß