質問

バイナリファイルから符号なしバイトを読み取りたい。 そこで、次のコードを書きました。

#include <iostream>
#include <fstream>
#include <vector>
#include <istream>

std::string filename("file");
size_t bytesAvailable = 128;
size_t toRead = 128;

std::basic_ifstream<unsigned char> inf(filename.c_str(), std::ios_base::in | std::ios_base::binary) ;
if (inF.good())
{
    std::vector<unsigned char> mDataBuffer;
    mDataBuffer.resize(bytesAvailable) ;
    inF.read(&mDataBuffer[0], toRead) ;
    size_t counted = inF.gcount() ;
}

この結果、カウントされる変数で示されるように、常に0バイトが読み取られます。

これを機能させるにはロケールを設定する必要があると言っているウェブ上の参照があるようです。これを正確に行う方法は私には明らかではありません。

「unsigned char」ではなく「char」データ型を使用して同じコードが機能します

unsigned charを使用した上記のコードはWindows上で動作するようですが、colinux Fedora 2.6.22.18での実行に失敗します。

Linuxで機能させるにはどうすればよいですか?

役に立ちましたか?

解決

C ++は、2つのバージョンの文字特性に明示的な特殊化を提供するためにのみ実装を必要とします。

std::char_traits<char>
std::char_traits<wchar_t>

ストリームと文字列は、これらの特性を使用して、EOF値、文字の範囲の比較、文字のintへの拡大など、さまざまなことを把握します。

次のようなストリームをインスタンス化する場合

std::basic_ifstream<unsigned char>

ストリームが使用できる対応する文字特性の特殊化があり、この特殊化が有用なことを行うことを確認する必要があります。さらに、ストリームはファセットを使用して、数値の実際のフォーマットと読み取りを行います。同様に、それらの特殊化も手動で提供する必要があります。標準では、プライマリテンプレートの完全な定義を実装に要求することさえありません。したがって、コンパイルエラーが発生する可能性もあります。

  

エラー:特殊化std :: char_traitsをインスタンス化できませんでした。

代わりにifstreamを使用し(これはbasic_ifstream<char>です)、次にvector<char>に進みます。ベクター内のデータを解釈するとき、後でそれらをunsigned charに変換できます。

他のヒント

specializtionが必要なため、basic_ifstreamを使用しないでください。

静的バッファの使用:

linux ~ $ cat test_read.cpp
#include <fstream>
#include <iostream>
#include <vector>
#include <string>


using namespace std;

int main( void )
{
        string filename("file");
        size_t bytesAvailable = 128;

        ifstream inf( filename.c_str() );
        if( inf )
        {
                unsigned char mDataBuffer[ bytesAvailable ];
                inf.read( (char*)( &mDataBuffer[0] ), bytesAvailable ) ;
                size_t counted = inf.gcount();
                cout << counted << endl;
        }

        return 0;
}
linux ~ $ g++ test_read.cpp
linux ~ $ echo "123456" > file
linux ~ $ ./a.out
7

ベクターの使用:

linux ~ $ cat test_read.cpp

#include <fstream>
#include <iostream>
#include <vector>
#include <string>


using namespace std;

int main( void )
{
        string filename("file");
        size_t bytesAvailable = 128;
        size_t toRead = 128;

        ifstream inf( filename.c_str() );
        if( inf )
        {

                vector<unsigned char> mDataBuffer;
                mDataBuffer.resize( bytesAvailable ) ;

                inf.read( (char*)( &mDataBuffer[0]), toRead ) ;
                size_t counted = inf.gcount();
                cout << counted << " size=" << mDataBuffer.size() << endl;
                mDataBuffer.resize( counted ) ;
                cout << counted << " size=" << mDataBuffer.size() << endl;

        }

        return 0;
}
linux ~ $ g++ test_read.cpp -Wall -o test_read
linux ~ $ ./test_read
7 size=128
7 size=7

最初の呼び出しでサイズ変更の代わりに予約を使用する:

linux ~ $ cat test_read.cpp

#include <fstream>
#include <iostream>
#include <vector>
#include <string>


using namespace std;

int main( void )
{
        string filename("file");
        size_t bytesAvailable = 128;
        size_t toRead = 128;

        ifstream inf( filename.c_str() );
        if( inf )
        {

                vector<unsigned char> mDataBuffer;
                mDataBuffer.reserve( bytesAvailable ) ;

                inf.read( (char*)( &mDataBuffer[0]), toRead ) ;
                size_t counted = inf.gcount();
                cout << counted << " size=" << mDataBuffer.size() << endl;
                mDataBuffer.resize( counted ) ;
                cout << counted << " size=" << mDataBuffer.size() << endl;

        }

        return 0;
}
linux ~ $ g++ test_read.cpp -Wall -o test_read
linux ~ $ ./test_read
7 size=0
7 size=7

ご覧のとおり、.resize(counted)を呼び出さないと、ベクターのサイズが間違っています。それを覚えておいてください。 キャストを使用することは一般的です。 cppReference

を参照してください。

はるかに簡単な方法:

#include <fstream>
#include <vector>

using namespace std;


int main()
{
    vector<unsigned char> bytes;
    ifstream file1("main1.cpp", ios_base::in | ios_base::binary);
    unsigned char ch = file1.get();
    while (file1.good())
    {
        bytes.push_back(ch);
        ch = file1.get();
    }
    size_t size = bytes.size();
    return 0;
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top