Petit prob VC++

cs_coluche Messages postés 8 Date d'inscription samedi 23 novembre 2002 Statut Membre Dernière intervention 15 août 2003 - 15 août 2003 à 09:46
cs_fll Messages postés 15 Date d'inscription vendredi 15 août 2003 Statut Membre Dernière intervention 20 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

Merci d'avance a tous!! :)

COLUCHE

// lire ecrirDlg.cpp : implementation file
//

#include "stdafx.h"
#include "lire ecrir.h"
#include "lire ecrirDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
CAboutDlg();

// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA

// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL

// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CLireecrirDlg dialog

CLireecrirDlg::CLireecrirDlg(CWnd* pParent /*=NULL*/)
: CDialog(CLireecrirDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CLireecrirDlg)
m_text = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CLireecrirDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CLireecrirDlg)
DDX_Text(pDX, IDC_EDIT1, m_text);
//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CLireecrirDlg, CDialog)
//{{AFX_MSG_MAP(CLireecrirDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CLireecrirDlg message handlers

BOOL CLireecrirDlg::OnInitDialog()
{
CDialog::OnInitDialog();

// Add "About..." menu item to system menu.

// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);

CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}

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

void CLireecrirDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}

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

SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

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

//On definit les tab et Enter en format texte

CString Enter;
CString Tab;

Enter.FormatMessage("\n");
Tab.FormatMessage("\t");



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

void CLireecrirDlg::OnButton1()
{

UpdateData(FALSE);

CLireecrirDlg::Sauvegarder("c:\toto.txt",m_text,"Ajouter");

}

8 réponses

cs_Kaid Messages postés 949 Date d'inscription mardi 2 octobre 2001 Statut Membre Dernière intervention 8 juillet 2006 1
15 août 2003 à 10:58
J'ai simplement survolé le code, il y a une petite erreur ici:

void CLireecrirDlg::OnButton1() 
{

UpdateData(FALSE);

CLireecrirDlg::Sauvegarder("c:\toto.txt",m_text,"Ajouter");

}


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.

Kaid - kaid.fr.st
0
cs_coluche Messages postés 8 Date d'inscription samedi 23 novembre 2002 Statut Membre Dernière intervention 15 août 2003
15 août 2003 à 13:44
ben j'ai deja essayé sans backslash mais le resultat est le meme...

COLUCHE
0
cs_Kaid Messages postés 949 Date d'inscription mardi 2 octobre 2001 Statut Membre Dernière intervention 8 juillet 2006 1
15 août 2003 à 13:46
Il ne faut pas enlever le backslash mais un mettre un deuxième.

Kaid - kaid.fr.st
0
cs_coluche Messages postés 8 Date d'inscription samedi 23 novembre 2002 Statut Membre Dernière intervention 15 août 2003
15 août 2003 à 14:08
ce que je veux dire c'est que j'ai mis le fichier dans le meme dossier que l'exe est le chemin j'ai mis "toto.txt"
COUCHE
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
cs_Kaid Messages postés 949 Date d'inscription mardi 2 octobre 2001 Statut Membre Dernière intervention 8 juillet 2006 1
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.

Kaid - kaid.fr.st
0
cs_coluche Messages postés 8 Date d'inscription samedi 23 novembre 2002 Statut Membre Dernière intervention 15 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).

COLUCHE
0
cs_Kaid Messages postés 949 Date d'inscription mardi 2 octobre 2001 Statut Membre Dernière intervention 8 juillet 2006 1
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.

Kaid - kaid.fr.st
0
cs_fll Messages postés 15 Date d'inscription vendredi 15 août 2003 Statut Membre Dernière intervention 20 septembre 2004
15 août 2003 à 23:36
Lors du premier lancement de ton programme, le fichier que tu essaye d'écrire n'aiste pas.
La fonction :
    Verif=fichier.Open( file, CFile::modeWrite); 

ne permet pas de créer un fichier.
Essaye plutôt
    Verif=fichier.Open( file, CFile::modeWrite | CFile::modeCreate | CFile::modeNoTruncate );

Si ton fichier n'existe pas, il est créé, sinon, il est simplement ouvert.
0
Rejoignez-nous