C++ C++;fstream-can';无法使用二进制格式读取文件

C++ C++;fstream-can';无法使用二进制格式读取文件,c++,binary,fstream,C++,Binary,Fstream,我现在尝试将一些整数值写入文件,然后使用fstream读取它。我是这样做的: #include <stdlib.h> #include <iostream> #include <fstream> #include <string> typedef unsigned char BYTE; // 1byte typedef unsigned short WORD; // 2bytes typedef unsigned lon

我现在尝试将一些整数值写入文件,然后使用fstream读取它。我是这样做的:

#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <string>           

typedef unsigned char  BYTE; // 1byte
typedef unsigned short  WORD; // 2bytes
typedef unsigned long  DWORD; //4bytes

using namespace std;

template <typename Word>
ostream& write_word( ostream& outs, Word value )
{
    for (unsigned size = sizeof( Word ); size; --size, value >>= 8)
        outs.put( static_cast <char> (value & 0xFF) );
    return outs;
}

template <typename Word>
istream& read_word( istream& ins, Word& value )
{
    for (unsigned size = 0, value = 0; size < sizeof( Word ); size++)
            value |= ins.get() << (8 * size);   
    return ins;
}   


int main()
{            
    system("CLS");
    int num = 1;                 
    string *str;

    cout<<"How much strings do you want to write: ";
    cin>>num;

    if(num <= 0)
    {                       
        cout<<"\nInvalid value!"<<endl;
        return 1;                
    }

    str = new string[num];

    ofstream out("2.txt",ios::binary|ios::out);

    for(int i = 0; i< num; i++){
        cout<<"Insert string";
        cout<<i;      
        cout<<": ";
        cin>>str[i];

        write_word(out, (DWORD)str[i].length());
        out<<str[i];

    }                               

    out.close();
    cout<<"Values saved to 2.txt."<<endl;

    for(int i = 0; i< num; i++){
        cout<<"string"<<i<<" = "<<str[i]<<endl; 
    }

    system("PAUSE");

    cout<<"Loading values from 2.txt now."<<endl;

    ifstream in("2.txt",ios::binary|ios::in);

    if(!in.is_open()){ cout<<"ERROR"; return 1; }

    for(int i = 0; i< num; i++){
        DWORD len = 0x0;
        read_word(in, len);

        cout<<"string"<<i<<" length is "<<len<<endl;

        char *tmpStr = new char[len];
        in.read(tmpStr, len);
        std::string str2(tmpStr, len);

        cout<<"string"<<i<<" = "<<str2<<endl;
    }

    in.close();    
    system("PAUSE");        

    return 0;
}
#包括
#包括
#包括
#包括
typedef无符号字符字节;//1字节
typedef无符号短字;//20字节
typedef无符号长双字//4字节
使用名称空间std;
模板
ostream和write_单词(ostream和out,单词值)
{
for(无符号大小=sizeof(Word);size;--size,值>>=8)
输出(静态_转换(值和0xFF));
退出;
}
模板
istream和read_word(istream和ins、word和value)
{
for(无符号大小=0,值=0;大小for(unsigned size=0,value=0;size
在循环范围内声明一个新的

通过参考
,您正在更改循环中声明的
无符号值
,而不是参数。在循环之前将值设置为零。这也更易于阅读和理解

最终代码:

template <typename Word>
istream& read_word( istream& ins, Word& value )
{
    value = 0;
    for (unsigned size = 0; size < sizeof( Word ); size++)
            value |= ins.get() << (8 * size);   
    return ins;
} 
模板
istream和read_word(istream和ins、word和value)
{
数值=0;
for(无符号大小=0;大小value |=ins.get()Clang给了我一个很好的警告,您的一个参数未使用。您是否已经通过调试程序检查了代码?