|
-
January 15th, 2009, 10:39 PM
#1
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();
}
-
January 15th, 2009, 11:11 PM
#2
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 <<
-
January 16th, 2009, 01:09 AM
#3
Re: How to convert from ofstream to CFile
in the loop?
what about the things outside the loop?
can you write the program, please?
-
January 16th, 2009, 01:47 AM
#4
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.
-
January 16th, 2009, 02:03 AM
#5
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..
-
January 16th, 2009, 03:48 AM
#6
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
}
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|