Example complet de bd en c (files binaires)

Soyez le premier à donner votre avis sur cette source.

Vue 13 981 fois - Téléchargée 1 096 fois

Description

Un example complet, que j'ai cree comme un projet final. Tâche: introduction des contrats, pour chaque contrat creer un compte client où il faut introduir de facturation et de paiment par client et indiquer un saldo pour chaque locataire, créer une liste des saldo's commune, back-up automatique (ici seulment pour BD "contrats")
Intêr. pour savoire-faire comment creer, supprimer, sauveg., afficher sans risques des perdre vos BD, autonumerotation etc. Et "pardon" pour franç. - je suis etrang.
Envoyez vos commentaires!

Source / Exemple :


#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include <io.h>
#include <conio.h>
#define sizenom 20
#define sizeprenom 20
#define sizecar 12
#define sizerue 20
#define sizenrue 4
#define sizenapp 4
#define sizeville 15
#define sizecod 4
#define dateex 9
#define datpai 9
#define nomresp 20
#define sizeteleph 15
#define a 100

struct contrat
{
        int num_contrat;
        char stdnom[sizenom];
        char stdprenom[sizeprenom];
        char stdcarteid[sizecar];
        char rue[sizerue];
        char num_rue[sizenrue];
        char num_app[sizenapp];
        char ville[sizeville];
        char code_postal[sizecod];
        char date_expir[dateex];
        char date_paiment[datpai];
        float somme_mois;
        char nom_respons_contr[nomresp];
        char telephone[sizeteleph];
        char dc[9];
};

struct compta
{
int numzap;
char date_facturation[9];
float facture;
char date_facture[9];
char date_paiment[9];
float somef;
float saldo;
};

struct saldo_com
{
int id;
float saldo_indiv;
};

struct saldo_com temp; //pour recherche

float tesal; //globale variable pour enreg saldo
void menu(void);
void novbase(void);
void add(void);
void delenreg( FILE * );
void update( FILE * );
void affich( FILE * );
void deltotal(void);
int autonumer(void);
float roman(int, int );
int copyfile( int , int );
void creationcompte(char *);
float ouvrircompte(char *, int);
void affcompt(FILE *);
int nombrecontrats(void);
void creationsaldo(void);
void autosal(void);
float recherchid(int);
void updsaldo(int, float);
void afficheglobal(void);
int refcompt(void);
void refsceen(void);
void risunik(void);

void main(void)
{

menu();
}

//***********************Menu*****************************
void menu(void)

{
    char dest[20],*nom_file;
		char  nz[9];
                char *c =".dll";       
		char *disc="C:\\";
		int fdin,fdout,i,mon,mona,monac;
		float novzntesal;
	char c1;
_strdate(nz); 
L1:
       
       
	{
		FILE *opn; FILE *sald;
                if ( (opn = fopen ( "C:\\stdcontrat.dat", "rb" ) ) != NULL )
			printf( "\n" );
		else
		{
       
		novbase();
		}
		fclose(opn);
	if ( (sald = fopen ( "C:\\saldoc.dat", "rb" ) ) != NULL )
			printf( "\n" );
		else
		{
       		creationsaldo();
		}
		fclose(sald);	
	}
	system("cls");
         refsceen();
         risunik();
        
        printf("\t---------------TRAVAIL AVEC CONTRATS-----------------\n\n");
        printf( "\tIntroduir un nouveau contrat..........................[n]\n" );
        printf( "\tChanger enregistrement en contrat.....................[c]\n" );
        printf( "\tSupprimer contrat.....................................[s]\n" );
        printf( "\tAfficher la liste des toutes contrats avec Editeur....[l]\n" );
        printf( "\tSupprimer la base des donnees des contrats............[d]\n\n" );
        printf("\t----------TRAVAIL AVEC COMPTES DES CLIENTS----------\n\n");
        printf("\tIntroduir Facturation ou Paiment......................[m]\n");
        printf("\tAfficher compte du client.............................[a]\n");
        printf("\tListe des Saldo's global .............................[x]"); 
        printf("\n");	
        printf( "\tSortir................................................[q]\n\n" );
        printf( "\tVotre choix: " );
	scanf( "%c", &c1 ); 
c1 =toupper(c1);
	fflush( stdin );
        if ( c1=='N')
		add();
        else if ( c1=='C')
	{
		{
			FILE *opn;
                        if ( (opn = fopen ( "C:\\stdcontrat.dat", "rb+" ) ) != NULL )
				printf( "\n" );
			else
			{
                                printf( "Base de donnees n'existe pas.creation...\n" );
				novbase();
			}
			system( "cls" );
                          refsceen();
                         risunik();

			update(opn);
			fclose(opn);
		}
	}
        else if ( c1== 'S')
	{
                printf( "Supprission un contrat en base de donnees.....\n" );
		{
			FILE *opn;
                        if ( (opn = fopen ( "C:\\stdcontrat.dat", "rb+" ) ) != NULL )
				printf( "\n" );
			else
			{
                                printf("Base de donnees n'existe pas.Creation...\n" );
				novbase();
			}
			system( "cls" );
         refsceen();
         risunik();

			delenreg( opn );
			fclose(opn);
		}
	}
        else if ( c1 == 'L')
	{
                printf( "Affichage de la base de donnees...\n" );
		{
			FILE *opn;
                        if ( (opn = fopen ( "C:\\stdcontrat.dat", "rb" ) ) != NULL )
				printf( "\n" );
			else
			{
                                printf( "Base de donnees n'existe pas.Creation..." );
				novbase();
			}
			affich( opn );
			fclose(opn);
		}
	} 
	else if ( c1 == 'D')
		deltotal();
	else if ( c1 == 'X')
		afficheglobal();
//*************SOUS_MENU_COMPTE****************************
                  else if (c1=='M') 
	{
	
	printf("Numero du compte de client:(si 0 -sortir)\n"); scanf("%d", &mon); monac=nombrecontrats()-1; 
	if(mon>monac) { 
	
        printf("\aCompte avec numero %d n'existe pas!\nVerifiez la liste des contrats [l]!\nEnfoncez une touche pour continuer...\n",mon);  
	getche();
	}
	if((mon<=monac)&&(mon!=0)) {  tesal=recherchid( mon ); novzntesal= roman( mon, 2); updsaldo(mon,novzntesal); }
	system("cls");
         refsceen();
         risunik();

	 goto L1;
}
//****************Affichage du compte*****************
             else if (c1=='A') 
	{
	printf("Numero du compte de client:(si 0 -sortir)\n"); 
	scanf("%d", &mona); monac=nombrecontrats()-1; 
	if(mona>monac) {
	printf("Compte avec numero %d n'existe pas! Verifiez la liste des contrats!\n Enfoncez une touche pour continuer...\n", mona); 	
	getche();
	}
	if((mona<=monac)&&(mona!=0)) roman( mona, 3); 
	system("cls");
	goto L1;
}
//*****QUITTER********************************
	else if ( c1 == 'Q')
	{
           		
		
		for (i=0;i<strlen(nz);i++) {if (nz[i]=='/') nz[i]='_';}
           		strcpy(dest,disc);
		strcat(dest,nz);
		strcat(dest,c);
                printf("Le programme doit effectuer le back-up la base des donnees:\n");
		nom_file=dest;
                printf("Effectue:**%s** vos donnees sont copiees!",nom_file);
		fdin=open("C:\\stdcontrat.dat",100);
		fdout=creat(nom_file, 100);
		copyfile(fdin,fdout);
		close(fdin); close(fdout);
                printf("Merci et au revoir...\n");
          		exit(1);
             
             
	}
        else if ( c1 != 'N' && c1 != 'C' && c1 != 'S' && c1 != 'L' && c1 !='Q' && c1 != 'D' &&c1!='M' &&c1!='A'&&c1!='X')
 	{
		system( "cls" );
		fflush( stdin );
		goto L1;
	}
	goto L1;
}
//**BASE CONTRAT NOUV************************
void novbase(void)
{
        int  i;
        
	{
       struct contrat blankcontrat = { 0,"", "", "", "", "", "", "", "", "","", 0,"", "","" };
            FILE *crt;
                 if ( ( crt =      
       fopen( "C:\\stdcontrat.dat", "wb" ) ) == NULL )
 		{
            
            printf( "Erreur de la creation de la base de donnees...." );
			menu();
 		}
		else
		{
			for ( i = 1; i <= a; i++ )
                        fwrite( &blankcontrat, sizeof( struct contrat ), 1, crt );
			fclose( crt );
		}
	}
	system( "cls" );
	menu();
}
//*******CREER FILE SALDO COMMUN*************************
void creationsaldo(void)
{
FILE *csa;
int i;
struct saldo_com blanksaldo ={ 0, 0 };

	if ((csa = fopen("C:\\saldoc.dat", "wb"))==NULL) printf("Erreur de la creation le file SALDO COMMUN\n");
	else {
	for (i=1;i<a;i++) fwrite (&blanksaldo, sizeof (struct saldo_com), 1, csa);
	fclose (csa);
	autosal();
	}
}

//*****UPDATE SALDO-LIST**********************
void updsaldo(int chtoid, float novzn)
{
FILE *ups;
struct saldo_com std ={ 0, 0 };
struct saldo_com blanksaldo = {0,0};

ups=fopen("C:\\saldoc.dat","rb+");
fseek(ups,(chtoid-1)*sizeof(struct saldo_com), SEEK_SET);
fread(&std, sizeof(struct saldo_com), 1, ups);
if(std.id==0) printf("Ne peux pas trouver compte\n");
else{
	fseek(ups,(chtoid-1)*sizeof(struct saldo_com), SEEK_SET);
	fwrite(& blanksaldo, sizeof(struct saldo_com), 1, ups);
	std.id=chtoid; std.saldo_indiv=novzn;
	fseek(ups,(chtoid-1)*sizeof(struct saldo_com), SEEK_SET);
	fwrite(& std, sizeof(struct saldo_com), 1, ups);
}
fclose(ups);
}

//*****AAADDDD*************************************
void add(void)
{
 FILE *ed;
 int response, num_compt_client;

 struct contrat std = { 0, "", "", "", "", "","", "", "", "", "", 0, "" ,"" ,""};
        system( "cls" );
       //  refsceen();
       //  risunik();
        // textcolor(WHITE);
        if ( ( ed = fopen( "C:\\stdcontrat.dat", "rb+" ) ) == NULL )
	{
                printf( "Erreur ouverture de la base de donnees.....\n" );
		menu();
	}
	else
	{
	     
                        do{
                        std.num_contrat=autonumer();   
		num_compt_client=std.num_contrat;
		roman(num_compt_client, 1);
              refsceen(); risunik();
               gotoxy(1,7);
        
                        printf( "NUMERO CONTRAT:  %d\n",std.num_contrat);
			fflush( stdin ); 
                        printf( "\nNOM du Client(pas des espaces):\n" );
                        fscanf( stdin, "%s", std.stdnom );
			fflush( stdin );
                        printf( "\nPRENOM du Client(pas des espaces):\n" );
                        fscanf( stdin, "%s", std.stdprenom );
			fflush( stdin );
                        printf( "\nNUMERO DE LA CARTE D'IDENTITE(pas des espaces):\n" );
                        fscanf( stdin, "%s", std.stdcarteid );
			fflush( stdin );
                        printf( "\nRUE(pas des espaces):\n" );
                        fscanf( stdin, "%s", std.rue );
			fflush( stdin );
                        printf( "\nNUMERO_RUE(pas des espaces):\n" );
                        fscanf( stdin, "%s", std.num_rue );
			fflush( stdin );
                        printf( "\nNUMERO_APPARTEMENT(pas des espaces):\n");
                        fscanf(stdin, "%s", std.num_app );
                        fflush ( stdin );
                        printf( "\nVILLE(pas des espaces):\n" );
                        fscanf( stdin, "%s", std.ville );
			fflush( stdin );
                        printf( "\nCODE POSTAL(pas des espaces):\n" );
                        fscanf( stdin, "%s", std.code_postal );
			fflush( stdin );
                        printf( "\nLA DATE DU FIN CONTRAT(pas des espaces):\n" );
                        fscanf( stdin, "%s", std.date_expir );
			fflush( stdin );
                        printf( "\nLa DATE DE PAIMENT(pas des espaces): \n");
			fscanf( stdin, "%s", std.date_paiment );
			fflush (stdin);
                        printf( "\nLa SOMME DU MOIS(pas des espaces) pour appartement: \n");
                        scanf("%f",&std.somme_mois);
			fflush(stdin);
                        printf( "\nNOM DE RESPONSABLE DE CONTRAT(pas des espaces):\n" );
                        fscanf( stdin, "%s", std.nom_respons_contr );
			fflush( stdin );
                        printf( "\nNumero telephone du CLIENT(pas des espaces):\n" );
                        fscanf( stdin, "%s", std.telephone );
			fflush( stdin );
                        _strdate(std.dc);
                        fflush( stdin );
                        fseek( ed, ( std.num_contrat - 1 ) * sizeof( struct contrat ), SEEK_SET );
                        fwrite( &std, sizeof( struct contrat ), 1, ed );
			system( "cls" );
                 refsceen(); risunik(); gotoxy(1,7);       
                 printf( "Encore contrat-appuyez sur la touche Y ; NO - zero:0  " );
  
                scanf( "%d", &response );
		fflush( stdin );
                }
while (response!=0);
		fclose( ed );
	}
	system( "cls" );
}
//**UPDATE  CONTRAT*************************
void update( FILE *up )
{
	int updt;
	char r;
  struct contrat std = { 0,"","", "", "", "", "", "", "", "", "", 0, "", "",""};
 Lu1:
        printf( "Entrez numero contrat pour changement (Si 0 (zero)-refuser: " );
        scanf( "%d", &updt );
        if(updt==0) menu();
        else{
        fflush( stdin );
	{
 struct contrat std, blankstd = { 0,"", "", "","", "", "", "", "", "","",0,"" ,"" ,""};
 fseek( up, ( updt - 1 ) * sizeof( struct contrat ),SEEK_SET );
             
 fread( &std, sizeof( struct contrat ), 1, up ); 
                 
 if (std.num_contrat == 0 )
 		{
   
 printf("\aNumero contrat %d n'existe pas.\n\n", std.num_contrat );
  
	goto Lu1;
 		}
		else
		{
                        fseek( up, ( updt - 1 ) * sizeof( struct contrat ), SEEK_SET );
                        fwrite( &blankstd, sizeof( struct contrat ), 1, up );
		}
	}
        std.num_contrat = updt;
        printf( "\nNOM CLIENT (pas des espaces):\n" );
        fscanf( stdin, "%s", std.stdnom );
	fflush( stdin );
        printf( "\nPRENOM CLIENT ((pas des espaces):\n" );
        fscanf( stdin, "%s", std.stdprenom );
	fflush( stdin );
        printf( "\nNUMERO DE LA CARTE D'IDENTITE (pas des espaces):\n" );
        fscanf( stdin, "%s", std.stdcarteid );
	fflush( stdin );
        printf( "\nRUE(pas des espaces):\n" );
        fscanf( stdin, "%s", std.rue );
	fflush( stdin );
        printf( "\nNUMERO_RUE(pas des espaces):\n" );
        fscanf( stdin, "%s", std.num_rue );
        fflush( stdin );
        printf( "\nNUMERO_APPARTEMENT(pas des espaces) :\n");
        fscanf(stdin, "%s", std.num_app );
        fflush ( stdin );
        printf( "\nVILLE(pas des espaces):\n" );
        fscanf( stdin, "%s", std.ville );
	fflush( stdin );
        printf( "\nCODE_POSTAL(pas des espaces):\n" );
        fscanf( stdin, "%s", std.code_postal );
	fflush( stdin );
        printf( "\nLA DATE DU FIN DE CONTRAT(pas des espaces):\n" );
        fscanf( stdin, "%s", std.date_expir );
	fflush( stdin );
        printf( "\nDATE DE PAIMENT jj/mm/aa(pas des espaces): \n");
	fscanf( stdin, "%s", std.date_paiment );
	fflush (stdin);
        printf(" \nLa SOMME DU MOIS pour appartement(pas des espaces)\n" );
	scanf("%f",&std.somme_mois);
	fflush (stdin);
        printf( "\nNOM DE RESPONSABLE DU CONTRAT(pas des espaces):\n" );
        fscanf( stdin, "%s", std.nom_respons_contr );
	fflush( stdin );
        printf( "\nEntez NUMERO TELEPHONE DU CLIENT(pas des espaces)\n" );
        fscanf( stdin, "%s", std.telephone );
	fflush( stdin );
        _strdate(std.dc);
        printf ("\nDate d'enregistrement: %s\n",std.dc);
        fflush( stdin );

        fseek( up, ( std.num_contrat - 1 ) * sizeof( struct contrat ), SEEK_SET );
        fwrite( &std, sizeof( struct contrat ), 1, up );
        system( "cls" );  refsceen(); risunik(); gotoxy(1,7);
        printf( "Changer l'autre contrat ?\n(Y - confirmer; autre touche (lettre ou chiffre)-refuser)" );
	scanf( "%c", &r );
	fflush( stdin );
	if ( r == 'y' ||  r=='Y' )
	{
		goto Lu1;
	}
	else
	{
		system( "cls" );
	}
       }
}
//*DELETE CONTRAT****************************************************
void delenreg( FILE *rm )
{
	int rn;
	char rq;
 struct contrat std, blankstd = { 0,"", "","", "", "", "", "", "", "", "",0,"" ,"" ,""};
  Lr1:
        printf( "Entez numero contrat pour supprimer: " );
	scanf( "%d", &rn );
	fflush( stdin );
        fseek( rm, ( rn - 1 ) * sizeof( struct contrat ), SEEK_SET );
        fread( &std, sizeof( struct contrat ), 1, rm );
        if ( std.num_contrat == 0 )
	{
                printf( "Contrat numero %d n'existe pas!.\n\n", rn );
		goto Lr1;
	}
	else
	{
                fseek( rm, ( rn - 1 ) * sizeof( struct contrat ), SEEK_SET );
                fwrite( &blankstd, sizeof( struct contrat ), 1, rm );
                printf("Contrat est suprimmé‚!");
	}
        printf( "Vous voulez supprimer l'autre contrat?\nY-confirmer; autre touche(lettre ou chiffre)-refuser: " );
	scanf( "%c", &rq );
	fflush( stdin );
	if ( rq == 'y' )
	{
		printf( "\n" );
		goto Lr1;
	}
	else
		system( "cls" );
}
//*********AFFICHAGE DES CONTRATS*******************
void affich( FILE *opncontrat )
{
	FILE *writetxt;
 struct contrat std = { 0, "","", "", "","", "", "", "", "", "", 0, "" ,"" ,""};
	system( "cls" );
	if ( ( writetxt = fopen( "C:\\database.txt", "rb" ) ) == NULL )
		goto Lv1;
	else
		system( "del C:\\database.txt" );
Lv1:
	if ( ( writetxt = fopen( "C:\\database.txt", "wb" ) ) == NULL )
	{
                printf( "Erreur de la creation de la base de donnees...." );
		fclose( writetxt );
		menu();
	}
	else
	{
                rewind( opncontrat );
	fprintf(writetxt,"%-25s%-45s%-35s%-35s%-35s%-35s%-35s%-35s%-55s%-25s%-35s%-45s%-35s%-35s%-30s\n", "NUM_CONTRAT.", "NOM CLIENT", "PRENOM CLIENT", "CARTE IDENT", "RUE", "NUM_RUE", "NUM_APP", "VILLE","CODE_POSTAL",
                "DATE_FIN_CONTR", "DATE_PAIM","SOMME_MOIS","RESPONS","TELEPH_CLIENT","DATE_ ENR" );
                 while (!feof( opncontrat ) ) {

                fread( &std,sizeof( struct contrat ), 1, opncontrat );
                 if (std.num_contrat != 0 )
fprintf( writetxt,"%-25d%-45s%-35s%-35s%-35s%-35s%-35s%-35s%-55s%-25s%-35s%-45f%-35s%-35s%-30s\n",
std.num_contrat, std.stdnom, std.stdprenom, std.stdcarteid, std.rue,
std.num_rue, std.num_app, std.ville, std.code_postal, std.date_expir,
std.date_paiment, std.somme_mois,std.nom_respons_contr, std.telephone,std.dc );
 
	}
 	 
fclose( writetxt );
 	}
     
printf( "Creation de la base de donnees pour affichage.....\n" );
 	
system( "edit C:\\database.txt" );
system( "cls" );
 }
//****DELETE TOTAL******************************
void deltotal(void) { 
char k; 
system( "cls" );         
printf( "\aAttention!!!\nVous etes sur que vous voulez supprimer la base de donnees?\nY-confirmer,autre touche(lettre ou chiffre) - refuser: " );
  
scanf( "%c", &k ); 
fflush( stdin );  
if ( k == 'y' )                goto Ld1;
	else
	{
		system( "cls" );
		menu();
	}
Ld1:
        system( "del C:\\stdcontrat.dat" );
	system( "cls" );
	menu();
}
//************AUTONUMER******************************************
int autonumer(void)
{
  char NOM_FICH[] = "autonumer.txt"; 
  FILE *F;
  int h,nres;
   F= fopen(NOM_FICH, "rb");  fscanf(F, "%d", &h);  nres=h;   fclose(F);  F= fopen(NOM_FICH, "wb");   fprintf(F, "%d", h+1); 
   fclose(F);   return (nres);
} 
//*****COPIFILE**********************************************  
int copyfile( int from, int to )
	
{
	char buffer[ BUFSIZ ];
	int n;
	while(( n = read( from, buffer, BUFSIZ )) > 0 )
	
		    if( write( to,  buffer, n ) != n ){
                        printf( "\nEt cette copie quelq'un a cree avant vous...\n" );
			return 0;
		    }
	return 1;

}

//************CREATION FILE COMPTE OK*********************************************************//
void creationcompte(char *nomcom)
{
int i;
{
struct compta blankcompta = {0, "", 0, "", 0, 0 };
FILE *cc;

if((cc=fopen( nomcom, "wb"))==NULL) {
printf("Erreur de creation du compte de client\n");
}
else
{
for (i=1;i<=50;i++) 
 fwrite (&blankcompta, sizeof(struct compta), 1, cc);
fclose(cc);
}
}

}
//***UPDT  COMPT*************************************************
float ouvrircompte(char *nomouvrir, int qoui)
{
FILE *money;
FILE *affmoney;
char choix;
float novtesal=0;
struct compta std={0,"   ",0,"   ",0,0};
system("cls");
if(qoui==2){
	if ((money=fopen(nomouvrir,"rb+"))==NULL) {
        printf("\aErreur ouverture du compte !\nVous etes sur que contrat avec numero que vous avez tapez existe?\nPour verifier choisissez [l] dans le menu\n");
	}
        else {  refsceen(); risunik();
	//printf("Numero d'enregistrement:\n");
	//scanf("%d",&
                   std.numzap= refcompt();
	fflush(stdin);
	printf("Compte du client est enregistre sur nom  %s\n",nomouvrir);
	printf("Vous voulez introduir FACTURATION [f] ou PAIMENT[p] ?\n");
	do{
	scanf("%c",&choix); choix=toupper(choix);
	}while (choix!='F' &&choix!='P');
	if(choix=='F') {
	printf("Date de facturation:\n");
	fscanf(stdin,"%s",std.date_facturation);
	fflush(stdin);
	printf("Somme que vous avez facturee:\n");
	scanf("%f",&std.facture);
	fflush(stdin);
	}
	else {
	printf("Date ou paiment effectue:\n");
	fscanf(stdin,"%s",std.date_paiment);
	fflush(stdin);
	printf("Somme que vous avez recu:\n");
	scanf("%f",&std.somef);
	fflush(stdin);
	}
	std.saldo=tesal-std.facture+std.somef;
	novtesal=std.saldo;
	fseek( money, (std.numzap-1)*sizeof(struct compta), SEEK_SET);
	fwrite(&std, sizeof(struct compta), 1,  money);
	fclose(money);
	}
	if ((money=fopen(nomouvrir,"rb+"))!=NULL) {
	printf("\n");
	}
	else
	{
	printf("Erreur ouverture du compte !\n");
	}
	affcompt(money);
	fclose(money);

}
if(qoui==3){
	if ((money=fopen(nomouvrir,"rb+"))!=NULL) {
	printf("\n");
	}
	else
	{
	printf("Erreur ouverture du compte !\n");
	}
	affcompt(money);
	fclose(money);
}
return(novtesal);
}
//********AFFICHAGE COMPTE*****************************
void affcompt(FILE *opncompt)
{
FILE *writecompt;
struct compta std={0,"",0,"",0,0};
system("cls");

if((writecompt=fopen("C:\\affichecompte.txt", "rb"))==NULL)
goto NN;
else
	system("del C:\\affichecompte.txt");
NN:
	if((writecompt=fopen("C:\\affichecompte.txt", "wb"))==NULL) {
	printf("Erreur de la creation d'affichage..\nVous etes sur que contrat avec numero que vous avez tapez existe?\nPour verifier choisissez [l] dans le menu\n");
	fclose( writecompt);
	}
	
else
	{
	rewind(opncompt);
	fprintf(writecompt, "%-5s%-15s%-15s%-15s%-15s%-15s\n", "N°", "DATE_F", "DATE_F", "DATE_PAIM", "SOMME_PAIM", "SALDO");
	while(!feof(opncompt)) {  
	fread(&std,sizeof(struct compta),1,opncompt); 
	if(std.numzap!=0){ 
fprintf( writecompt, "%-5d%-15s%-15.2f%-15s%-15.2f%-15.2f\n", std.numzap, std.date_facturation, std.facture, std.date_paiment, std.somef, std.saldo);
 }
}
fclose (writecompt);
}
printf("Creation du Compte pour affichage...\n");
system("edit C:\\affichecompte.txt");
system("cls");

}
//****************************Controle nombre des contrats****************************
int nombrecontrats(void)
{
  char NOM_FICH[] = "autonumer.txt"; 
  FILE *ncm; int x;
  ncm= fopen(NOM_FICH, "rb");  fscanf(ncm, "%d", &x);     fclose(ncm);  
  return (x);
}  

/////**************************AUTASALDO****************************************************
void autosal(void)
{
struct saldo_com std ={ 0, 0 };
FILE *cs;
int i;
if ( (cs = fopen ("C:\\saldoc.dat", "rb+"))==NULL)  printf("Erreur de ouverture du file SALDO COMMUN\n");
	else{
		
		for(i=1;i<=a;i++) {
		std.id=i;
		std.saldo_indiv=0;
		fseek( cs, (std.id-1)*sizeof(struct saldo_com), SEEK_SET);
		fwrite(&std, sizeof(struct saldo_com), 1, cs);
		
		}
	}
fclose(cs);
}

//********SEARCH SALDO INDIVIDUEL****************************
float recherchid( int kid )
{
FILE *rech;  
int flag=0;
float repon;
//  int kid;
  //clrscr();
  rech=fopen("C:\\saldoc.dat","rb");
//  printf("ID du client:");
//  scanf("%d",&kid);
  while((!flag)&&(fread(&temp,sizeof(temp),1,rech))==1)
     {
       if(temp.id==kid)
       flag=1;
     }
     if(flag)
     {
	 
	// printf("\nSaldo client:  %f",temp.saldo_indiv);
	repon=temp.saldo_indiv;
	
     }
     else	 printf("\n");
  fclose(rech);
return (repon);
}

//********CREATION NOM DU COMPTE*****************************
float roman(int input, int test)
    {
 	float novs=0;
	 int ones, tens, hundreds, thousands, temp,  x;
    	char rOnes[100] = "";
    	char rTens[100] = "";
    	char rHundreds[100] = "";
    	char rThousands[100] = "";
    	char *rTemp;
     	char dest[100];
	char *disc="C:\\";
                   char *c=".dat";
       	char *nomcompte;
    	strcpy(dest,disc);   
	 	thousands = input/1000;
        		hundreds = (input-(thousands*1000))/100;
        		tens= (input-(thousands*1000 + hundreds*100))/10;
        		ones= (input-(thousands*1000 + hundreds*100 + tens*10));
        		temp = 0;
        		
        	while(temp!=thousands)

            	{
            		rTemp = "M";
            		strcat(rThousands, rTemp);
            		temp++;
            	}
            	
            	temp = 0;
            	
            	if(temp!=hundreds)

                	{
                		if(hundreds==1) strcpy(rHundreds, "C");
                		if(hundreds==2) strcpy(rHundreds, "CC");
                		if(hundreds==3) strcpy(rHundreds, "CCC");
                		if(hundreds==4) strcpy(rHundreds, "CD");
                		if(hundreds==5) strcpy(rHundreds, "D");
                		if(hundreds==6) strcpy(rHundreds, "DC");
                		if(hundreds==7) strcpy(rHundreds, "DCC");
                		if(hundreds==8) strcpy(rHundreds, "DCCC");
                		if(hundreds==9) strcpy(rHundreds, "CM");
                	}
                	
                  temp = 0;
          	if(temp!=tens)

                    	{
                    		if(tens==1) strcpy(rTens, "X");
                    		if(tens==2) strcpy(rTens, "XX");
                    		if(tens==3) strcpy(rTens, "XXX");
                    		if(tens==4) strcpy(rTens, "XL");
                    		if(tens==5) strcpy(rTens, "L");
                    		if(tens==6) strcpy(rTens, "LX");
                    		if(tens==7) strcpy(rTens, "LXX");
                    		if(tens==8) strcpy(rTens, "LXXX");
                    		if(tens==9) strcpy(rTens, "XC");
                    	}
                    	
              
                    	temp = 0;
          		if(temp!=ones)

                        	{
                        		if(ones==1) strcpy(rOnes, "I");
                        		if(ones==2) strcpy(rOnes, "II");
	                        	if(ones==3) strcpy(rOnes, "III");
                        		if(ones==4) strcpy(rOnes, "IV");
                        		if(ones==5) strcpy(rOnes, "V");
                        		if(ones==6) strcpy(rOnes, "VI");
                        		if(ones==7) strcpy(rOnes, "VII");
                        		if(ones==8) strcpy(rOnes, "VIII");
                        		if(ones==9) strcpy(rOnes, "IX");
                        	}
                        	
                              
	        	strcat(dest, rThousands);     strcat(dest, rHundreds); 
		strcat(dest, rTens);  strcat(dest, rOnes);
	                   strcat(dest,c); nomcompte=dest;
                        	if (test==1) creationcompte(nomcompte); 
		if (test==2) novs=ouvrircompte(nomcompte, 2);
		if (test==3) ouvrircompte(nomcompte, 3);
                        	strcpy(rOnes, "");
                        	strcpy(rTens, "");
                        	strcpy(rHundreds, "");
                        	strcpy(rThousands, "");

return(novs);
                        
                }

//*******AFFICH SALDO GLOBAL*******************************************************
void afficheglobal(void)
{
FILE *opnsal;
FILE *writesal;
int i;
struct saldo_com sl={0,0};
system("cls");
opnsal=fopen("C:\\saldoc.dat", "rb+");
if(( writesal=fopen("C:\\afsaldoc.txt", "rb"))==NULL)
goto NN;
else
	system("del C:\\afsaldoc.txt");
NN:
	if((writesal=fopen("C:\\afsaldoc.txt", "wb"))==NULL) {
	printf("Erreur de la creation d'affichage..\n");
	//fclose( writesal);
	}
	
else
	{
	rewind(opnsal);
	fprintf(writesal, "%-5s%-15s\n", "N_contr",  "SALDO");
	while(!feof(opnsal)) {  
	fread(&sl,sizeof(struct saldo_com),1,opnsal); 
	if (sl.id!=0)
	fprintf( writesal, "%d %f\n", sl.id, sl.saldo_indiv);

}
fclose (writesal);
}
printf("Creation du Compte pour affichage...\n");
system("edit C:\\afsaldoc.txt");
system("cls");

}
//****************************************************************************
int refcompt(void)
{
  char NOM_FICH[] = "C:\\refcompt.txt"; 
  FILE *ref;
  int h,nres;
  ref= fopen(NOM_FICH, "rb");  fscanf(ref, "%d", &h);  nres=h;   fclose(ref);  ref= fopen(NOM_FICH, "wb");   fprintf(ref, "%d", h+1); 
   fclose(ref);   return (nres);
} 
//************************************
void refsceen(void)
{
clrscr();
textcolor(WHITE);
textbackground(BLACK);
gotoxy(1,25);
cprintf("                                                                  ");
clrscr();
textcolor(WHITE);
textbackground(BLUE);
}
//*****************************************************
void risunik(void)
{
gotoxy(1,1);
cprintf("________________________________________________________________________________");
gotoxy(1,2);
cprintf("     Projet final de Natpoitiers  E-mail: natpoitiers@tiscali.be                 ");
gotoxy(1,3);
cprintf("                 Powered by Notepad     All rights are free                         ");
gotoxy(1,4);
cprintf("--------------------------------------------------------------------------------");
}

Conclusion :


1.Vous devez copier encore 2 files: autonumer.txt et refcompt.txt sur disque C:// (uo creez vous- même : autonumer.txt - seulement tapez n° 1 et refcompt.txt - avec n° 1 aussi)
2. Attention, placez vos files d'example directement sur C://
Merci pour vos commentaires.

Codes Sources

A voir également

Ajouter un commentaire Commentaires
le boulet Messages postés 15 Date d'inscription lundi 7 juin 2004 Statut Membre Dernière intervention 28 juin 2004
30 juin 2004 à 11:14
C'est un super code que tu as fait. Du bon travail. J'ai juste une êtite question : Quel compilateur utilises tu ?

En C, le challenge est pas mal Félicitation.

@++.
cs_Ares Messages postés 18 Date d'inscription jeudi 22 juin 2000 Statut Membre Dernière intervention 28 août 2003
7 juil. 2002 à 11:43
Bien, mais je te conseil de code en C++ avec les class de la STL, <vector> et <string>

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.