How to convert from ofstream to CFile
Hi all,
I'm a newbie in C++ and I have a problem converting this ofstream codes to CFile..
Anyone can help me?
void CCTImageDoc::WriteToFile(double* arr, int Cnt, char *fName)
{
ofstream fout(fName);
for (int i=0; i<Cnt; i++)
{
fout<<arr[i]<<endl;
}
fout<<flush;
fout.close();
}
Re: How to convert from ofstream to CFile
Assuming that you want text output, the simplest way would
be to use CStdioFile.
In the loop, create a CString variable, use the CString::Format
to convert the double to a CString, and then use CStdioFile::WriteString.
Note: above is not type save like ostream operator <<
Re: How to convert from ofstream to CFile
in the loop?
what about the things outside the loop?
can you write the program, please?
Re: How to convert from ofstream to CFile
Take a look in MSDN. You'll find examples there. CFile also has Flush() and Close(). But Close() does flushing, you don't have to call it explicitly.
Re: How to convert from ofstream to CFile
void CCTImageDoc::WriteToFile(float *arr, int Cnt, char *fName)
{
CFile fout(fName);
fout.Write(arr,Cnt);
}
I write it that way, but it doesn't work..
what should I do?
I really need the answer quick.. :(
Re: How to convert from ofstream to CFile
Of course it doesn't work. That's now how you write an array of floats. You have to do it like this:
Code:
void CCTImageDoc::WriteToFile(float *arr, int Cnt, char *fName)
{
CFile fout(fName);
for(int i = 0; i < Cnt; ++i)
{
fout.Write((void*)&arr[i], sizeof(float));
}
fout.Close(); // not actually necessary, the destructor calls it
}