Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. CFile Error When Writing

CFile Error When Writing

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
9 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    AmbiguousName
    wrote on last edited by
    #1

    Hello. I am trying to write some wave data into pcm file. I pass this data and its length to the write function but it raises this exception An unknown error occurred while accessing file ..... file path + name .... I am using following line code to open this file and write contents into it

    CFile* pFile = new CFile;
    pFile->Open("Output.pcm", CFile::modeCreate|modeReadWrite);
    pFile->Write((void*)pData, dwLength); // exception raised here
    pFile->Close();

    What could be wrong here? Thanks for any input.

    This world is going to explode due to international politics, SOON.

    C S J 3 Replies Last reply
    0
    • A AmbiguousName

      Hello. I am trying to write some wave data into pcm file. I pass this data and its length to the write function but it raises this exception An unknown error occurred while accessing file ..... file path + name .... I am using following line code to open this file and write contents into it

      CFile* pFile = new CFile;
      pFile->Open("Output.pcm", CFile::modeCreate|modeReadWrite);
      pFile->Write((void*)pData, dwLength); // exception raised here
      pFile->Close();

      What could be wrong here? Thanks for any input.

      This world is going to explode due to international politics, SOON.

      C Offline
      C Offline
      CPallini
      wrote on last edited by
      #2

      Why didn't you check Open return value (see, for instance, the documentation[^])?

      Veni, vidi, vici.

      A 1 Reply Last reply
      0
      • A AmbiguousName

        Hello. I am trying to write some wave data into pcm file. I pass this data and its length to the write function but it raises this exception An unknown error occurred while accessing file ..... file path + name .... I am using following line code to open this file and write contents into it

        CFile* pFile = new CFile;
        pFile->Open("Output.pcm", CFile::modeCreate|modeReadWrite);
        pFile->Write((void*)pData, dwLength); // exception raised here
        pFile->Close();

        What could be wrong here? Thanks for any input.

        This world is going to explode due to international politics, SOON.

        S Offline
        S Offline
        Software_Developer
        wrote on last edited by
        #3

        Since the exception is "An unknown error occurred while accessing file ", Do you still get errors with the unicode syntax _T( ) , e.g CFile file (_T ("File.txt"), CFile::modeReadWrite) );

        CFile file;
        CFileException e;
        if (file.Open (_T ("File.txt"), CFile::modeReadWrite, &e)) {
        // It worked!

        }
        else {
        // Open failed. Tell the user why.
        e.ReportError ();
        }

        A 1 Reply Last reply
        0
        • S Software_Developer

          Since the exception is "An unknown error occurred while accessing file ", Do you still get errors with the unicode syntax _T( ) , e.g CFile file (_T ("File.txt"), CFile::modeReadWrite) );

          CFile file;
          CFileException e;
          if (file.Open (_T ("File.txt"), CFile::modeReadWrite, &e)) {
          // It worked!

          }
          else {
          // Open failed. Tell the user why.
          e.ReportError ();
          }

          A Offline
          A Offline
          AmbiguousName
          wrote on last edited by
          #4

          The file opens successfully. The problem occurs when I try to write something using it. Please see me code again, I have commented it there as well.

          This world is going to explode due to international politics, SOON.

          S L L 3 Replies Last reply
          0
          • C CPallini

            Why didn't you check Open return value (see, for instance, the documentation[^])?

            Veni, vidi, vici.

            A Offline
            A Offline
            AmbiguousName
            wrote on last edited by
            #5

            File opens successfully. It is the Write() function that is raising this exception.

            This world is going to explode due to international politics, SOON.

            1 Reply Last reply
            0
            • A AmbiguousName

              The file opens successfully. The problem occurs when I try to write something using it. Please see me code again, I have commented it there as well.

              This world is going to explode due to international politics, SOON.

              S Offline
              S Offline
              Software_Developer
              wrote on last edited by
              #6

              The error should be obvious, the inners of write look like this..

              virtual void Write(
              const void* lpBuf,
              UINT nCount
              );

              Parameters lpBuf - A pointer to the user-supplied buffer that contains the data to be written to the file. nCount - The number of bytes to be transferred from the buffer. For text-mode files, carriage return–linefeed pairs are counted as single characters. Official MSDN example:

              CFile cfile;
              cfile.Open(_T("Write_File.dat"), CFile::modeCreate | CFile::modeReadWrite);

              char pbufWrite[100];

              memset(pbufWrite, 'a', sizeof(pbufWrite));

              cfile.Write(pbufWrite, 100);
              cfile.Flush();

              http://msdn.microsoft.com/en-us/library/esb6sz20.aspx[^]

              1 Reply Last reply
              0
              • A AmbiguousName

                Hello. I am trying to write some wave data into pcm file. I pass this data and its length to the write function but it raises this exception An unknown error occurred while accessing file ..... file path + name .... I am using following line code to open this file and write contents into it

                CFile* pFile = new CFile;
                pFile->Open("Output.pcm", CFile::modeCreate|modeReadWrite);
                pFile->Write((void*)pData, dwLength); // exception raised here
                pFile->Close();

                What could be wrong here? Thanks for any input.

                This world is going to explode due to international politics, SOON.

                J Offline
                J Offline
                Jonathan Davies
                wrote on last edited by
                #7

                include wchar.h and try

                WCHAR* pData = L"Testing Data";
                DWORD dwLength = wcslen(pData);
                CFile* pFile = new CFile;
                pFile->Open(L"Output.pcm, CFile::modeCreate | CFile::modeReadWrite);
                pFile->Write((void*)pData, dwLength * sizeof(WCHAR)); // exception raised here
                pFile->Close();

                1 Reply Last reply
                0
                • A AmbiguousName

                  The file opens successfully. The problem occurs when I try to write something using it. Please see me code again, I have commented it there as well.

                  This world is going to explode due to international politics, SOON.

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  AmbiguousName wrote:

                  The file opens successfully.

                  How can you be sure of that, since you do not check the return status?

                  1 Reply Last reply
                  0
                  • A AmbiguousName

                    The file opens successfully. The problem occurs when I try to write something using it. Please see me code again, I have commented it there as well.

                    This world is going to explode due to international politics, SOON.

                    L Offline
                    L Offline
                    leon de boer
                    wrote on last edited by
                    #9

                    Supercoder has given you the most likely answer although I agree with Richard you haven't conclusively shown the file is open. If the file doesn't exist you could have a problem because you probably need to open it with CFile::modeCreate | CFile::modeReadWrite Stick a debug point on the fail case and check. For the write you have a single line => pFile->Write((void*)pData, dwLength); Either 1.) pData is not a pointer to an actual array of data 2.) dwLength has not been set or exceeds the data in pData So all you have to do is work out which of the three things you got wrong and fix it. If you can't fix it we need to see how you defined pData and where you set dwLength probably.

                    In vino veritas

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups