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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T16:03:52+00:00 2026-05-12T16:03:52+00:00

What is an efficient, proper way of reading in a data file with mixed

  • 0

What is an efficient, proper way of reading in a data file with mixed characters? For example, I have a data file that contains a mixture of data loaded from other files, 32-bit integers, characters and strings. Currently, I am using an fstream object, but it gets stopped once it hits an int32 or the end of a string. if i add random data onto the end of the string in the data file, it seems to follow through with the rest of the file. This leads me to believe that the null-termination added onto strings is messing it up. Here’s an example of loading in the file:

void main()
{
    fstream fin("C://mark.dat", ios::in|ios::binary|ios::ate);
    char *mymemory = 0;
    int size;
    size = 0;
    if (fin.is_open())
    {
        size = static_cast<int>(fin.tellg());
        mymemory = new char[static_cast<int>(size+1)];
        memset(mymemory, 0, static_cast<int>(size + 1));

        fin.seekg(0, ios::beg);
        fin.read(mymemory, size);
        fin.close();
        printf(mymemory);
    std::string hithere;
    hithere = cin.get();
    }
}

Why might this code stop after reading in an integer or a string? How might one get around this? Is this the wrong approach when dealing with these types of files? Should I be using fstream at all?

  • 1 1 Answer
  • 1 View
  • 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-12T16:03:52+00:00Added an answer on May 12, 2026 at 4:03 pm

    Have you ever considered that the file reading is working perfectly and it is printf(mymemory) that is stopping at the first null?

    Have a look with the debugger and see if I am right.

    Also, if you want to print someone else’s buffer, use puts(mymemory) or printf(“%s”, mymemory). Don’t accept someone else’s input for the format string, it could crash your program.

    Try

    for (int i = 0; i < size ; ++i)
    {
      // 0 - pad with 0s
      // 2 - to two zeros max
      // X - a Hex value with capital A-F (0A, 1B, etc)
      printf("%02X ", (int)mymemory[i]);
      if (i % 32 == 0)
        printf("\n"); //New line every 32 bytes
    }
    

    as a way to dump your data file back out as hex.

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

Sidebar

Related Questions

What is the most efficient way to drop a table in SAS? I have
What is the best way to obtain a simple, efficient immutable queue data type
I am looking for the most proper and efficient way to bind Javascript events
I am interested in finding a more efficient way of selecting the proper select
I have a table that contains URL strings, i.e. /A/B/C /C/E /C/B/A/R Each string
What is the most efficient way for checking for equality of two m *
What is the most efficient way to set/re-order the TabIndex properties of many form
What is an efficient way of determining whether a BigDecimal is an integer value
Is there are more efficient way than the following for selecting the third parent?
What's an efficient way for someone who knows PHP and Ruby on Rails to

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.