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 919131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T18:29:30+00:00 2026-05-15T18:29:30+00:00

I am using the cstdio (stdio.h) to read and write data from binary files.

  • 0

I am using the cstdio (stdio.h) to read and write data from binary files. I have to use this library due to legacy code and it must be cross-platform compatible with Windows and Linux. I have a FILE* basefile_ which I use to read in the variables configLabelLength and configLabel, where configLabelLength tells me how much memory to allocate for configLabel.

unsigned int configLabelLength; // 4 bytes
char* configLabel = 0;          // Variable length

fread((char *) &configLabelLength, 1, sizeof configLabelLength, baseFile_);
configLabel = new char[configLabelLength];
fread(configLabel,1, configLabelLength,baseFile_);

delete [] configLabel; // Free memory allocated for char array
configLabel = 0; // Be sure the deallocated memory isn't used

Is there a way to read in configLabel without using a pointer? For example is there a solution where I can use the c++ vector library or something where I do not have to worry about pointer memory management.

  • 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-05-15T18:29:31+00:00Added an answer on May 15, 2026 at 6:29 pm

    Just do:

    unsigned int configLabelLength; // 4 bytes*
    fread((char *) &configLabelLength, 1, sizeof configLabelLength, baseFile_);
    
    std::vector<char> configLabel(configLabelLength);
    fread(&configLabel[0], 1, configLabel.size(), baseFile_);
    

    The elements in a vector are contiguous.


    * I assume you know that unsigned int isn’t necessary always 4 bytes. If you pay attention to your implementation details that’s fine, but it’ll be a bit easier if you adopt Boost’s cstdint.hpp and just use uint32_t.

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

Sidebar

Related Questions

I am using fopen to write to a binary file and using the cstdio
Given this code: #include <cstdio> #include <iostream> #include <string> using std::cin; using std::cout; using
that this snippet of code actually do? #include <iostream> #include <cstdio> #include <cstdlib> using
I'm getting compile error in this code #include<iostream> #include<cstdio> #include<string> using namespace std; void
Is this code standard conforming ? #include <stdio.h> #include <cstdio> int main() { FILE
#include <ctime> #include <cstdio> #include <sys/time.h> #include <iostream> using namespace std; int main() {
using this http://bl.ocks.org/950642 we can see how to add images to nodes, the question
Using CI for the first time and i'm smashing my head with this seemingly
Using ASIHTTPRequest, I downloaded a zip file containing a folder with several audio files.
I have a C++ program (.cpp) inside which I wish to use some of

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.