Reading in 4 bytes at a time

前端 未结 5 1384
暗喜
暗喜 2021-01-05 01:54

I have a big file full of integers that I\'m loading in. I\'ve just started using C++, and I\'m trying out the filestream stuff. From everything I\'ve read, it appears I c

相关标签:
5条回答
  • 2021-01-05 02:01

    You could try this:

    const int HRSIZE = 129951336/sizeof(int);  //The size of the table
    int bhr[HRSIZE];   //The table
    
    int main(int argc, char *argv[])
    {
        ifstream fstr;
    
        /* load the handranks.dat file */
        std::cout << "Loading table.dat...\n";
        fstr.open("table.dat");
        for (int i=0; i<HRSIZE; ++i)
        {
            fstr.read((char *)(bhr+i), sizeof(int));
        }
        fstr.close();
    
        // for correctness
        return 0;
    }
    
    0 讨论(0)
  • 2021-01-05 02:10

    To read a single integer, pass in the address of the integer to the read function and ensure you only read sizeof int bytes.

    int myint;
    
    //...
    
    fstr.read(reinterpret_cast<char*>(&myint), sizeof(int));
    

    You may also need to open the file in binary mode

    fstr.open("table.dat", std::ios::binary);
    
    0 讨论(0)
  • 2021-01-05 02:13

    To read by 4 bytes from ifstream you could overload operator>> as follows (it is actually a partial specialization of the basic_istream class template so istream_iterator could use operator>> from it. Class basic_ifstream is used here to inherit all input file stream functionality from it):

    #include <fstream>
    
    typedef unsigned int uint32_t;    
    struct uint32_helper_t {};
    
    namespace std {
    template<class traits>
    class basic_istream<uint32_helper_t, traits> : public basic_ifstream<uint32_t> {
    public:
        explicit basic_istream<uint32_helper_t, traits>(const char* filename, 
            ios_base::openmode mode ) : basic_ifstream<uint32_t>( filename, mode ) {}
    
        basic_istream<uint32_helper_t, traits>& operator>>(uint32_t& data) {
            read(&data, 1);
            return *this;
        }
    };
    } // namespace std {}
    

    Then you could use it in the following way:

    std::basic_istream<uint32_helper_t> my_file( FILENAME, std::ios::in|std::ios::binary );
    // read one int at a time
    uint32_t value;
    my_file >> value;
    
    // read all data in file
    std::vector<uint32_t> data;
    data.assign( std::istream_iterator<uint32_t, uint32_helper_t>(my_file),
      std::istream_iterator<uint32_t, uint32_helper_t>() );
    
    0 讨论(0)
  • 2021-01-05 02:16

    Is this what you mean? By the way, your code (and this) assumes native endianness of the data.

    const int HRSIZE = 129951336;  //The size of the table
    int dhr[HRSIZE/sizeof(int)];   //The table
    
    int main()
    {
        ifstream fstr;
    
        /* load the handranks.dat file */
        std::cout << "Loading table.dat...\n";
        fstr.open("table.dat");
        fstr.read((char*)dhr, HRSIZE);
        fstr.close();
    }
    
    0 讨论(0)
  • 2021-01-05 02:24

    your can do:

    int i;
    fstr.read((int*)&i, sizeof(int));
    
    0 讨论(0)
提交回复
热议问题