Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

StackOverflow Point

StackOverflow Point Navigation

  • Web Stories
  • Badges
  • Tags
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Web Stories
  • Badges
  • Tags
Home/ Questions/Q 2983
Alex Hales
  • 0
Alex HalesTeacher
Asked: June 1, 20222022-06-01T22:20:45+00:00 2022-06-01T22:20:45+00:00

Bad file descriptor error by working on files

  • 0

[ad_1]

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

[ad_2]

  • 0 0 Answers
  • 4 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report
Leave an answer

Leave an answer
Cancel reply

Browse

Sidebar

Ask A Question

Related Questions

  • xcode - Can you build dynamic libraries for iOS and ...

    • 0 Answers
  • bash - How to check if a process id (PID) ...

    • 9 Answers
  • database - Oracle: Changing VARCHAR2 column to CLOB

    • 11 Answers
  • What's the difference between HEAD, working tree and index, in ...

    • 10 Answers
  • Amazon EC2 Free tier - how many instances can I ...

    • 0 Answers

Stats

  • Questions : 43k

Subscribe

Login

Forgot Password?

Footer

Follow

© 2022 Stackoverflow Point. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.