c++ - 将字符串写入文件不等于从中读取的字符串

标签 c++ file binary binaryfiles

第一阶段

示例 1:我有 string text = "01100001" 然后我想写入文件 "a"
示例 2:我有 string text = "0110000101100010" 所以我想写入文件 "ab"

注意:我解决了阶段 1 并且写入结果为真。

第 2 阶段

例如1:

I want read the file and put it to temp.
So temp = "a" and i convert it to "01100001"

例如2:

I want read the file and put it to temp.
So temp = "ab" and i convert it to "0110000101100010"

问题

在我的代码中我有以下输入

    string text ="00000110101011100010001011111110011011110101100101110101101111010111111110101011"
"00111011000011100011100000100010111110111110111001100001110001110000101001111010"
"00000101";

我做了“第 1 阶段”,我在十六进制编辑器中打开文件,文字是真实的。
但是在执行“第 2 阶段”之后 temp != text。为什么?

我的代码

#include <iostream>
#include <sstream>
#include <vector>
#include <fstream>
#include <string>
#include <stdlib.h>
using namespace std;

class bitChar{
public:
    unsigned char* c;
    int shift_count;
    string BITS;

    bitChar()
    {
        shift_count = 0;
        c = (unsigned char*)calloc(1, sizeof(char));
    }

    string readByBits(ifstream& inf)
    {
        string s ="";
        while (inf)
        {
            string strInput;
            getline(inf, strInput );

            for (int i =0 ; i < strInput.size() ; i++)
            {
                s += getBits(strInput[i]);
            }
        }
        return s;
    }

    void setBITS(string X)
    {
        BITS = X;
    }

    int insertBits(ofstream& outf)
    {
        int total = 0 ;
        while(BITS.length())
        {
            if(BITS[0] == '1')
                *c |= 1;
            *c <<= 1;
            ++shift_count;
            ++total;
            BITS.erase(0, 1);

            if(shift_count == 7 )
            {
                if(BITS.size()>0)
                {
                    if(BITS[0] == '1')
                        *c |= 1;
                    ++total;
                    BITS.erase(0, 1);
                }

                writeBits(outf);
                shift_count = 0;
                free(c);
                c = (unsigned char*)calloc(1, sizeof(char));
            }
        }

        if(shift_count > 0)
        {
            *c <<= (7 - shift_count);
            writeBits(outf);
            free(c);
            c = (unsigned char*)calloc(1, sizeof(char));
        }
        outf.close();
        return total;
    }

    string getBits(unsigned char X)
    {
        stringstream itoa;
        for(unsigned s = 7; s > 0 ; s--)
        {
            itoa << ((X >> s) & 1);
        }

        itoa << (X&1) ;
        return itoa.str();
    }

    void writeBits(ofstream& outf)
    {
        outf << *c;
    }

    ~bitChar()
    {
        if(c)
            free(c);
    }
};


int main()
{
    ofstream outf("ssSample.dat",ios::binary);

    string text ="00000110101011100010001011111110011011110101100101110101101111010111111110101011"
"00111011000011100011100000100010111110111110111001100001110001110000101001111010"
"00000101";
    cout<< text<<endl;

    //write to file
    bitChar bchar;
    bchar.setBITS(text);
    bchar.insertBits(outf);

    outf.close();

    ifstream inf("ssSample.dat" ,ios::binary);
    //READ FROM FILE
    string temp=bchar.readByBits(inf);

    cout << endl;
    cout << temp << endl;

    return 0;
}

最佳答案

您有一个 LF 换行字符。这是被省略的字符。

0000 1010

这可能无关紧要,但 Windows 需要 CRLF 换行。此代码在 Windows 和 Unix 中的行为可能不同。

一次读取一个字节。

string readByBits(ifstream& inf)
{
    string s ="";
    char buffer[1];
    while (inf.read (buffer, 1))
    {
       // string strInput;
        //getline(inf, strInput );

        //for (int i =0 ; i < strInput.size() ; i++)
        //{
            s += getBits(*buffer);
        //}
    }
    return s;
}

程序输出:

000001101010111000100010111111100110111101011001011101011011110101111111101010110011101100001110001110000010001011111011111011100110000111000111000010100111101000000101

000001101010111000100010111111100110111101011001011101011011110101111111101010110011101100001110001110000010001011111011111011100110000111000111000010100111101000000101

关于c++ - 将字符串写入文件不等于从中读取的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21000141/

相关文章:

c++ - 在 C++ 代码中使用 "umlauts"

c++ - Qt - Visual Studio 2013 插件不会编译项目

c# - 如何在 C# 中将对象写入二进制文件?

sql - 按给定序列将二进制转换为十进制

c++ - 如何使用 OpenMP 并行更新总和

c++ - <filesystem> 在 VS2013 与 VS2012 中的不同行为

python - 在 tar 文件中获取文件字节偏移量(和长度)的方法

iOS - 某些文件夹中的迭代

php - 并发读写文件

matlab - 两个矩阵的按元素二进制值串联