不良文件描述符错误通过处理文件

发布于 2025-02-03 16:15:19 字数 6067 浏览 3 评论 0原文

C 通过在文件上处理错误的文件描述符错误。 这是大学的任务。我需要压缩文件TXT,然后取消压缩。压缩方法正常工作 它通过以下想法来压缩,即最后的二进制数字(MSB)在任何ASCII CHAR中始终为零。 有人知道为什么会发生吗? 当我在Uncompress方法中执行FGETC(input_file_8to7)时,问题就会发生 主要问题是,我从ucompresd方法中的2 fgetC(input_file_8to7)获得-1

#include <stdio.h> 
#include <string.h> 
#include <malloc.h> 
#include <stdlib.h> 
#include <stdint.h> 

uncompress的输入是压缩的输出,输入是txt文件,它可以在接下来的2行中结合下来: 十六进制转储是从内存或计算机文件中对计算机数据的十六进制视图。

***compres method***

    void compress8to7(FILE *input,FILE* output8to7)
    {
        // 0x00 87 00 87 00 87 00 87 ll
        uint64_t magic=0x87008700870087ll;
        uint64_t inputsize=0;
    
        fwrite(&magic,sizeof (magic),1,output8to7);
        fwrite(&inputsize,sizeof(inputsize),1,output8to7);
        unsigned char  st;
        unsigned char st2;
        char check;
        char check2;
        char shift=7;
        char shift_st=0;
        unsigned char inbfile;

//将包含问的新二进制线的resullt

        int breakflag=0;
        int breakflag2=0;
        int cnt=-1;

//此参数将有助于知道何时我们不需要移动1 输入文件的指针

        while(1) {
    
            cnt++;
            if (ftell(input)>1 && cnt%7!=0) //
            {
                fseek(input,-1 ,SEEK_CUR) ;
            }
    
            check = fgetc(input);
            st=check;
            if(check2==EOF){breakflag2=1;}
    
            check2 = fgetc(input); 

> //if the length  is odd number check2 will get the eof

            st2=check2;
            if(check==EOF){breakflag=1;}
    
            st2=st2<<shift; 

> //move the digit to the right position

位操纵

            st=st>>shift_st;
            shift_st++;
            if(shift_st==7)
            {shift_st=0;}
    
            shift=shift-1;
            if(shift==0)
                shift=7;
    
            if(breakflag2!=1)
                {inbfile=st2|st;
                }else{ inbfile=st; }
            fwrite(&inbfile, sizeof(inbfile),1,output8to7);

写入文件

            if(feof(input))
            {
                inputsize= ftell(input);
           
                fseek(output8to7,8,SEEK_SET);
                fwrite(&inputsize,sizeof (inputsize),1,output8to7);
               // if(breakflag==1)
                break;}
    
        }
    
    }
*** uncompress method***

问题是在此方法中

void uncompress8to7 (FILE *input_file_8to7  ,FILE *output_file_txt){
     char st;
     char st2;
    char check;
    char check2;
    char shift2 = 7;
    char shift_st = 0;
    char shift_helper=7;
    char shift_helper2=6;
    char sthelper;
    char sthelper2;
    char inbfile; // will contain the resullt of the asked new  binary lines comprees
    int breakflag = 0;
    int breakflag2 = 0;
    int cnt = -1;//this parameter will help to know when we dont need to move 1 back in the pointer of input file
    rewind(input_file_8to7);

    printf("%d",ftell(input_file_8to7));
    fseek(input_file_8to7,16,SEEK_SET);
    printf("\n%d",ftell(input_file_8to7));
    int a=0;
    while(1) {
    cnt++;

    if(cnt>1) //
        {fseek(input_file_8to7,-1 ,SEEK_CUR);}
    printf("\n%d",ftell(input_file_8to7));

从那个fgetc我得到不良文件描述符erorr

    check =  fgetc(input_file_8to7);


    if(ferror(input_file_8to7)){
        perror("eror by perror");
        printf("file erorr");}

  //  printf("\n%d",ftell(input_file_8to7));

    st = check;
    check2 = fgetc(input_file_8to7);
    st2 = check2;

    if(cnt<2)
        fseek(input_file_8to7,0,SEEK_SET);

    if(check2==EOF){
    breakflag2 = 1;
    }

    sthelper2=st2;
    sthelper2=sthelper2>>shift_helper2;

    st2=st2<<shift2;
    st2=st2>>shift2;

    sthelper=st;
    sthelper=sthelper>>shift_helper;
    sthelper=shift_helper<<shift_helper-1;
    st=st<<shift_st;// to make all zero after the msb
    st=st>>shift_st;// to make all zero after the msb


    shift_helper2--;
    if(shift_helper==-1)
        {shift_helper2=6;}

    shift_helper--;
    if(shift_helper==-1){
        shift_helper=7;
    }

    shift_st++;
    if(shift_st==7)
        {shift_st=0;}

    shift2=shift2-1;
    if(shift2==0)
        shift2=7;

    if(breakflag2==1)
        {break;}
    if(cnt%7==0){
        inbfile=st;
    }else{
        inbfile=sthelper|st2;
    }

写入文件

    fwrite(&inbfile,sizeof(inbfile),1,output_file_txt);

当我们到达文件末尾

时打破循环
    if(feof(input_file_8to7))
         { break;}

    }
}


***main***

int main(int argc, char **argv) {
    char* input=NULL;
    char* output8to7=NULL;
    input=argv[1];
    output8to7=argv[2];

打开文件

    FILE* inputfile = fopen(input, "r");
    if(inputfile==NULL)
    {
        printf("couldnt open input file ");
        exit(-1);
    }

    FILE* file8to7=fopen(output8to7, "wb");
    if(file8to7==NULL)
    {
        printf("couldnt open output file");
        printf(output8to7);
        exit(-1);
    }

压缩

    compress8to7(inputfile,file8to7);

    FILE* file8to7input=fopen("exampleout.bin", "ab");

    FILE* output_file=fopen("UNoutput_file2.txt", "wb");
    if(output_file==NULL)
    {printf("couldnt open output file");
        exit(-1);
    }

    uncompress8to7(file8to7input,output_file);
   fclose(output_file);
    fclose(file8to7input);
    fclose(inputfile);
    fclose(file8to7);
    return 0;
}

C.
bad file descriptor error by working on files.
This is an assignment for college. I need to compress file txt and then to uncompress it. The compress method working fine
and it compresses by the idea that the last binary digit (Msb) is zero always in any ASCII char.
Does anybody know why it happens?
The problem happens when I do fgetc(input_file_8to7) in the uncompress method
The mainly problem is that i get -1 from the 2 fgetc(input_file_8to7) in uncompresd method

#include <stdio.h> 
#include <string.h> 
#include <malloc.h> 
#include <stdlib.h> 
#include <stdint.h> 

the input for the uncompress is the output of the compress , the input is txt file that conatain the next 2 lines:
A hex dump is a hexadecimal view of computer data, from memory or from a computer file.

***compres method***

    void compress8to7(FILE *input,FILE* output8to7)
    {
        // 0x00 87 00 87 00 87 00 87 ll
        uint64_t magic=0x87008700870087ll;
        uint64_t inputsize=0;
    
        fwrite(&magic,sizeof (magic),1,output8to7);
        fwrite(&inputsize,sizeof(inputsize),1,output8to7);
        unsigned char  st;
        unsigned char st2;
        char check;
        char check2;
        char shift=7;
        char shift_st=0;
        unsigned char inbfile;

// will contain the resullt of the asked new binary lines comprees

        int breakflag=0;
        int breakflag2=0;
        int cnt=-1;

//this parameter will help to know when we dont need to move 1 back in
the pointer of input file

        while(1) {
    
            cnt++;
            if (ftell(input)>1 && cnt%7!=0) //
            {
                fseek(input,-1 ,SEEK_CUR) ;
            }
    
            check = fgetc(input);
            st=check;
            if(check2==EOF){breakflag2=1;}
    
            check2 = fgetc(input); 

> //if the length  is odd number check2 will get the eof

            st2=check2;
            if(check==EOF){breakflag=1;}
    
            st2=st2<<shift; 

> //move the digit to the right position

bit manipulation

            st=st>>shift_st;
            shift_st++;
            if(shift_st==7)
            {shift_st=0;}
    
            shift=shift-1;
            if(shift==0)
                shift=7;
    
            if(breakflag2!=1)
                {inbfile=st2|st;
                }else{ inbfile=st; }
            fwrite(&inbfile, sizeof(inbfile),1,output8to7);

write to the file

            if(feof(input))
            {
                inputsize= ftell(input);
           
                fseek(output8to7,8,SEEK_SET);
                fwrite(&inputsize,sizeof (inputsize),1,output8to7);
               // if(breakflag==1)
                break;}
    
        }
    
    }
*** uncompress method***

the problem is in this method

void uncompress8to7 (FILE *input_file_8to7  ,FILE *output_file_txt){
     char st;
     char st2;
    char check;
    char check2;
    char shift2 = 7;
    char shift_st = 0;
    char shift_helper=7;
    char shift_helper2=6;
    char sthelper;
    char sthelper2;
    char inbfile; // will contain the resullt of the asked new  binary lines comprees
    int breakflag = 0;
    int breakflag2 = 0;
    int cnt = -1;//this parameter will help to know when we dont need to move 1 back in the pointer of input file
    rewind(input_file_8to7);

    printf("%d",ftell(input_file_8to7));
    fseek(input_file_8to7,16,SEEK_SET);
    printf("\n%d",ftell(input_file_8to7));
    int a=0;
    while(1) {
    cnt++;

    if(cnt>1) //
        {fseek(input_file_8to7,-1 ,SEEK_CUR);}
    printf("\n%d",ftell(input_file_8to7));

from that fgetc i get the bad file descriptor erorr

    check =  fgetc(input_file_8to7);


    if(ferror(input_file_8to7)){
        perror("eror by perror");
        printf("file erorr");}

  //  printf("\n%d",ftell(input_file_8to7));

    st = check;
    check2 = fgetc(input_file_8to7);
    st2 = check2;

    if(cnt<2)
        fseek(input_file_8to7,0,SEEK_SET);

    if(check2==EOF){
    breakflag2 = 1;
    }

    sthelper2=st2;
    sthelper2=sthelper2>>shift_helper2;

    st2=st2<<shift2;
    st2=st2>>shift2;

    sthelper=st;
    sthelper=sthelper>>shift_helper;
    sthelper=shift_helper<<shift_helper-1;
    st=st<<shift_st;// to make all zero after the msb
    st=st>>shift_st;// to make all zero after the msb


    shift_helper2--;
    if(shift_helper==-1)
        {shift_helper2=6;}

    shift_helper--;
    if(shift_helper==-1){
        shift_helper=7;
    }

    shift_st++;
    if(shift_st==7)
        {shift_st=0;}

    shift2=shift2-1;
    if(shift2==0)
        shift2=7;

    if(breakflag2==1)
        {break;}
    if(cnt%7==0){
        inbfile=st;
    }else{
        inbfile=sthelper|st2;
    }

writing to the file

    fwrite(&inbfile,sizeof(inbfile),1,output_file_txt);

break the loop when we got to the end of file

    if(feof(input_file_8to7))
         { break;}

    }
}


***main***

int main(int argc, char **argv) {
    char* input=NULL;
    char* output8to7=NULL;
    input=argv[1];
    output8to7=argv[2];

open files

    FILE* inputfile = fopen(input, "r");
    if(inputfile==NULL)
    {
        printf("couldnt open input file ");
        exit(-1);
    }

    FILE* file8to7=fopen(output8to7, "wb");
    if(file8to7==NULL)
    {
        printf("couldnt open output file");
        printf(output8to7);
        exit(-1);
    }

compress

    compress8to7(inputfile,file8to7);

    FILE* file8to7input=fopen("exampleout.bin", "ab");

    FILE* output_file=fopen("UNoutput_file2.txt", "wb");
    if(output_file==NULL)
    {printf("couldnt open output file");
        exit(-1);
    }

    uncompress8to7(file8to7input,output_file);
   fclose(output_file);
    fclose(file8to7input);
    fclose(inputfile);
    fclose(file8to7);
    return 0;
}

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

横笛休吹塞上声 2025-02-10 16:15:19

这是打开文件的代码:

    FILE* file8to7input=fopen("exampleout.bin", "ab");

在附加模式下,将其打开为输出文件。您正在尝试在uncompress8to7()函数中从中读取它。您需要在读取模式下像输入文件一样打开它。将该行更改为:

    FILE* file8to7input=fopen("exampleout.bin", "rb");

This is the code to open the file:

    FILE* file8to7input=fopen("exampleout.bin", "ab");

This opens it as an output file in append mode. You're trying to read from it in the uncompress8to7() function. You need to open it as in input file in read mode. Change that line to:

    FILE* file8to7input=fopen("exampleout.bin", "rb");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文