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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T12:56:09+00:00 2026-06-16T12:56:09+00:00

In an array like int a[5] we can store 5 values from a[0] to

  • 0

In an array like int a[5] we can store 5 values from a[0] to a[4]. not this..?

I have a char mobile[10] variable in my class and I was storing exactly 10 character long string in this variable. But when I am reading it from file, a few characters from the next variable (declared just after this variable in class) are being appended in variable mobile. It took hours to investigate what is wrong.

I tried everything I could by changing the order of variable etc.

At last I’d set the size of mobile to 11 (char mobile[11]) and then store it into the binary file. Then everything goes well.

Here I have created a demo program that can demonstrate my study:

#include <iostream.h>
#include <conio.h>
#include <string.h>
#include <fstream.h>
#include <stdio.h>

class Test
{
    public:
    char mobile[10], address[30];
};

void main()
{
    clrscr();
    Test t;
    // uncoment below to write to file
    /*strcpy(t.mobile, "1234567890");
    strcpy(t.address, "Mumbai");

    fstream f("_test.bin", ios::binary | ios::out | ios::app);
    f.write((char*)&t, sizeof(t));*/

    // uncomment below to read from file
    /*fstream f("_test.bin", ios::binary | ios::in);
    f.read((char*)&t, sizeof(t));
    cout << t.mobile << "\t" << t.address;*/

    f.close();

    getch();
}

Is my assumption correct that I can not store n characters in an array like char[n] when working with files more specifically with binary files..?

Should I always take 1 extra size of required size..??

My compiler is Turbo C++ (may be 3.0). It is very old and discontinued product.

  • 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-16T12:56:10+00:00Added an answer on June 16, 2026 at 12:56 pm

    character pointers in C/C++ must be null terminated. That means you must allot another character with value of ‘\0’ at the end.

    Also note, strcpy function copies all the characters from one string to another, until \0 is encountered, unless its a const string(an example is “hello world”) which is stored as “hello world\0” during compilation.

    Try this code:

    #include <iostream.h>
    #include <conio.h>
    #include <string.h>
    #include <fstream.h>
    #include <stdio.h>
    
    class Test
    {
        public:
        char mobile[11], address[30];
    };
    
    void main()
    {
        clrscr();
        Test t;
        // uncoment below to write to file
        strcpy(t.mobile, "1234567890");
        strcpy(t.address, "Mumbai");
        t.address[10] = '\0';
        fstream f("_test.bin", ios::binary | ios::out | ios::app);
        f.write((char*)&t, sizeof(t))
    
        // uncomment below to read from file
        fstream f("_test.bin", ios::binary | ios::in);
        f.read((char*)&t, sizeof(t));
        cout << t.mobile << "\t" << t.address;
    
        f.close();
    
        getch();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array which looks like this: $arr = ( [0]=>Int(2) [1]=>Array( ....)
Suppose that I have a 2D array (matrix) in Java like this... int[][] MyMat
i have an array like below int[] array = new array[n];// n may be
I want to have a large 2 dimensional array such like int myArray[10000][2]; I
I have an Array like below in PHP array( (int) 0 => array( 'id'
I have two arrays that I create like this: public int GameBoard[][] = new
I have the following php array that gets all values from a form full
I have an array whose indices map back to Enum values. I would like
To assign specific value to 1D array I'm using LINQ like so: int[] nums
I declared a multidimensional array in C, like so: int arr[4][2]; int length =

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.