CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jan 2009
    Posts
    3

    Question 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();
    }

  2. #2
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

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

  3. #3
    Join Date
    Jan 2009
    Posts
    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?

  4. #4
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    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.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  5. #5
    Join Date
    Jan 2009
    Posts
    3

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

  6. #6
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    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
    }
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

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
  •  





Click Here to Expand Forum to Full Width

Featured