Comparateur de fichier texte

Contenu du snippet

Un code en CPP VC++6 pour la comparaison de 2 fichiers textes .

Source / Exemple :


// 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.

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.