Ajouter du text a un fichier sans effecer le contenue
cybermatthieu
Messages postés7Date d'inscriptionvendredi 9 mai 2003StatutMembreDernière intervention30 septembre 2004
-
12 mai 2003 à 17:27
Bub325
Messages postés41Date d'inscriptiondimanche 9 février 2003StatutMembreDernière intervention28 avril 2010
-
3 juil. 2003 à 08:55
Je suis a la recherche une methode pour enregistrer du text dans un fichier text. Le probleme est que je rechercher dans le fichier text un TAG HTML puis par la suite lorsque que je l'ai trouver je veux inscrire du text apres.( je chercher <title> ). Le probleme est que lorsque j'ajoute le text au fichier, les caracter qui etais apres le tag se font remplacer par le text que je lui est envoyer. Comment faire pour qu'il tasse seulement les caractere deja existant puis enregistre pas la suite?
Pour ce faire j'utilise <fstream>.
Merci
Voici mon code :
void main(){
char buffer[8], fileLocation[100];
char title[150] = {"allo"};
int fSize=0;
int position;
fstream fEdit;
strcpy(fileLocation,"Permits\\test.txt");
//strcat(fileLocation,list[x]);
//strcat(fileLocation,".htm");
fEdit.open(fileLocation, ios::in|ios::out);
if( !fEdit ){
exit( 1 );
}
while( !fEdit.eof()){
fEdit.get(buffer,8,NULL);
cout << buffer;
if (!strcmp(buffer,"<title>")){
//flag=fEdit.tellg();
fEdit.seekp(fEdit.tellg());
fEdit << "Allo";
//isTitle=1;
}
position = fEdit.tellg();
fEdit.seekg(position - 6 );
}
fEdit.close();
}
Le fichier text Avant l'execution :
123456789123456789123456789<title>123456789123456789123456789123456789123456789123456789
Apres :
123456789123456789123456789<title>Allo56789123456789123456789123456789123456789123456789
Bub325
Messages postés41Date d'inscriptiondimanche 9 février 2003StatutMembreDernière intervention28 avril 2010 3 juil. 2003 à 08:55
voici un petit code que je m'était fait pour simplifier la tache pour la programmation d'un language script. Il est loin d'être complet mais il traite exactement la partie que tu cherche, ça devrais donc t'aider. Tient moi au courant.
int write(string fichier, int ligne, string texte)
{
ifstream rfile(fichier.c_str());
rfile.seekg (0,ios::end);
int taille = rfile.tellg() + texte.length();
rfile.seekg (0);
int pos = 0;
char *ftmp = new char[taille];
for (int l=1;l < ligne;l++)
{
rfile.getline(ftmp+pos,taille);
pos += rfile.gcount();
*(ftmp+pos-1) = '\n';
}
strcpy(ftmp+pos, texte.c_str());
pos += texte.length();
while (rfile.getline(ftmp+pos,taille))
{
pos += rfile.gcount();
if (*(ftmp+pos-1) == '\0')
*(ftmp+pos-1) = '\n';
}
rfile.close();