Search code examples
cfilegetchar

Using getchar() to read from file


I have an assignment and basically i want to read all the bytes from an audio file using getchar() like this: while(ch = getchar()) != EOF) At some point I have to read 4 consecutive bytes that stand for size of file and I can't understand the following: If the file my program is reading is for example 150 bytes in size, that is enough to be stored in 1 of the 4 bytes, which means that 3 of the bytes will be 0 and the last one will be 150 in that case. I understand that I need to read all 4 bytes, through 4 repetitions of the while in the above section of cod, in order to get all the information I need, but what exactly is getchar() going to return to my variable, as it returns the ASCII code for the character it just read? Also what happens for larger numbers, that can't be stored in a single byte?


Solution

  • Cant comment since i dont have enough reputation, i am deeply perplexed with your question for I do not understand what do you mean or what are you trying to achieve The function getChar() should be used for returning mostly a single byte at a time, in fact only upon reading your question did i check the manual to learn it reads more than one although from my experience and the tests i performed now it seems it is used for reading multi byte characters heres the simple code i used to check for it

        char * c;
        printf("Enter character: ");
        c = getchar();
        printf("%s",c);
    

    The character i used and this will probably unformat is the stack overflow glyph i use in my polybar, 溜, here it shows as an asian character.

    Not only that but fgets will return EOF when arriving at the end of the file(or when an error occurs) as stated in the linux manual https://linux.die.net/man/3/getchar

    Also upon further reading it depends on how the file stores data, if its big endian the first byte read will be 0,0,0,150 else if its little endian it will be 150,0,0,0 but thats assuming it is reading 1 character at the time and not 4 at once as you described it

    As for the "solution" of your question why not use fread() reading the 4 bytes at once or a derivative when it does it job properly?

    EDIT As asked by the comment the following "concatenates" the values bit-wise i used scanf because i was too lazy to manually check for every ASCII key, this assuming the file is big endian, ie 0,0,0,150 else invert the order in which the << is done and it should "just werk™"

    #include <stdio.h>
    #include <stdlib.h>
    unsigned char c[4];
    unsigned int dosomething(){
        unsigned int result=0;
        result= (unsigned int)c[0]<< 24 | (unsigned int)c[1]<< 16 | (unsigned int)c[2]<< 8 | (unsigned int)c[3];
        return result;
    }
    int main(int argc, char const *argv[]){
        
        for (size_t i = 0; i < 4; i++)
        {
            printf("Enter character: ");
            scanf ("%u", &c[i]);
            printf("%u\n", c[i]);
            //printf("%s",c);
        
        }
        printf("%u",dosomething());
        
      
        return 0;
    }
    

    Now for the fread it is used like the following fread(pointertodatatoread, sizeofdata, sizeofarray, filepointer); for indepth look here is the manual: https://www.tutorialspoint.com/c_standard_library/c_function_fread.htm this should be asked in a different thread as i feel im asking another question