carraysfreadchars

C - fread char from file


I have a dummy program which writes some chars in a file :

int main (int argc, char **argv)
{
    char i = '0';
    int j = 0;
    FILE * fp = NULL;

    fp = fopen("num.txt","w+");
    if(!fp){
        printf("fopen failed\n");
        return -1;
    }
    fseek(fp, SEEK_SET, 0);

    while(1){
        if (j >500) {
            fclose(fp);
            return 0;
        }
        usleep(5000);
        fwrite(&i,sizeof(char),1,fp);
        if (j%27 ==0) {
            i ='0';
        }
        i++;
        j++;
    }
    fclose(fp);
    return 0;
}

And another program in which I need to read from that file, here is a fragment:

 if ((num = fopen("num.txt", "r+")) == NULL){
    perror("Error al obrir l'arxiu");   
}

fseek( num, posicio, SEEK_SET );
fread(resposta, sizeof(char), offset, fp);
while(contador <500){
    printf("%c",resposta[contador]);
    contador++;
}
printf(" la resposta contiene %s \n",resposta);A

I want to read "offset" chars from the position "posicio". "resposta" is an array of 500 chars.The while you can see in the second program is because I'm desperate, when I execute the second program,in the console appears a bunch of symbols such as : xMh�� in the last printf.

I tried to read byte to byte in the while, but it keeps getting me these symbols, I don't know what may be doing this, my first suspicion was that I was somehow opening the file in binary mode, but nope!


Solution

  • So this is a re-write of your first program with some nice formatting

    #include <stdio.h>
    #include <unistd.h>
    
    int main(int argc, char **argv)
    {
        char i = '0';
        int j = 0;
        FILE *fp;
    
        fp = fopen("num.txt", "w+");
        /*fseek(fp, SEEK_SET, 0);  - NOT NEEDED*/
    
        if (fp != NULL)
        {
            for (j=0; j<500; j++)
            {
                usleep(5000);
    
                fwrite(&i, sizeof(char), 1, fp);
                fflush(fp);
    
                if (j % 27 == 0)
                    i = '0';
    
                i++;  /* could be put in the for() loop header */
            }
            fclose(fp);
        }
    
        return 0;
    }
    

    And

    void readBlock(int posicio, int offset, char *resposta)
    {
        int i;
        FILE *num = fopen("num.txt", "r+");
    
        if (num != NULL)
        {
            /* reposition to <posicio> bytes from the start of the file */
            fseek(num, posicio, SEEK_SET);
    
            /* read <offset> bytes from that position */
            fread(resposta, sizeof(char), offset, num);
    
            fclose(num);
    
            /* dump out the bytes */
            for (i=0; i<offset; i++)
            {
                printf("%c", resposta[i]);
            }
    
        }
    }
    

    Note that in your code snippet #2, you're opening the file with the handle in the variable ''num'', but then reading from the handle ''fp''. Is that on purpose? It looks like a bug, but is hard to say from here.