Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9118267
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:06:18+00:00 2026-06-17T05:06:18+00:00

I’m trying to read an array of unsigned shorts using the Qt API. Unfortunately,

  • 0

I’m trying to read an array of unsigned shorts using the Qt API. Unfortunately, I’m not getting the desired results.

The following code

        QFile in(fileName);
        int len = in.size();
        QDataStream d(&in);

        quint16 *data = new quint16[len];
        qDebug() << data[0];
        qDebug() << data[1];
        d >> data[0];
        qDebug() << data[0];
        qDebug() << data[1];

outputs

         52685 
         52685 
         13109 
         52685 

Implying that the data is only changed at the first array position. Also, I always thought that arrays are zero initialized? Using a QByteArray doesn’t seem to work here, that’s why I’m trying to use a array of quint16 (= unsigned shorts). Using a loop may be an option, but I’m trying to avoid a costly loop where ever possible.

So, how do fill said array (data) with the desired data from the file? Is it possible to carry the data using a QByteArray?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T05:06:20+00:00Added an answer on June 17, 2026 at 5:06 am

    First of all, in.size() returns the size of the file in bytes, and since you are using unsigned shorts (which are 2 bytes each), the size of your data array should be len/2.

    Also, QDataStream is provided for serialization purposes. This means that it is mainly useful for extracting single objects at a time. See the documentation for QDataStrean for more information.

    You can extract the whole array without loops or copying with this code:

    QFile in(fileName);
    in.open(QFile::ReadOnly);
    QByteArray byteArray = in.readAll();
    
    quint16 *data = (quint16*) byteArray.data();
    

    If you only wish to read data but never modify it, you can make your program a lot faster by changing the last line to:

    const quint16* data = (const quint16*) byteArray.constData();
    

    Keep in mind however that with this (somewhat ugly) code the pointer will only be valid for the lifetime of the QByteArray object. This usually means that you can only use data until the end of the function.

    If you wish your data to persist longer than that, you must allocate the array and read directly into it:

    QFile in(fileName);
    in.open(QFile::ReadOnly);
    int len = in.size();
    
    quint16 *data = new quint16[len/2];
    in.read((char*)data, len);
    

    This way, you can access the data until you delete[] data.

    Finally, to answer your subquestion, arrays are zero-initialized only if they are globally defined (and you shouldn’t rely even on this). Arrays allocated with new or malloc are never zero-initialized for performance reasons.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I'm making a simple page using Google Maps API 3. My first. One marker
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.