Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QIODevice::write and QByteArray

QIODevice::write and QByteArray

Scheduled Pinned Locked Moved General and Desktop
11 Posts 3 Posters 8.4k Views
  • 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.
  • P Offline
    P Offline
    p-himik
    wrote on last edited by
    #1

    I want to write a QByteArray (which stores PDF if this is relevant) to QFile with this code:

    @QByteArray filledForm = form->getFilledForm( formsToFill.second );
    if( filledForm.isEmpty() )
    {
    qDebug() << "data is empty";
    return;
    }
    QFile formFile( fileName );
    if( !formFile.open( QIODevice::WriteOnly ) )
    {
    qDebug() << "Unable to open";
    qDebug() << formFile.errorString();
    return;
    }
    if( formFile.write( filledForm ) < 0 )
    {
    qDebug() << "Unable to write";
    qDebug() << formFile.errorString();
    return;
    }@

    End every time i get
    @Unable to write
    The supplied user buffer is not valid for the requested operation.@

    This error remains on another PC. What can cause it?

    1 Reply Last reply
    0
    • G Offline
      G Offline
      goetz
      wrote on last edited by
      #2

      It's a windows error message (would have been handy if you provided us some info about your environment in the first place, btw).

      It's a "common error":http://lmgtfy.com/?q=The+supplied+user+buffer+is+not+valid+for+the+requested+operation which unfortunately nobody has a general solution for.

      What's the exact filepath your're trying to write?
      Does it work with another path?

      http://www.catb.org/~esr/faqs/smart-questions.html

      1 Reply Last reply
      0
      • P Offline
        P Offline
        p-himik
        wrote on last edited by
        #3

        Sorry, next time i'll provide info about environment in the main post.
        Windows 7, Qt 4.7.4, MSVS2008.

        [quote author="Volker" date="1317205153"]It's a "common error":http://lmgtfy.com/?q=The+supplied+user+buffer+is+not+valid+for+the+requested+operation which unfortunately nobody has a general solution for.[/quote]

        Yes, i know, i've been searching for a few hours for solution. It's now more interesting not to know just the solution, but what in general can cause this error.

        I'm trying to write right in the Desktop folder. Each time i try an empty file is created. Other paths don't work either.

        1 Reply Last reply
        0
        • G Offline
          G Offline
          goetz
          wrote on last edited by
          #4

          [quote author="p-himik" date="1317206485"]
          [quote author="Volker" date="1317205153"]It's a "common error":http://lmgtfy.com/?q=The+supplied+user+buffer+is+not+valid+for+the+requested+operation which unfortunately nobody has a general solution for.[/quote]

          Yes, i know, i've been searching for a few hours for solution. It's now more interesting not to know just the solution, but what in general can cause this error.
          [/quote]

          It would be helpful, if you had mentioned your search efforts beforehand. We cannot know - the crystal ball is still defective and even Google didn't tell us ;-)

          [quote author="p-himik" date="1317206485"]I'm trying to write right in the Desktop folder. Each time i try an empty file is created. Other paths don't work either.
          [/quote]

          • Is it with the byte array write only or can you open/write using other methods?
          • Did you try to write a simple helloworld.txt?
          • Is a virus scanner running, which could intercept the write?
          • Can you create a file using wordpad.exe or another editor (Qt Creator)?
          • Does your byte array contain any bytes at all?

          http://www.catb.org/~esr/faqs/smart-questions.html

          1 Reply Last reply
          0
          • P Offline
            P Offline
            p-himik
            wrote on last edited by
            #5

            [quote author="Volker" date="1317247933"]It would be helpful, if you had mentioned your search efforts beforehand. We cannot know - the crystal ball is still defective and even Google didn't tell us ;-)[/quote]

            In case you need people to test crystal ball on here i am ;)

            Just wrote
            @QByteArray filledForm( "test" );@
            instead of the fist line of the code provided in the main post. And it worked, i got file containing "test".
            How can contents of QByteArray (and it's not empty - second line of the code) prevent writing this QByteArray to a file? I thought that QByteArray is just, as says it's name, an array of bytes - nothing more (considering only data of course).

            Also i forgot to mention that code in the main post works on Linux flawlessly.

            1 Reply Last reply
            0
            • G Offline
              G Offline
              giesbert
              wrote on last edited by
              #6

              It could be due to a virus scanner or other things.

              Nokia Certified Qt Specialist.
              Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

              1 Reply Last reply
              0
              • P Offline
                P Offline
                p-himik
                wrote on last edited by
                #7

                Unfortunately disabling virus scanner gave nothing.

                Oh... well, i just did a stupid thing - just to kill some time i guess. Every time QFile::write returned this error i chopped of 1kB from QByteArray and tried again to write it. At some point (let's say 'B') program crashed. And again. And again. And unexpectedly it wrote this file (at the same point 'B'). I increased chop() argument that it will chop almost to 'B' and relaunched app again. And now it crashes at QByteArray::chop. I completely confused. How the hell can QByteArray::chop() crash my app? Documentation says: "If n is greater than size(), the result is an empty byte array.". More than that, wrapping chop() with try-catch gave nothing - code in catch is never executed.

                1 Reply Last reply
                0
                • P Offline
                  P Offline
                  p-himik
                  wrote on last edited by
                  #8

                  That would be funny if it wasn't so sad. I returned all code back to it's first state (just like in the main post) and replaced 14'th line with this:
                  @if( formFile.write( filledForm.data(), filledForm.size() ) < 0 )@
                  And app crashed just at this line! The actual culprit is filledForm.data().

                  Ok, let's dig into data()'s source code.

                  1 Reply Last reply
                  0
                  • P Offline
                    P Offline
                    p-himik
                    wrote on last edited by
                    #9

                    Hm...
                    @inline const char *QByteArray::data() const
                    { return d->data; }
                    inline int QByteArray::size() const
                    { return d->size; }@
                    The first one crashes my app while the second doesn't. Am i missing something? As i can see this code can crash only and only if 'd' is null. But why than the second function is working?

                    1 Reply Last reply
                    0
                    • P Offline
                      P Offline
                      p-himik
                      wrote on last edited by
                      #10

                      A-ha! There is another one data() which calls detach(). And actually the detach() is crashing.

                      1 Reply Last reply
                      0
                      • P Offline
                        P Offline
                        p-himik
                        wrote on last edited by
                        #11

                        I'm stupid. And i'm very sorry for wasting your time.
                        In the form->getFilledForm( formsToFill.second ); i used QByteArray::fromRawData. With stack objects.
                        And now i'm wondering what made me write QByteArray::fromRawData instead of just a QByteArray's constructor.
                        And why did it work on Linux?

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved