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

  • Home
  • SEARCH
  • 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 6197037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:45:02+00:00 2026-05-24T03:45:02+00:00

I am trying to pwrite some data at some offset of a file with

  • 0

I am trying to pwrite some data at some offset of a file with a given file descriptor. My data is stored in two vectors. One contains unsigned longs and the other chars.

I thought of building a void * that points to the bit sequence representing my unsigned longs and chars, and pass it to pwrite along with the accumulated size. But how can I cast an unsigned long to a void*? (I guess I can figure out for chars then). Here is what I’m trying to do:

void writeBlock(int fd, int blockSize, unsigned long offset){
  void* buf = malloc(blockSize);
  // here I should be trying to build buf out of vul and vc
  // where vul and vc are my unsigned long and char vectors, respectively.
  pwrite(fd, buf, blockSize, offset);
  free(buf);
}

Also, if you think my above idea is not good, I’ll be happy to read suggestions.

  • 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-24T03:45:03+00:00Added an answer on May 24, 2026 at 3:45 am

    You cannot meaningfully cast an unsigned long to a void *. The former is a numeric value; the latter is the address of unspecified data. Most systems implement pointers as integers with a special type (that includes any system you’re likely to encounter in day-to-day work) but the actual conversion between the types is considered harmful.

    If what you want to do is write the value of an unsigned int to your file descriptor, you should take the address of the value by using the & operator:

    unsigned int *addressOfMyIntegerValue = &myIntegerValue;
    pwrite(fd, addressOfMyIntegerValue, sizeof(unsigned int), ...);
    

    You can loop through your vector or array and write them one by one with that. Alternatively, it may be faster to write them en masse using std::vector‘s contiguous memory feature:

    std::vector<unsigned int> myVector = ...;
    unsigned int *allMyIntegers = &myVector[0];
    pwrite(fd, allMyIntegers, sizeof(unsigned int) * myVector.size(), ...);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to merge two or more postscript files into one. I tried
Trying to separate out my LAMP application into two servers, one for php and
I am trying to write 2GB to a file using pwrite, but my code
Trying to setup an SSH server on Windows Server 2003. What are some good
Trying to find some simple SQL Server PIVOT examples. Most of the examples that
Trying to figure out how to read in multiple variables through a file. for
Trying to install roundcube webmail - had some issues related to driver configuration -
Trying to get the difference between two DateTime objects. With the below code, I
Trying to import a custom UITableViewCell and it keeps giving me a file not
Trying to implement a MSMQ-backed WCF PubSub. I understand that net.msmq is one-way; however

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.