Read/write file AMELIORER MON AFFREUX CODE

elroulianito Messages postés 17 Date d'inscription lundi 27 janvier 2003 Statut Membre Dernière intervention 31 janvier 2009 - 12 mai 2003 à 16:55
BruNews Messages postés 21040 Date d'inscription jeudi 23 janvier 2003 Statut Modérateur Dernière intervention 21 août 2019 - 12 mai 2003 à 17:08
voila g déposer une source comportant une fonction qui a besoin d'être écrite proprement.

(l'appli fonctionne et tous les fichiers du projet sont dispo)

voila la fonction très vilaine:

void CDlg_textDlg::OnSave()

FILE *fp;
int nFileLong;
double X,Y,Z;

UpdateData(TRUE); //Force data from Edit box to variable

CFileDialog m_ldFile(FALSE, "pud", NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, "PRELUDE files (*.pud)|*.pud|All files|*.*||");


if (m_ldFile.DoModal() == IDOK) //Start file dlg box
{
int NumberOfLines=0;
m_sOutputFileName=m_ldFile.GetPathName(); //Get file name
m_fileOut=m_sOutputFileName;
fstream input(m_sFileName,ios::in);
fstream output(m_sOutputFileName,ios::out);
nFileLong = m_stringIn.GetLength(); //Length of text in Edit box

NumberOfLines = nFileLong/(3*sizeof(double)); //BEURK!!!

for (int i=1;i<=NumberOfLines;i++)
{
input>>X>>Y>>Z;
output<<"[PT_"<<i<<"]"<<
"\nn=1"<< "\n1="<<X<<","<<Y<<","<<Z<<",0.0,0.0,0.0"<< "\nDT1=0.0,0.0,0.0"<<
"\nRT1=0.0"<<
"\nRB1=0.0"<<endl;
}
output.close();
input.close();

//C la la que ça devient moche: après fstream input( xxx),fp=fopen(xxx)
fp=fopen(m_sOutputFileName,"rb"); //Open file for reading

fseek(fp,0,SEEK_END); //Go to file end
nFileLong=ftell(fp); //Get length
char* sText = new char[nFileLong+1];//reserve string space
fseek(fp,0,SEEK_SET); //Go to file start
int j=fread(sText,1,nFileLong,fp); //Read the characters
sText[j]=0; //Set string terminating null
m_stringOut=sText; //Put text in Edit box's variable
m_string=m_stringOut;
fclose(fp);

UpdateData(FALSE);
}

}

1 réponse

BruNews Messages postés 21040 Date d'inscription jeudi 23 janvier 2003 Statut Modérateur Dernière intervention 21 août 2019
12 mai 2003 à 17:08
en MFC ??? donc Win32 exclusif, OK ???
Alors que font encore la ces flux obsoletes ?
Remplace par API Win32:
CreateFile();
ReadFile();
WriteFile();
SetFilePointer();
GetFileSize();
CloseHandle();
BruNews, ciao...
0
Rejoignez-nous