0

the fgetc function in C should read just on caracter but in this code read more then one caracter .

in this code i test the function fseek to move the cursur and get each time the next caracter using the function fgetc . I apply this on file containe the number from 1 to 19 . but when i try to read the first 1 in the number 11 it read 12 i don't why this happen.

#include <stdio.h>
#include <stdlib.h>

#define BLOCK_SIZE 20

int main() {
    int i;
    FILE *file;
    char buffer[BLOCK_SIZE];
    int returnCode;
    int value;

    // Open or create the binary file for writing
    file = fopen("data.bin", "wb");
    if (file == NULL) {
        perror("Error opening file");
        return 1;
    }

    // Fill the buffer with consecutive values from 0 to 19
    for (i = 0; i < BLOCK_SIZE; i++) {
        buffer[i] = i;
    }

    // Write the entire buffer to the file
    fwrite(buffer, BLOCK_SIZE, 1, file);

    // Close the file
    fclose(file);

    // Open the file for reading in binary mode
    file = fopen("data.bin", "rb");
    if (file == NULL) {
        perror("Error opening file");
        return 1;
    }

    // Move the file cursor to the 5th byte from the beginning
    returnCode = fseek(file, 5, SEEK_SET);
    if (returnCode != 0) {
        printf("Changement de position impossible\n");
        fclose(file);
        exit(EXIT_FAILURE);
    }

    // Read the byte at the current cursor position
    value = fgetc(file);
    printf("Le 5eme octet allant du debut est %d\n", value);

    // Read the next byte after the current cursor position
    value = fgetc(file);
    printf("L'octet suivant est %d\n", value);

    // Move the file cursor 5 bytes forward from the current position
    returnCode = fseek(file, 5, SEEK_CUR);
    value = fgetc(file);
    printf("Octet 5 positions plus loin de la position actuelle est %d\n", value);

    // Move the file cursor 5 bytes backward from the end of the file
    returnCode = fseek(file, -5, SEEK_END);
    value = fgetc(file);
    printf("5eme octet à partir de la fin est %d\n", value);

    // Close the file
    fclose(file);

    return 0;
}

this the outuput : this the output of this code

10
  • 2
    You are printing the numerical values of the characters. Use %c if you want the characters themselves. Commented Apr 19, 2024 at 20:52
  • You're not putting characters in the file, you're putting binary bytes. Then when you read the file you're printing the same bytes. Commented Apr 19, 2024 at 20:55
  • 1
    @EugeneSh. There aren't any printable characters in the file, only control characters. Commented Apr 19, 2024 at 20:59
  • 1
    See Why should I not upload images of code/text/errors when asking a question? It would be helpful if you explained what you expected to get because what I see in the image looks like what I'd expect. Commented Apr 19, 2024 at 21:16
  • 1
    Who are you asking "can you explain more"? Identify a person by typing @name. Commented Apr 19, 2024 at 21:22

1 Answer 1

1

You said: I apply this on file containe the number from 1 to 19. It's wrong. File contains numbers from 0 to 19.

Here is hexdump of data.bin:

[gmt@arch ~]$ hexdump -X data.bin
0000000  00  01  02  03  04  05  06  07  08  09  0a  0b  0c  0d  0e  0f
0000010  10  11  12  13                                                
0000014

After opening file for reading, initially it is on position 0 (I'll use 0 indexed numbering). So you seek it 5 bytes, then the position is on 5 (6th byte) and reading character at this position must result 5 and its ok.

    returnCode = fseek(file, 5, SEEK_SET);
    if (returnCode != 0) {
        printf("Changement de position impossible\n");
        fclose(file);
        exit(EXIT_FAILURE);
    }

    // Read the byte at the current cursor position
    value = fgetc(file);
    printf("Le 5eme octet allant du debut est %d\n", value);

Result: Le 5eme octet allant du debut est 5.

Next you get another character (in index=6 or 7th character) and it must be 6:

    // Read the next byte after the current cursor position
    value = fgetc(file);
    printf("L'octet suivant est %d\n", value);

Result: L'octet suivant est 6

Now file is in index=7 (8th position), So after you seek it 5 positions, it must be in index=12 (or 13th position) and it must read 12:

    // Move the file cursor 5 bytes forward from the current position
    returnCode = fseek(file, 5, SEEK_CUR);
    value = fgetc(file);
    printf("Octet 5 positions plus loin de la position actuelle est %d\n", value);

Result: Octet 5 positions plus loin de la position actuelle est 12.

Finally you seek file backward from its end 5 positions. It is 20 bytes file and its last character index is 19. So seek -5 means index=14 (or 15th character). So reading it must result 0x0f or 15 and it's ok too.

    // Move the file cursor 5 bytes backward from the end of the file
    returnCode = fseek(file, -5, SEEK_END);
    value = fgetc(file);
    printf("5eme octet à partir de la fin est %d\n", value);

Result: 5eme octet à partir de la fin est 15

Sign up to request clarification or add additional context in comments.

3 Comments

thank you for your time . but what i don't understand is that when i it read 6 then seek 5 postion it should be read just 1 because it shift 7 then shift 8 then 9 , 1,0 and this is a five postion and so it should be return 1 because fgetc return just one caracter.
Your file is not a text file, it is binary file. And numbers like 12 takes just one byte, not two bytes. See hexdump output in my post. See your code: file = fopen("data.bin", "wb");, you opened it as binary file.
Okay thank you a lot :) . i should do some searches on binary file .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.