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

  • Home
  • SEARCH
  • 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 5966557
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:49:00+00:00 2026-05-22T19:49:00+00:00

I have a binary file. There are 2288*2288 longitude float values stored in top

  • 0

I have a binary file. There are 2288*2288 longitude float values stored in top half section, and the same number of latitude float values occupied the bottom half. I used the following code to load them into a float vector. It can run like a charm, but gave me incorrect results. With regard to my binary file, the float vector should be filled with a total of 2288*2288*2=10469888 elements, but only 159005, all their values are the same 200.0000. Would you please explain what’s wrong with my code?

Thank you in advance!

bool LoadData(const char* pszDataFile)
{   
    typedef char_traits<float> traits_type;
    typedef std::codecvt<float, char, mbstate_t> cvt;

    std::basic_ifstream<float, traits_type> input( pszDataFile, std::ios::binary );
    std::locale loc(std::locale(), new cvt());
    input.imbue(loc);

    std::vector<float> fvBuffer;    

    // Copies all data into buffer 
    std::copy(std::istreambuf_iterator<float>(input),          
              std::istreambuf_iterator<float>( ),         
              std::back_inserter(fvBuffer)); 

    long nSzie = fvBuffer.size();  // Wrong vector size (159005)

    return true;
}
  • 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-22T19:49:00+00:00Added an answer on May 22, 2026 at 7:49 pm

    To make std::basic_ifstream works, you have to define your trait_type so that it provides everything expected by input stream, and I’m not sure that it will be possible. That will be far more than just a codecvt<float, char, mbstate_t> (which you seem to think is already present while the standard demand only the wchar_t, char and char, char specializations).

    If you want a binary input iterator, you’ll have to write one yourself to work with basic_ifstream, something like this (run and give the expected result, but not debugged further):

    #include <fstream>
    #include <algorithm>
    #include <vector>
    #include <iterator>
    #include <iostream>
    
    template <typename T>
    class BinaryInputIterator
        : public std::iterator<std::input_iterator_tag, T, std::ptrdiff_t,
                               const T*, const T&>
    {
    public:
        BinaryInputIterator();
        BinaryInputIterator(std::istream&);
        // Compiler generated version OK:
        // BinaryInputIterator(BinaryInputIterator const& other);
        // BinaryInputIterator& operator=(BinaryInputIterator const& other);
        ~BinaryInputIterator();
    
        T const& operator*() const;
        T const* operator->() const;
        BinaryInputIterator& operator++();
        BinaryInputIterator operator++(int);
    
    private:
        std::istream* myStream;
        T myValue;
    
        friend bool operator==
           (BinaryInputIterator const& l, BinaryInputIterator const& r)
        {
            return ((l.myStream == NULL && (r.myStream == NULL || !*r.myStream))
                    || (r.myStream == NULL && (l.myStream == NULL || !*l.myStream)));
        }
        friend bool operator!=
           (BinaryInputIterator const& l, BinaryInputIterator const& r)
        {
            return !(l == r);
        }
    };
    
    template <typename T>
    BinaryInputIterator<T>::BinaryInputIterator()
        : myStream(0)
    {}
    
    template <typename T>
    BinaryInputIterator<T>::BinaryInputIterator(std::istream& is)
        : myStream(&is)
    {
        myStream->read(reinterpret_cast<char*>(&myValue), sizeof myValue);
    }
    
    template <typename T>
    BinaryInputIterator<T>::~BinaryInputIterator()
    {}
    
    template <typename T>
    T const& BinaryInputIterator<T>::operator*() const
    {
        return myValue;
    }
    
    template <typename T>
    T const* BinaryInputIterator<T>::operator->() const
    {
        return &myValue;
    }
    
    template <typename T>
    BinaryInputIterator<T>& BinaryInputIterator<T>::operator++()
    {
        myStream->read(reinterpret_cast<char*>(&myValue), sizeof myValue);
        return *this;
    }
    
    template <typename T>
    BinaryInputIterator<T> BinaryInputIterator<T>::operator++(int)
    {
        BinaryInputIterator result(this);
        ++*this;
        return result;
    }
    
    int main()
    {
     {
        std::ofstream os("foo.dta");
        std::vector<float> vect1;
        vect1.push_back(4.2);
        vect1.push_back(3.14);
        os.write(reinterpret_cast<char*>(&vect1[0]), sizeof(float)*vect1.size());
     }
     {  
        std::ifstream is("foo.dta");
        std::vector<float> vect2;
        std::copy(BinaryInputIterator<float>(is),
                  BinaryInputIterator<float>(),
                  std::back_inserter(vect2));
        std::copy(vect2.begin(), vect2.end(), std::ostream_iterator<float>(std::cout, "\n"));
     }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a binary file - Windows static library (*.lib). Is there a simple
I have a binary file that I have to parse and I'm using Python.
Say I have a binary file (generated with Java) containing a 32 bit int
I have binary data in a file that I can read into a byte
I have to read a binary file in a legacy format with Java. In
I have written a database application using a binary file as storage. it is
If you have binary strings (literally String objects that contain only 1's and 0's),
I have a binary field in my database that is hard to describe in
I have 4 binary bits Bit 3 Bit 2 Bit 1 Bit 0 Normally
I have a class that I need to binary serialize. The class contains one

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.