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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T14:40:27+00:00 2026-05-22T14:40:27+00:00

I’ve a bad problem. I’m trying to write to a file via filedescriptor and

  • 0

I’ve a bad problem. I’m trying to write to a file via filedescriptor and memalign. I can write to it but only something like an wrong encoded char is written to a file.

Here’s my code:

fdOutputFile = open(outputFile, O_CREAT | O_WRONLY | O_APPEND | O_DIRECT, 0644)

void writeThis(char* text) {
    while (*text != '\0') {
        // if my internal buffer is full -> write to disk
        if (buffPositionOutput == outputbuf.st_blksize) {
            posix_memalign((void **)&bufferO, outputbuf.st_blksize, outputbuf.st_blksize);

            cout << "wrote " << pwrite(fdOutputFile, bufferO, outputbuf.st_blksize, outputOffset*outputbuf.st_blksize) << " Bytes to disk." << endl;
            buffPositionOutput = 0;
            ++outputOffset;
        }

        // buffer the incoming text...
        bufferO[buffPositionOutput] = *text;
        ++text;
        ++buffPositionOutput;
    }
}

I think it’s the alignment – can someone help me?
It writes to the file but not the correct text, just a bunch of ‘[]’-chars.

Thanks in advance for your help!

  • 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-22T14:40:27+00:00Added an answer on May 22, 2026 at 2:40 pm

    Looking at your program, here is what happens:

    1. You fill the memory initially pointed to by buffer0+buffPositionOutput (Which is where, precisely? I don’t know based on the code you give.) up to buffer0+outputbuf.st_blksize with data.
    2. You pass the address of the buffer0 pointer to posix_memalign, which ignores its current value and overwrites it with a pointer to outputbuf.st_blksize bytes of newly-allocated memory.
    3. You write data from the newly-allocated block to disk; this might be anything, since you just allocated memory and haven’t written anything there yet.

    This won’t work, obviously. You probably want to initialize your buffer via posix_memalign at the top of your function, and then just overwrite the block’s worth of data in it as you use your aligned buffer to repeatedly write data into the file. (Reset buffpositionoutput to zero after each time you write data, but don’t re-allocate.) Make sure you free your buffer when you are done.

    Also, why are you using pwrite instead of write?

    Here’s how I would implement writeThis (keeping your variable names so you can match it up with your version):

    void writeThis(char *text) {
        char *buffer0;
        size_t buffPositionOutput = 0;
        posix_memalign(&buffer0, outputbuf.st_blksize, outputbuf.st_blksize);
        while (*text != 0) {
            ++text; ++buffPositionOutput;
            if (buffPositionOutput == outputbuf.st_blksize) {
                write(fdOutputFile, buffer0, outputbuf.st_blksize);
                buffPositionOuput = 0;
            }
        }
        if (buffPositionOutput != 0) {
            // what do you want to do with a partial block of data?  Not sure.
        }
    }
    

    (For speed, you might consider using memcpy calls instead of a loop. You would need to know the length of the data to write ahead of time though. Worry about that after you have a working solution that does not leak memory.)

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

Sidebar

Related Questions

I am trying to render a haml file in a javascript response like so:
I want use html5's new tag to play a wav file (currently only supported
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I have a jquery bug and I've been looking for hours now, I can't

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.