Probleme avec ifstream et ofstream

kobee12 Messages postés 153 Date d'inscription dimanche 26 janvier 2003 Statut Membre Dernière intervention 31 octobre 2006 - 5 févr. 2003 à 21:01
cs_megaman Messages postés 7 Date d'inscription mercredi 10 mars 2004 Statut Membre Dernière intervention 7 novembre 2006 - 14 juin 2004 à 13:48
J'ai un petit probleme dans mon programme. J'ai voulu inserer une fonction de cryptage pour crypter et decrypter un fichier. Le probleme c'est que j'ai des erreurs dans "index.h".

Le compilateur m'ecrit : l. 75"redeclaration of class ifstream fi", l.33"class ifstream fi previously declared here", et les memes erreurs pour ofstream aux lignes 85 et 43.

J'ai mis les differents fichiers qui compose mon projet en dessous.
SI vous savez pourquoi j'ai ces erreurs, pourriez vous me le dire.
Merci d'avance pour votre aide,
Kobee12

**************index.h*********************

#include <errno.h>
#include
#include <string>
#include <fstream>

using namespace std;

void index(FILE *, char*,int);
string crypte(string x,int plus);
string decrypte(string x,int moins);

void index(FILE * fic, char* nom_fic,int n)
{

char msg[200];
int num_ligne;
char ch[200];
long cle=10;
char *fichier,*fichiercible;
string line,s,cs;

fichier=nom_fic;
fichiercible=nom_fic;
//decryptage
ifstream fi(fichier);
while(! fi.eof())
{
getline(fi, line);
cs += line+'\n';
}
fi.close();
cout << "Decryptage...\n";
s=decrypte(cs, cle);
cout << "Ecriture dans le fichier cible...\n";
ofstream fo(fichiercible);
fo << s;
fo.close();
cout << "OK\n";
//fin decyptage

/* Ouverture d'un fichier */
fic = fopen(nom_fic, "r");

rewind (fic); //repositionnement en debut de fichier

do
{
fgets(msg, sizeof(msg), fic);
sscanf(msg,"%d %[^\n]%s\n", &num_ligne, ch);
if(num_ligne == n)
{printf("%s\n",ch);}

}while(num_ligne !=n);

fclose(fic);

//cryptage
ifstream fi(fichier);
while(!fi.eof())
{
getline(fi, line);
s += line+'\n';
}
fi.close();
cout << "Cryptage...\n";
cs=crypte(s, cle);
cout << "Ecriture dans le fichier cible...\n";
ofstream fo(fichiercible);
fo << cs;
fo.close();
cout << "OK\n";
//fin cryptage
}

string crypte(string x,int plus) {
for(int i=0;i<(int)x.size();i++)
{
x[i]=x[i]+plus;
}
return(x);
}

string decrypte(string x,int moins) {
for(int i=0;i<(int)x.size();i++)
{
x[i]=x[i]-moins;
}
return(x);
}

********************************************

********************resistance.c***************
#include <stdio.h>
#include
#include <math.h>
#include <string.h>
#include <conio.h>
#include <stdlib.h>
#include
#include <windows.h>
#include "index.h"
#include "cadre.h"

#define francais "french_resistance.txt"
#define anglais "english_resistance.txt"

char* choix_langue(FILE *,char*);
void saisie_couleur(FILE *,char*,char*,char*,char*);
void Delete(char*,char*,char*);

float conversion (char *couleur, char *nom_fichier)
{
float x=10;

/*couleur=strupr(couleur); //convertit en majuscule
if(strcmp("NOIR",couleur)==0)x=0;
else if(stricmp("MARRON",couleur)==0)x=1;
else if(stricmp("ROUGE",couleur)==0)x=2;
else if(stricmp("ORANGE",couleur)==0)x=3;
else if(stricmp("JAUNE",couleur)==0)x=4;
else if(stricmp("VERT",couleur)==0)x=5;
else if(stricmp("BLEU",couleur)==0)x=6;*/

if (nom_fichier == francais)
{
//stricmp permet d'eviter l'utilisation de strupr
if(stricmp(couleur,"NOIR")==0)x=0;
else if(stricmp(couleur,"MARRON")==0)x=1;
else if(stricmp(couleur,"ROUGE")==0)x=2;
else if(stricmp(couleur,"ORANGE")==0)x=3;
else if(stricmp(couleur,"JAUNE")==0)x=4;
else if(stricmp(couleur,"VERT")==0)x=5;
else if(stricmp(couleur,"BLEU")==0)x=6;
}
else
{
if(stricmp(couleur,"BLACK")==0)x=0;
else if(stricmp(couleur,"BROWN")==0)x=1;
else if(stricmp(couleur,"RED")==0)x=2;
else if(stricmp(couleur,"ORANGE")==0)x=3;
else if(stricmp(couleur,"YELLOW")==0)x=4;
else if(stricmp(couleur,"GREEN")==0)x=5;
else if(stricmp(couleur,"BLUE")==0)x=6;
}
return(x); //x intialise a 10, donc permet d'ajouter un controle d'erreur
}

void main ()
{
int i=0;
FILE * fichier;
char* nom_fichier;
char *coul1,*coul2,*coul3;

//ouverture de depart
nom_fichier = francais;
fichier = fopen (nom_fichier,"r");
if (fichier == NULL) {
fprintf(stderr, "Error : %s %s\n", francais, strerror(errno));
exit(-1);
}

rewind (fichier); //repositionnement en debut de fichier
while(i!=3)
{
system("CLS");
clrscr();
sim_menu(1,1, 50, 12, 19);
gotoxy(12,2);
textcolor(10);
printf("############################\r\n");
gotoxy(12,3);
index(fichier, nom_fichier,0);
gotoxy(12,4);
printf("############################\r\n");
gotoxy(3,5);
printf("1.");index(fichier,nom_fichier,1);
gotoxy(3,6);
printf("2.");index(fichier,nom_fichier,2);
gotoxy(3,7);
printf("3.");index(fichier,nom_fichier,3);
gotoxy(3,9);
index(fichier,nom_fichier,4);

sim_menu(3,10,4,2,11);
gotoxy(5,11);
textcolor(15);
scanf("%d",&i);

clrscr();
switch(i)
{
case 1:nom_fichier = choix_langue(fichier,nom_fichier);
fflush(stdin);
sim_menu(12,11,25,2, 11);
gotoxy(18,10);
index(fichier,nom_fichier,5);
gotoxy(14,12);
textcolor(15);
printf("%s\n",nom_fichier);
getch();break;
case 2:saisie_couleur(fichier, nom_fichier, coul1, coul2, coul3);break;
case 3:break;
//case 4:curseur();
default:fflush(stdin);index(fichier,nom_fichier,17);getch();break;
}

}

}

char* choix_langue(FILE * fic, char* nom_fic)
{
char langue;
do
{
system("CLS");
clrscr();
sim_menu(1,1, 50, 14, 19);
gotoxy(16,3);
textcolor(10);
index(fic,nom_fic,6);
gotoxy(8,5);
index(fic,nom_fic,7);
sim_menu(20,7, 5, 2, 11);
gotoxy(22,8);
textcolor(15);
fflush(stdin);
langue=getch();

if((langue=='f')||(langue=='F')){return francais;}
else if((langue=='a')||(langue=='A')){return anglais;}
else{fflush(stdin);
gotoxy(14,12);
textcolor(15);
index(fic,nom_fic,17);getch();}
}while((langue!='f')||(langue!='F')||(langue!='a')||(langue!='A'));
}

void saisie_couleur(FILE * fic, char* nom_fic,char *coul1,char* coul2,char* coul3)
{
float r,c1,c2,c3;
coul1=new char[8];
coul2=new char[8];
coul3=new char[8];

coul1 = (char*)malloc(9*sizeof(char));
clrscr();
system("CLS");
sim_menu(10,2, 51, 2, 19);
gotoxy(11,3);
textcolor(10);
index(fic,nom_fic,13);
textcolor(15);
gotoxy(2,7);index(fic,nom_fic,14); sim_menu(12,6, 10, 2, 11);while(!(int)coul1[0]){gotoxy(14,7);textcolor(15);fflush(stdin);gets(coul1);}

gotoxy(2,10);index(fic,nom_fic,15); sim_menu(12,9, 10, 2, 11);while(!(int)coul2[0]){gotoxy(14,10);textcolor(15);fflush(stdin);gets(coul2);}
gotoxy(2,13);index(fic,nom_fic,16); sim_menu(12,12, 10, 2, 11);while(!(int)coul3[0]){gotoxy(14,13);textcolor(15);fflush(stdin);gets(coul3);}

c1=conversion(coul1,nom_fic);
c2=conversion(coul2,nom_fic);
c3=conversion(coul3,nom_fic);

gotoxy(30,10);
cout << ">-------->";

if((c1==10)||(c2==10)||(c3==10)){sim_menu(49,9, 10, 2, 11);gotoxy(51,10);index(fic,nom_fic,12);printf("\a\a");}
else
{
sim_menu(45,12, 10, 2, 11);
r=(c1*10+c2)*pow(10,c3);
if(r<1000){ gotoxy(45,10);index(fic,nom_fic,8);gotoxy(47,13);cout<<r;gotoxy(51,13);index(fic,nom_fic,9);}
else if((r>=1000)&&(r<1e6))
{
r=r/1000;
gotoxy(45,10);index(fic,nom_fic,8);gotoxy(47,13);cout<<(int)r;gotoxy(50,13);index(fic,nom_fic,10);
}
else if(r>=1e6)
{
r=r/1e6;
gotoxy(45,10);index(fic,nom_fic,8);gotoxy(47,13);cout<<(int)r;gotoxy(50,13);index(fic,nom_fic,11);
}
}

fflush(stdin);
getch();
}

void Delete(char *couleur1,char* couleur2,char* couleur3)
{
delete couleur1;
delete couleur2;
delete couleur3;
}

**********************************************

******************cadre.h*********************

int sim_menu(int, int, int, int, int);

int sim_menu(int posx, int posy, int longx, int longy, int couleur_texte)
{
//posx, posy : positionx et positiony du cadre a partir du nord-ouest
//longx, longy : longueurx et longueury du cadre
int a, b; //a : boucle barre nord b : indicateur pour savoir s'il s'agit de la barre du haut ou du bas
char caractere[9] = {0, 187, 188, 201, 200, 205, 186, 24, 25};
gotoxy(posx, posy); //en haut à gauche du cadre
int x = posx;
int y = posy;
textcolor(couleur_texte);
for (b = 1; b < 3; b++)
{
if (b == 1)
cout << caractere[3]; //caractere ASCII du coin nord-ouest du cadre
else
cout << caractere[4]; //caractere ASCII du coin sud-ouest du cadre

for (a = 0; a < longx - 1; a++) //a < longx - 1, car il faut compter les coins nord-ouest et sud-ouest
{
cout << caractere[5]; //caractere ASCII de la barre horizontale du cadre
}
if (b == 1)
{
cout << caractere[1]; //caractere ASCII du coin nord-est du cadre
y += longy; //si la barre nord a déjà ete faite, il faut passer a la barre sud
gotoxy(x, y);
}
else
cout << caractere[2]; //caractere ASCII du coin sud-est du cadre
}
y = posy + 1; //Il faut revenir au nord-ouest du cadre avec un cran d'écart
x = posx;
for (b = 1; b < 3; b++)
{
for (a = 0; a < longy - 1; a++)
{
gotoxy (x, y);
cout << caractere[6]; //carcatere ASCII de la barre verticale
y++; //Aller une ligne en dessous
}
if (b == 1)
{
x += longx; //Aller au debut de la 2eme barre
y = posy + 1; //avec un cran d'écart
}
}
}

************************************************

2 réponses

cs_davidsm Messages postés 35 Date d'inscription lundi 6 janvier 2003 Statut Membre Dernière intervention 12 novembre 2004
5 févr. 2003 à 23:13
Bonjour,

Il y a effectivement un PB de double déclaration dans inedx.h dans la fonction index. J'ai mis en commentaire la 2ème déclaration, c'a passe à la compil.

Conseil :
- Mets toutes tes déclarations regroupées en-tête au lieu deles répartir.
- Si tu as besoisn de plusieurs instances "variable" d'un type change les noms : fi1, fi2, fo1 fo2

Bonne prog

void index(FILE * fic, char* nom_fic,int n)
{

char msg[200];
int num_ligne;
----

ifstream fi(fichier);
while(! fi.eof())
{
------
cout << "Ecriture dans le fichier cible...\n";
ofstream fo(fichiercible);
fo << s;

------
//cryptage
////ifstream fi(fichier);
while(!fi.eof())
-------
cs=crypte(s, cle);
cout << "Ecriture dans le fichier cible...\n";
////ofstream fo(fichiercible);
fo << cs;
---
}
0
cs_megaman Messages postés 7 Date d'inscription mercredi 10 mars 2004 Statut Membre Dernière intervention 7 novembre 2006
14 juin 2004 à 13:48
j'ai eu un peu près le meme prob que toi en faite ton prob de ofstream et ifstream enfin de la biblio fstrea.h sont du au faite à ta bibliothèque <string> ou <string.h>. Enlève là et tu veras ça marche biensur faut tout redefinir t string en char.
0
Rejoignez-nous