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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:26:51+00:00 2026-05-27T09:26:51+00:00

I have written a binary file using a struct as follows: struct block{ char

  • 0

I have written a binary file using a struct as follows:

struct block{
char data[32];
};

so what I end up with is basically a large binary file full of char[32]. The data is formatted in specific positions so grabbing specific pieces of information is not difficult. However, I tried to read the file like so:

int lines=0;
std::ifstream inputFile("file.bin",std::ios::binary);

while (!inputFile.eof())
{
    inputFile.read(blocks[lines].data, sizeof(block));
    lines++;
}

inputFile.close();
lines--;

and then displaying it like this:

std::cout<<"block 1: "<<blocks[0].data<<std::endl;
// etc ...

I thought that blocks[i].data should just give me the char[32] that belongs to index i, but it instead gives me every “data” element in the struct from that index to the end of the struct. I’m sure that it is my misunderstanding of how that works. My question is: how do I just get the char[32] represented by blocks[i].data?

  • 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-27T09:26:51+00:00Added an answer on May 27, 2026 at 9:26 am

    The problem is your std::cout output statement. When you try to output blocks[0].data, what operator<< gets is not the array of 32 chars, but a pointer to the first char. This is interpreted as pointer to a C string, and therefore it outputs all characters found in memory from there on until it finds a '\0'. Since each array element contains just the corresponding characters from the file, all characters of the file are output (unless there’s a '\0' in the file, then output stops there). Also, you seem to be (un-)lucky that a '\0' follows your data in memory, so the output stops there (instead of continuing to output whatever is in memory afterwards, and possibly giving a segmentation fault when the end of the process’ memory is reached).

    To just output the 32 charactes as characters, use std::cout.write(blocks[0].data,32). Otherwise to output them as ints just loop through them and convert each one to int:

    for (int i = 0; i < 32; ++i)
      std::cout << static_cast<int>(blocks[0].data[i]) << ' ';
    

    Of course you can use all the stream manipulators to get the numbers in the form you want (e.g. std::hex for hexadecimal output, and/or std::setw and std::setfill to get fixed width numbers).

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

Sidebar

Related Questions

I have written a database application using a binary file as storage. it is
I have a binary file of doubles that I need to load using C++.
I have a large (3Gb) binary file of doubles which I access (more or
I have written some code in C to read a binary file containing complex
Although I know the basic concepts of binary representation, I have never really written
I have 2 applications. One in C++ (windows) open a binary file and only
I have code that manipulates binary files using fstream with the binary flag set
I have written a C++ library that saves my data (a collection of custom
I have an application written in Java that uses a jar file(it uses more
I have a binary file which I am reading to a collection of byte

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.