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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T19:27:04+00:00 2026-06-08T19:27:04+00:00

Pointer related question. I’m going through some example code that currently reads in data

  • 0

Pointer related question. I’m going through some example code that currently reads in data from a file called dataFile into a buffer. The reading is done inside a loop as follows:

unsigned char* buffer = (unsigned char*)malloc(1024*768*);
fread(buffer,1,1024*768,dataFile);
redPointer = buffer;
bluePointer = buffer+1024;
greenPointer = buffer+768;

Now, I want to try and write the entire contents of the array buffer to a file, so that I can save just those discrete images (and not have a large file). However, I am not entirely sure how to go about doing this.

I was trying to cout statements, however I get a print-out of garbage characters on the console and also a beep from the PC. So then I end my program.

Is there an alternative method other than this:

for (int i=0; i < (1024*768); i++) {
fprintf(myFile, "%6.4f , ", buffer[i]); 
}
  • 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-08T19:27:06+00:00Added an answer on June 8, 2026 at 7:27 pm

    By declaring your buffer as a char*, any pointer arithmatic or array indexes will use sizeof(char) to calculate the offset. A char is 1 byte (8 bits).

    I’m not sure what you are trying to do with the data in your buffer. Here are some ideas:

    Print the value of each byte in decimal, encoded as ASCII text:

    for (int i=0; i < (1024*768); i++) {
        fprintf(myFile, "%d , ", buffer[i]);
    }
    

    Print the value of each byte in hexadecimal, encoded in ASCII text:

    for (int i=0; i < (1024*768); i++) {
        fprintf(myFile, "%x , ", buffer[i]);
    }
    

    Print the value of each floating point number, in decimal, encoded in ASCII text (I think my calculation of the array index is correct to process adjacent non-overlapping memory locations for each float):

    for (int i=0; i < (1024*768); i += sizeof(float)) {
        fprintf(myFile, "%6.4f , ", buffer[i]);
    }
    

    Split the buffer into three files, each one from a non-overlapping section of the buffer:

    fwrite(redPointer, sizeof(char), 768, file1);
    fwrite(greenPointer, sizeof(char), 1024-768, file2);
    fwrite(bluePointer, sizeof(char), (1024*768)-1024, file3);
    

    Reference for fwrite. Note that for the count parameter I simply hard-coded the offsets that you had hard-coded in your question. One could also subtract certain of the pointers to calculate the number of bytes in each region. Note also that the contents of these three files will only be sensible if those are sensibly independent sections of the original data.

    Maybe this gives you some ideas.

    Updated: so I created a complete program to compile and test the formatting behavior. This only prints the first 20 items from the buffer. It compiles (with gcc -std=c99) and runs. I created the file /tmp/data using ghex and simply filled in some random data.

    #include <stdlib.h>
    #include <stdio.h>
    
    int main()
        {
        FILE* dataFile = fopen("/tmp/data", "rb");
        if (dataFile == NULL)
            {
            printf("fopen() failed");
            return -2;
            }
    
        unsigned char* buffer = (unsigned char*)malloc(1024*768);
        if (buffer == NULL)
            {
            printf("malloc failed");
            return -1;
            }
    
        const int bytesRead = fread(buffer,1,1024*768,dataFile);
        printf("fread() read %d bytes\n", bytesRead);
    
        // release file handle
        fclose(dataFile); dataFile = NULL;
    
        printf("\nDecimal:\n");
        for (int i=0; i < (1024*768); i++) {
            printf("%hd , ", buffer[i]);
            if (i > 20) { break; }
        }
        printf("\n");
    
        printf("\nHexadecimal:\n");
        for (int i=0; i < (1024*768); i++) {
            printf("%#0hx , ", buffer[i]);
            if (i > 20) { break; }
        }
        printf("\n");
    
        printf("\nFloat:\n");
        for (int i=0; i < (1024*768); i += sizeof(float)) {
            printf("%6.4f , ", (float)buffer[i]);
            if (i > 20) { break; }
        }
        printf("\n");
    
        return 0;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

On a related note to this question , say I've got an file with
Related to my other question How can I get the address (acctual function pointer)
I realized that I had some confusion regarding the value of a dereferenced pointer,
This is somewhat related to another question that I've asked that I've pretty much
If I open a file in my C/C++/Java code using a pathname that goes
This is probably a stupid question as this crash is probably related to some
Derived from this question and related to this question : If I construct an
related to (gcc) Multi-Dim Array or Double Pointer for Warning-free Compile , is there
I hardly see any pointer on the following problem related to Hibernate. This pertains
How are file descriptors and file pointers related? When is it appropriate to use

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.