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
| #include <string>
#include <fstream>
#include <iostream>
#include <sstream>
using namespace std;
template<typename T>
bool from_string( const string & Str, T & Dest )
{
// créer un flux à partir de la chaîne donnée
istringstream iss( Str );
// tenter la conversion vers Dest
return iss >> Dest != 0;
}
int main (int argc,char**argv)
{
// le constructeur de ifstream permet d'ouvrir un fichier en lecture
ifstream fichier( "TESTFILE.TXT" );
std::ofstream out( "result.csv", std::ios_base::app );
string ligne; // variable contenant chaque ligne lue
string carac;
int frameNbr=0,position=0;
int currentframeNbr=0,currentFrameCount=0;
if ( fichier ) // ce test échoue si le fichier n'est pas ouvert
{
// cette boucle s'arrête dès qu'une erreur de lecture survient
while ( getline( fichier, ligne ) )
{
if (ligne.size() != 0 && ligne.find("sendNb :"))
{
position = ligne.find("sendNb :"); //recuper la position de la chaine de caracteres
position = position +8; // se place apres les deux points
carac = ligne.at(position); //recuper le caractere qui est forcement un nombre
if(!from_string(carac,frameNbr)) //tente la conversion
{
cout << "carac :"<<carac<<endl;
cout << "no conv available"<<endl;
}
else
{
if (frameNbr != currentframeNbr)
{
out << currentframeNbr << ";"<< currentFrameCount;
currentFrameCount=0;
}
else currentFrameCount++;
}
}
}
}
else
cout << "fichier introuvable"<<endl;
return 0;
} |
Partager