cs_coluche
Messages postés8Date d'inscriptionsamedi 23 novembre 2002StatutMembreDernière intervention15 août 2003
-
15 août 2003 à 09:46
cs_fll
Messages postés15Date d'inscriptionvendredi 15 août 2003StatutMembreDernière intervention20 septembre 2004
-
15 août 2003 à 23:36
Bonjour a tous,
j'ai telecharger la source SAUVEGARDER ET CHARGER AVEC CFILE de Sioban que je remercie...mais je ne comprend pas j'ai bien suivi les indication pour l'inseré dans mon code d'ailleurs j'ai aucune erreur quand je compile.
mais le problème c'est que quand j'appelle la fonction et que je lui passe les parametre j'ai aucune erreur mais il se passe rien !!!! la fonction doit ecrire dans un fichier texte mais elle ne le fait pas
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CLireecrirDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CLireecrirDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
int CLireecrirDlg::Sauvegarder(CString file,CString data,CString mode)
{
//////////////////////////////////////////////////////////////////////////////////////
//Mode Ajouter mode="Ajouter"
//Ajoute à la fin du fichier(par defaut)
//
//Mode Remplacer mode="Remplacer"
// Remplace le fichier
//////////////////////////////////////////////////////////////////////////////////////
CFile fichier;
int Verif;
int OK=0;
//////////////Mode Ajouter////////////////
if (mode == "Ajouter")
{
CString st;
st=Charger(file);
st=st.Right(1);
if(st!='\n')
{
data=Enter+data;
//on ajoute un entrée (\n) à la fin du fichier s'il n'y en à pas
}
Verif=fichier.Open(file,CFile::modeWrite);
//Ouverture du fichier en lecture/écriture
if(Verif != 0)
{
fichier.SeekToEnd(); //aller à la fin du fichier
fichier.Write(data,data.GetLength()); // sauvegarde dans le fichier
fichier.Close(); // fermeture du fichier
OK=1;
}
}
//////////////Mode Remplacer////////////////
if (mode == "Remplacer")
{
Verif=fichier.Open(file,CFile::modeCreate |CFile::modeWrite);
//Ouverture du fichier en lecture/écriture et le créer s'il n'existe pas
if(Verif != 0)
{
fichier.Write(data,data.GetLength()); // sauvegarde de la log dans un fichier
fichier.Close(); // fermeture du fichier
OK=1;
}
}
return OK; //OK=1 si pas de pb OK=0 si pb
}
//////////////////////////////////////////////////////////////////////////////////////
//En fonction du nom du fichier on le charge et on le retourne dans un
//CString st
CString CLireecrirDlg::Charger(CString file)
{
CFile fichier;
char Buffer[100000];
int Taille=100000;
CString st="";
int Buff;
int Verif=fichier.Open(file,CFile::modeRead);//Ouverture du fichier en lecture
if(Verif != 0)
{
Buff=int(fichier.GetLength());
//On recupere la taille du fichier en octets
do
{
if(Buff>Taille)
//si la taille du fichier est encore > au buffer
{
fichier.Read(Buffer,Taille);
//on en prend que 100000
CString tmp=Buffer;
st=st+tmp.Mid(0,Taille);
//on copie les 100000 dans le cstring
Buff=Buff-Taille; //on remet le nombre d'octet à charger
}else
{
fichier.Read(Buffer,Buff);
//si le buffer suffit on le lit
CString tmp=Buffer;
st=st+tmp.Mid(0,Buff); //on copie le reste dans le cstring
break;
//on casse la boucle quand on a tout lu
}
}while(true);
//boucle infinie tant que le fichier n'est pas chargé entiérement
fichier.Close(); // fermeture du fichier
}else
{
st="NULL"; //Si problème retourne NULL
}
return st; // retourne le texte dans un CString
}
/*Aide pour ceux qui ont du mal(les autres: désolé pour la trivialité des infos)
Vous pouvez ajouter dans votre projets directement le code
/!\ Il faut renommer CLireecrirDlg par le nom de votre projet
Ensuite
Il faut ajouter dans votre .h (ex: totoDlg.h) dans la partie (public:):
afx_msg int Sauvegarder(CString file,CString data,CString mode);
afx_msg CString Charger(CString file);
Et maintenant l'utilisation:
exemple d'appel des fonctions
CString texte=Charger("toto.txt");
//texte contient le texte qui était dans toto.txt
CString data="dfhsldfh...dfsf" ou une variable de type CString d'une edit box ou autre ...
Sauvegarder("toto.txt",data,"Ajouter")
// Ca ajoute dfhsldfh...dfsf à la fin du fichier toto
//On peut controler si la fonction a fonctionné avec un int
int ETAT=Sauvegarder("toto.txt",data,"Ajouter")
//si ETAT=1 pas de pb si ETAT=0 il y a eu un problème */
En C/C++ le backslash dans une chaîne de caractères suivi d'un caractère indique une séquence d'échappement. Ici "\t" désigne en fait une tabulation, il est donc normal que le fichier ne soit pas écrit.
Pour annuler cet effet, il faut mettre un second backslash juste après le premier.
cs_Kaid
Messages postés949Date d'inscriptionmardi 2 octobre 2001StatutMembreDernière intervention 8 juillet 20061 15 août 2003 à 14:12
Ok.
Si tu as fait un copier-coller d'un code qui fonctionne et qu'il ne fonctionne pas chez toi, tu peux exécuter le programme pas à pas sous le debugger pour trouver l'erreur.
cs_coluche
Messages postés8Date d'inscriptionsamedi 23 novembre 2002StatutMembreDernière intervention15 août 2003 15 août 2003 à 14:24
j'ai pas l'habitude d'utilisé le debugger mais j'ai executer le programme et il y a pas mal d'erreur je comprend pas tout.
Loaded 'ntdll.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\kernel32.dll', no matching symbolic information found.
Loaded symbols for 'C:\WINDOWS\system32\MFC42D.DLL'
Loaded symbols for 'C:\WINDOWS\system32\MSVCRTD.DLL'
Loaded 'C:\WINDOWS\system32\gdi32.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\user32.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\advapi32.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\rpcrt4.dll', no matching symbolic information found.
Loaded symbols for 'C:\WINDOWS\system32\MFCO42D.DLL'
Loaded 'C:\WINDOWS\system32\mfc42loc.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\comctl32.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\msctf.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\msvcrt.dll', no matching symbolic information found.
Loaded 'C:\Program Files\Logitech\MouseWare\system\LgWndHk.dll', no matching symbolic information found.
Loaded 'C:\Program Files\Fichiers communs\Logitech\Scrolling\LGMSGHK.DLL', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\msvcp60.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\version.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\oleaut32.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\ole32.dll', no matching symbolic information found.
The thread 0x838 has exited with code 0 (0x0).
The program 'C:\Program Files\Microsoft Visual Studio\MyProjects\lire ecrir\Debug\lire ecrir.exe' has exited with code 0 (0x0).
cs_Kaid
Messages postés949Date d'inscriptionmardi 2 octobre 2001StatutMembreDernière intervention 8 juillet 20061 15 août 2003 à 14:49
Ce ne sont pas des erreurs, c'est simplement l'output du debugger.
Ce que tu dois faire, c'est mettre un point d'arrêt où commence le code qui écrit dans un fichier puis exécuter ce code pas à pas pour trouver l'erreur.
Si tu n'es pas famillier avec le debugger, tu peux mettre des traces (des AfxMessageBox() ou afxDump) après chaque fonction appellée pour voir s'il y a des erreurs ou non.