C++ 如何读取乱码文件内容?

本人近期在做国创项目,其中需要使用重复码,并且使用经过重复码编码后的数据进行加密。
问题是重复码编码后再读取就乱码了,而且我不知道怎么样可以读取出二进制数据
重复码代码如下:
  1.  
    #include<iostream>
  2.  
    #include<stdlib.h>
  3.  
    #include<string>
  4.  
    #include<fstream>
  5.  
     
  6.  
    using namespace std;
  7.  
     
  8.  
    static unsigned int inbfr,outbfr;
  9.  
    static FILE *outfile,*infile;
  10.  
    static int incnt,outcnt,mask;
  11.  
     
  12.  
    voidinit()
  13.  
    {
  14.  
    outbfr=0;
  15.  
    outcnt=8;
  16.  
    inbfr=0;
  17.  
    incnt=8;
  18.  
    mask=0x80; //10000000
  19.  
    }
  20.  
     
  21.  
    intgetbit()
  22.  
    {
  23.  
    int bitval;
  24.  
    bitval=inbfr&mask; //bitval0000000
  25.  
    incnt--; //7
  26.  
    mask >>= 1; //01000000
  27.  
    bitval >>= incnt;
  28.  
    if (incnt==0)
  29.  
    {
  30.  
    inbfr=fgetc(infile);
  31.  
    incnt=8;
  32.  
    mask=0x80;
  33.  
    }
  34.  
    return bitval; //0000000bitval
  35.  
    }
  36.  
     
  37.  
    voidputbit( int bitval)
  38.  
    {
  39.  
    outbfr = (outbfr<<1)&255; //00000000
  40.  
    outbfr |= bitval; //0000000bitval
  41.  
    outcnt --;
  42.  
    if (outcnt==0)
  43.  
    {
  44.  
    fputc(outbfr,outfile);
  45.  
    outcnt = 8;
  46.  
    }
  47.  
    }
  48.  
     
  49.  
    voidalignbits()
  50.  
    {
  51.  
    if (outcnt!=8)
  52.  
    {
  53.  
    for (int i=0;i<outcnt;i++)
  54.  
    putbit(0);
  55.  
    }
  56.  
    }
  57.  
     
  58.  
     
  59.  
    voiddecode()
  60.  
    {
  61.  
    int n = 3;
  62.  
    int bitsum;
  63.  
    if ((infile = fopen("ciphertext.txt", "rb")) == NULL)
  64.  
    {
  65.  
    printf("cannot open infile!!!n");
  66.  
    exit(0);
  67.  
    }
  68.  
    if ((outfile = fopen("decryption.txt", "wb")) == NULL)
  69.  
    {
  70.  
    printf("cannot open outfile!!!n");
  71.  
    exit(0);
  72.  
    }
  73.  
    init();
  74.  
    inbfr = fgetc(infile);
  75.  
    while (!feof(infile))
  76.  
    {
  77.  
    bitsum = 0;
  78.  
    for (int i = 0; i < n; i++) bitsum += getbit();
  79.  
    if (bitsum >= 2) putbit(1);
  80.  
    else putbit(0);
  81.  
    }
  82.  
    alignbits();
  83.  
    fclose(infile);
  84.  
    fclose(outfile);
  85.  
    }
  86.  
     
  87.  
     
  88.  
    voidcode()
  89.  
    {
  90.  
    int n = 3;
  91.  
    int bitval;
  92.  
    if((infile=fopen("plaintext.txt","rb"))==NULL)
  93.  
    {
  94.  
    int a;
  95.  
    cout << "请输入数值: ";
  96.  
    cin >> a;
  97.  
    ofstream file_writer("plaintext.txt", ios_base::out);
  98.  
    file_writer << a;
  99.  
    file_writer.close();
  100.  
    }
  101.  
     
  102.  
     
  103.  
    if ((infile = fopen("plaintext.txt", "w+")) != NULL)
  104.  
    {
  105.  
    int a;
  106.  
    cout << "请输入数值: ";
  107.  
    cin >> a;
  108.  
    ofstream file_writer("plaintext.txt", ios_base::out);
  109.  
    file_writer << a;
  110.  
    file_writer.close();
  111.  
    }
  112.  
     
  113.  
     
  114.  
    if((outfile=fopen("ciphertext.txt","wb"))==NULL)
  115.  
    {
  116.  
    ofstream infile("ciphertext.txt");
  117.  
    }
  118.  
     
  119.  
     
  120.  
     
  121.  
    init();
  122.  
    inbfr=fgetc(infile);
  123.  
    while(!feof(infile))
  124.  
    {
  125.  
    bitval=getbit();
  126.  
    for (int i=0;i<n;i++)
  127.  
    putbit(bitval);
  128.  
     
  129.  
    }
  130.  
    alignbits();
  131.  
     
  132.  
    ifstream in("ciphertext.txt");
  133.  
    string str;
  134.  
    while (getline(in, str));
  135.  
    cout << "重复码编码后的内容: " << str << endl; //无效读取
  136.  
    in.close();
  137.  
     
  138.  
    fclose(infile);
  139.  
    fclose(outfile);
  140.  
    }
  141.  
     
  142.  
    intmain(){
  143.  
    code();
  144.  
    decode();
  145.  
    }
  146.  
     
乱码截图如下:

 

C++ 如何读取乱码文件内容?

 

 

我试过网上的读取方法,我用的都不适用。
我想读取重复码编码后的二进制数据,最好是用十进制表示的二进制数 ,因为后续还要加密(例如 2 表示为 十进制的10)。求各位大佬帮助!

原文链接: https://www.cnblogs.com/MoRanPiao/p/16772707.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    C++ 如何读取乱码文件内容?

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/5519

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月4日 下午7:38
下一篇 2023年2月4日 下午7:39

相关推荐