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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:39:54+00:00 2026-06-15T08:39:54+00:00

#include <iostream> #include <fstream> #include <iterator> #include <vector> int main() { std::ifstream file(data.bin, std::ios::binary

  • 0
#include <iostream>
#include <fstream>
#include <iterator>
#include <vector>

int main()
{
    std::ifstream file("data.bin", std::ios::binary );

    if( file.fail() )
    {
        std::cout << "File does not exist or could not open file";

        return 0;
    }

    std::vector<short> buffer;

    std::copy( 
            std::istreambuf_iterator<char>( file ),
            std::istreambuf_iterator<char>(),
            std::back_inserter( buffer )
            );

   return 0;
}

This only gives me ranges of char values (-128, 128).

I thought using istreambuf_iterator<short> would give me what I want but it throws an “invalid conversion” error.

What can I do to read binary values that are in the short range?

  • 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-15T08:39:55+00:00Added an answer on June 15, 2026 at 8:39 am

    Here’s my recommended Spirit approach:

    #include <fstream>
    #include <boost/spirit/include/qi.hpp>
    
    namespace qi = boost::spirit::qi;
    
    int main()
    {
        std::ifstream file("data.bin", std::ios::binary);
        if(!file)
            std::cout << "File does not exist or could not open file";
        boost::spirit::istream_iterator f(file), l;
    
        std::vector<int16_t> buffer;
        bool ok = qi::parse(f, l, *qi::word, buffer);
    }
    

    Of course, there is qi::dword, qi::qword, big-endian/little-endian variatations etc.:

    • http://www.boost.org/doc/libs/1_52_0/libs/spirit/doc/html/spirit/qi/reference/binary/binary_native.html
    • http://www.boost.org/doc/libs/1_52_0/libs/spirit/doc/html/spirit/qi/reference/binary/binary_little.html
    • http://www.boost.org/doc/libs/1_52_0/libs/spirit/doc/html/spirit/qi/reference/binary/binary_big.html

    You can look at wchar_t

    #include <iostream>
    #include <fstream>
    #include <iterator>
    #include <vector>
    int main()
    {
        std::wifstream file("data.bin", std::ios::binary );
        if( file.fail() )
        {
            std::cout << "File does not exist or could not open file";
            return 0;
        }
        std::vector<wchar_t> buffer;
        std::copy( 
                std::istreambuf_iterator<wchar_t>( file ),
                std::istreambuf_iterator<wchar_t>(),
                std::back_inserter( buffer )
                );
    }
    

    I’m not too sure whether the actual size of wchar_t is implementation defined.

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

Sidebar

Related Questions

#include<iostream> #include<fstream> #include<cstdlib> #include<iomanip> using namespace std; int main() { ifstream in_stream; // reads
#include<fstream> #include<iostream> #include<string> #include<algorithm> using namespace std; int main(){ ifstream infile; ofstream outfile; infile.open(oldfile.txt);
#include <iostream> #include <fstream> using namespace std; int main () { ofstream myfile; myfile.open
I created this program: #include <iostream> #include <fstream> using namespace std; int main ()
Here's my code so far: #include<iostream> #include<string> #include<fstream> using namespace std; int main() {
#include <iostream> #include <string> #include <fstream> using namespace std; int main() { char x[20];
#include <iostream> #include <string> #include <fstream> using namespace std ; string strWord( int index
#include<iostream> #include<time.h> #include<list> #include<stdlib.h> #include<fstream> using namespace std; typedef struct diskBtNode { int parent;
#include <cmath> #include <fstream> #include <iomanip> #include <iostream> int main() { int count =
Here is my code: #include <iostream> #include <stdlib.h> #include <fstream> using namespace std; int

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.