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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T12:20:26+00:00 2026-06-01T12:20:26+00:00

As part of an assignment, I need to read data from a binary file

  • 0

As part of an assignment, I need to read data from a binary file which consists of int, char datatypes of data. This binary file is divided into records 96 bytes each. I am trying to reading these 96 bytes into a char buffer and then trying to split them according to info I have. But I am getting nothing when trying to get int values from the buffer. Can you help me in this?

#include<iostream>
#include<fstream>
#include<cstdio>

using namespace std;

int main()
{
    char buffer[100];
    char *p;
    char temp[10];
    int val;
    fstream ifs,ofs;
    ifs.open("write.bin",ios::binary);
    if(ifs.read(buffer,96)) 
    {
        cout << "READ" << endl;
    }      
    p = buffer; 
    memcpy(temp,buffer,4);
    cout << temp << endl;
    val = atoi(temp); 
    cout << val << endl;
} 

I used strncpy also in place of memcpy.
The output is 0 for val and blank for temp.

  • 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-01T12:20:27+00:00Added an answer on June 1, 2026 at 12:20 pm

    atoi transforms strings (char arrays) into integers. So something like "42" would return the integer 42. The way your question is written, it sounds like the integers are simply stored as binary in the textfile.

    A simple cast of the buffer pointer to the desired type plus a dereference should do:

    /* read 96 bytes into char array buffer */
    uint32_t your_number = *(uint32_t*)buffer;
    cout << your_number << endl;
    

    If you want to use memcpy, there’s no need to copy to a char array, you can copy directly to an integer address:

    uint32_t your_number;
    memcpy(&your_number, buffer, 4);
    cout << your_number << endl;
    

    With plain C (not C++) this would be:

    uint32_t your_number;
    FILE *f = fopen("write.bin", "r");
    fread(&your_number, sizeof(uint32_t), 1, f); /* uint32's size is 4 bytes */
    fclose(f);
    printf("%d\n", your_number);
    

    I chose uint32_t as a data type, because it is guaranteed to have 32 bits/4 bytes – int might, on some platforms/compilers, be of different size.

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

Sidebar

Related Questions

so this is part of my homework assignment. need some help with it. my
I've this question from an assignment to create a Store which rent out books,
As part of a homework assignment I need to concatenate certain values in an
This is part of a homework assignment. I've got several questions asking find the
(this is indirectly a part of a much larger homework assignment) I have something
Let me establish that this is part of a class assignment, so I'm definitely
I need to write a file format that writes out data to a file
As a part of an assignment, I need to write two functions: a function
This is part of an assignment to complete a word search. I have to
This is part of an assignment. Everything that's supposed to work, already works, but

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.