Accueil > > > COMPARATEUR DE FICHIER TEXTE
COMPARATEUR DE FICHIER TEXTE
Information sur la source
Description
Un code en CPP VC++6 pour la comparaison de 2 fichiers textes .
Source
- // GestionFichier.h: interface for the CGestionFichier class.
- //
- //////////////////////////////////////////////////////////////////////
-
- #if !defined(AFX_GESTIONFICHIER_H__07305EA4_A252_4912_8B24_78A39AA65E68__INCLUDED_)
- #define AFX_GESTIONFICHIER_H__07305EA4_A252_4912_8B24_78A39AA65E68__INCLUDED_
-
- #if _MSC_VER > 1000
- #pragma once
- #endif // _MSC_VER > 1000
-
- #include <stdio.h>
-
- class CGestionFichier
- {
- public:
- bool eoc();
- bool eof();
- CGestionFichier();
- virtual ~CGestionFichier();
- bool Open(char *Nomfichier,char * option);
- char * Readfileline();
- int Writefileline(char *line);
- bool close();
-
- private:
- FILE *m_Fichier;
- unsigned int m_Position;
- char m_Intfichier[4069];
- bool m_eof;
- bool m_eoc;
- protected:
- int eol();
- };
-
- #endif // !defined(AFX_GESTIONFICHIER_H__07305EA4_A252_4912_8B24_78A39AA65E68__INCLUDED_)
-
-
-
- // GestionFichier.cpp: implementation of the CGestionFichier class.
- //
- //////////////////////////////////////////////////////////////////////
-
-
- #include "stdafx.h"
- #include "GestionFichier.h"
- #include <string.h>
-
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
-
- CGestionFichier::CGestionFichier()
- {
- }
-
- CGestionFichier::~CGestionFichier()
- {
-
- }
-
- bool CGestionFichier::Open(char *Nomfichier,char * option)
- {
- m_eof=false;
- m_eoc=false;
- m_Position = 0;
-
- bool open = true;
- m_Fichier = fopen(Nomfichier,option);
- if (m_Fichier==NULL)
- open=false;
- return (open);
- }
-
-
-
- //extraction d'une ligne d'un fichier txt
- char * CGestionFichier::Readfileline()
- {
- char * returnline;
- int pos=0;
- int l_eol;
-
- if (m_Position==0)
- {
- fread(m_Intfichier,4096,1,m_Fichier);
- if (feof(m_Fichier)==0)
- m_eof=false;
- else
- m_eof=true;
- l_eol = eol();
- returnline = new char[l_eol];
- for (int tour = 0; tour < l_eol;tour++)
- {
- returnline[pos]=m_Intfichier[tour];
- pos++;
- }
- }
- else
- {
- if ((eol()==-1)&&(eof()==false))
- {
- fseek(m_Fichier,m_Position,SEEK_CUR);
- fread(m_Intfichier,4096,1,m_Fichier);
- if (feof(m_Fichier)==0)
- m_eof=false;
- else
- m_eof=true;
-
- }
- else if ((eof()==true)&&(eol()==-1))
- {
- returnline = new char[4096-m_Position];
- for (int tour = m_Position; tour < 4097;tour++)
- {
- returnline[pos]=m_Intfichier[tour];
- pos++;
- }
- if (tour = 4096)
- m_eoc=true;
- }
- else if ((eof()==true)&(eol()!=-1))
- {
- l_eol = eol() + m_Position;
- returnline = new char[l_eol];
- for (int tour = m_Position; tour < l_eol;tour++)
- {
- returnline[pos]=m_Intfichier[tour];
- pos++;
- }
- }
- }
- returnline[pos]=NULL;
- m_Position = m_Position + pos + 1;
-
- return (returnline);
- }
-
- int CGestionFichier::Writefileline(char *line)
- {
- int taille=-1;
- int writedon;
- do
- {
- taille++;
- }while (line[taille]!=NULL);
- line[taille]=0x0a;
- writedon=fwrite(line,taille+1,1,m_Fichier);
- return(1);
- }
- //comparaison de deux string
-
- bool CGestionFichier::eof()
- {
- return m_eof;
- }
-
- int CGestionFichier::eol()
- {
- int nbcar;
- nbcar = m_Position;
- while ((m_Intfichier[nbcar]!=0x0a)&&(nbcar!=4096))
- {
- nbcar++;
- }
- if (nbcar!=4096)
- return(nbcar - m_Position);
- else
- return(-1);
- }
-
- bool CGestionFichier::close()
- {
- if (fclose(m_Fichier)==0)
- return true;
- else
- return false;
- }
-
- bool CGestionFichier::eoc()
- {
- return m_eoc;
- }
-
-
-
-
- // compfile.cpp : Defines the entry point for the console application.
- //
-
- #include "stdafx.h"
- #include "gestionfichier.h"
- #include <stdio.h>
- #include <string.h>
-
-
- int compstring(char *string1,char *string2);
-
-
- int main(int argc, char* argv[])
- {
- int compline;
- char *intfile1;
- char *intfile2;
- CGestionFichier fichier1;
- CGestionFichier fichier2;
- CGestionFichier fichier3;
-
- if (argc < 5)
- {
- //openning first file
- if (fichier1.Open("texte1.txt"/*argv[2]*/,"r")==true)
- {
- if (fichier2.Open("result.txt"/*argv[4]*/,"w")==true)
- {
- do
- {
- intfile1 = fichier1.Readfileline();
- fichier3.Open("texte2.txt"/*argv[3]*/,"r");
- do
- {
- intfile2 = fichier3.Readfileline();
- if ((compstring(intfile1,intfile2)==0)&&(fichier3.eoc()==false))
- {
- compline=1;
- fichier2.Writefileline(intfile2);
- }
- else
- compline=0;
- }while((compline==0)&&(fichier3.eoc()!=true));
- fichier3.close();
- }while(fichier1.eoc()!=true);
- }
- }
- fichier1.close();
- fichier2.close();
- }
- else
- {
- //message mauvaise utilisation
-
- }
- return 0;
- }
-
-
-
- int compstring(char *string1,char *string2)
- {
- return(_stricmp(string1,string2));
- }
// GestionFichier.h: interface for the CGestionFichier class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_GESTIONFICHIER_H__07305EA4_A252_4912_8B24_78A39AA65E68__INCLUDED_)
#define AFX_GESTIONFICHIER_H__07305EA4_A252_4912_8B24_78A39AA65E68__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include <stdio.h>
class CGestionFichier
{
public:
bool eoc();
bool eof();
CGestionFichier();
virtual ~CGestionFichier();
bool Open(char *Nomfichier,char * option);
char * Readfileline();
int Writefileline(char *line);
bool close();
private:
FILE *m_Fichier;
unsigned int m_Position;
char m_Intfichier[4069];
bool m_eof;
bool m_eoc;
protected:
int eol();
};
#endif // !defined(AFX_GESTIONFICHIER_H__07305EA4_A252_4912_8B24_78A39AA65E68__INCLUDED_)
// GestionFichier.cpp: implementation of the CGestionFichier class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "GestionFichier.h"
#include <string.h>
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CGestionFichier::CGestionFichier()
{
}
CGestionFichier::~CGestionFichier()
{
}
bool CGestionFichier::Open(char *Nomfichier,char * option)
{
m_eof=false;
m_eoc=false;
m_Position = 0;
bool open = true;
m_Fichier = fopen(Nomfichier,option);
if (m_Fichier==NULL)
open=false;
return (open);
}
//extraction d'une ligne d'un fichier txt
char * CGestionFichier::Readfileline()
{
char * returnline;
int pos=0;
int l_eol;
if (m_Position==0)
{
fread(m_Intfichier,4096,1,m_Fichier);
if (feof(m_Fichier)==0)
m_eof=false;
else
m_eof=true;
l_eol = eol();
returnline = new char[l_eol];
for (int tour = 0; tour < l_eol;tour++)
{
returnline[pos]=m_Intfichier[tour];
pos++;
}
}
else
{
if ((eol()==-1)&&(eof()==false))
{
fseek(m_Fichier,m_Position,SEEK_CUR);
fread(m_Intfichier,4096,1,m_Fichier);
if (feof(m_Fichier)==0)
m_eof=false;
else
m_eof=true;
}
else if ((eof()==true)&&(eol()==-1))
{
returnline = new char[4096-m_Position];
for (int tour = m_Position; tour < 4097;tour++)
{
returnline[pos]=m_Intfichier[tour];
pos++;
}
if (tour = 4096)
m_eoc=true;
}
else if ((eof()==true)&(eol()!=-1))
{
l_eol = eol() + m_Position;
returnline = new char[l_eol];
for (int tour = m_Position; tour < l_eol;tour++)
{
returnline[pos]=m_Intfichier[tour];
pos++;
}
}
}
returnline[pos]=NULL;
m_Position = m_Position + pos + 1;
return (returnline);
}
int CGestionFichier::Writefileline(char *line)
{
int taille=-1;
int writedon;
do
{
taille++;
}while (line[taille]!=NULL);
line[taille]=0x0a;
writedon=fwrite(line,taille+1,1,m_Fichier);
return(1);
}
//comparaison de deux string
bool CGestionFichier::eof()
{
return m_eof;
}
int CGestionFichier::eol()
{
int nbcar;
nbcar = m_Position;
while ((m_Intfichier[nbcar]!=0x0a)&&(nbcar!=4096))
{
nbcar++;
}
if (nbcar!=4096)
return(nbcar - m_Position);
else
return(-1);
}
bool CGestionFichier::close()
{
if (fclose(m_Fichier)==0)
return true;
else
return false;
}
bool CGestionFichier::eoc()
{
return m_eoc;
}
// compfile.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "gestionfichier.h"
#include <stdio.h>
#include <string.h>
int compstring(char *string1,char *string2);
int main(int argc, char* argv[])
{
int compline;
char *intfile1;
char *intfile2;
CGestionFichier fichier1;
CGestionFichier fichier2;
CGestionFichier fichier3;
if (argc < 5)
{
//openning first file
if (fichier1.Open("texte1.txt"/*argv[2]*/,"r")==true)
{
if (fichier2.Open("result.txt"/*argv[4]*/,"w")==true)
{
do
{
intfile1 = fichier1.Readfileline();
fichier3.Open("texte2.txt"/*argv[3]*/,"r");
do
{
intfile2 = fichier3.Readfileline();
if ((compstring(intfile1,intfile2)==0)&&(fichier3.eoc()==false))
{
compline=1;
fichier2.Writefileline(intfile2);
}
else
compline=0;
}while((compline==0)&&(fichier3.eoc()!=true));
fichier3.close();
}while(fichier1.eoc()!=true);
}
}
fichier1.close();
fichier2.close();
}
else
{
//message mauvaise utilisation
}
return 0;
}
int compstring(char *string1,char *string2)
{
return(_stricmp(string1,string2));
}
Conclusion
suite aux remarques j'ai modifier les sources et j'ai créé une classe.
Pour la classe la véritable fin de fichier correpond au eoc. Le eof correspond à la fin de fichier physique, mais il ne prend pas en compte le fait que l'on lit 4096 octets systématiquement.
Par contre par manque de temps je n'ai pas encore pus tester avec des fichiers de plus 4096 octets.
Sources du même auteur
Sources de la même categorie
Commentaires et avis
|
Derniers Blogs
IMAGINE CUP 2012, MAKE A SIGN EN FINALEIMAGINE CUP 2012, MAKE A SIGN EN FINALE par junarnoalg
Voilà qui est fait, la nouvelle est officielle ! L'équipe belge "Make a Sign" va au pays des kangourous défendre son projet dans la catégorie Software Design. http://www.imaginecup.com/CompetitionsContent/Competition/WorldwideFinalists.aspx V...
Cliquez pour lire la suite de l'article par junarnoalg KINECT 1.5 IS OUT !KINECT 1.5 IS OUT ! par Vko
La version 1.5 du Kinect For Microsoft vient tout juste de sortir ! Plein de nouveautés: Tracking de squelette en Near Mode Détection en position assise Détection faciale avec un SDK dédié Documentation et des guideline (enfin) Un out...
Cliquez pour lire la suite de l'article par Vko LES ACTUALITéS DE LA SEMAINE SUR C2I.FR (14 MAI - 20 MAI) LES ACTUALITéS DE LA SEMAINE SUR C2I.FR (14 MAI - 20 MAI) par richardc
Mise à jour des Web API du 14 Mai
Réservez dès maintenant votre journée du 20 juin pour le Windows Azure Dev Camp 2012 à Paris
Mise à jour de Team Foundation Service
MechCommander 2 sur Windows 8
Entity Framework 5 Release Candidate e...
Cliquez pour lire la suite de l'article par richardc REACTIVE EXTENSIONS : CONSOMMER DES SERVICES AVEC RX PARTIE 3, LES PIèGES à éVITERREACTIVE EXTENSIONS : CONSOMMER DES SERVICES AVEC RX PARTIE 3, LES PIèGES à éVITER par Groc
Une mauvaise utilisation de rx lors de l'écriture d'une couche d'accès à des services peut conduire à des cas embarassants avec des erreurs mal gérées, des appels qui ne partent lorsqu'ils le devraient, et même des résultats incorrects . le tout nuis...
Cliquez pour lire la suite de l'article par Groc SHAREPOINT BLOG SITE, PROBLèME D'ARCHIVESSHAREPOINT BLOG SITE, PROBLèME D'ARCHIVES par junarnoalg
Dernièrement, nous avons migré le site
myTIC
vers un nouveau serveur SharePoint 2010. Dans les contenus que nous vouloins récupérer, nous avions un certain nombre de blogs.
Nous avons utilisé les commandes Power...
Cliquez pour lire la suite de l'article par junarnoalg
Forum
MATRICE TEMPLATEMATRICE TEMPLATE par hjr2610
Cliquez pour lire la suite par hjr2610 RE : SAC A DOS RE : SAC A DOS par hadjkaddour
Cliquez pour lire la suite par hadjkaddour
Logiciels
sDEVIS-FACTURES vlPRO (8.1.0.3)SDEVIS-FACTURES VLPRO (8.1.0.3)sDEVIS-FACTURES vlPRO a été mis au point pour les particuliers, créateurs, entrepreneurs, artisa... Cliquez pour télécharger sDEVIS-FACTURES vlPRO 974 Application Server (12.2.4.6)974 APPLICATION SERVER (12.2.4.6)Développez de puissantes applications dans un environnement de 'cloud computing', clusterisé, séc... Cliquez pour télécharger 974 Application Server vPicture (1.4.2.1)VPICTURE (1.4.2.1)Avec vPicture, hébergez vos images facilement et rapidement.
vPicture est un utilitaire simple, ... Cliquez pour télécharger vPicture Easy-Planning (2.2.1.6)EASY-PLANNING (2.2.1.6)Easy-Planning permet de créer des plannings sous la représentation de diagrammes et est adapté au... Cliquez pour télécharger Easy-Planning COM-BACKUP (2.0)COM-BACKUP (2.0)
COM-BACKUP est un logiciel de sauvegarde qui permet de planifier les sauvegardes de vos dossiers ...
Cliquez pour télécharger COM-BACKUP
|