/**************************************************************************** Дан бинарный файл, содержащий символы (тип char). Удалить из файла все цифры, если они следуют после знаков препинания. *****************************************************************************/ #include <stdio.h> #include <string.h> int main(int argc, char* argv[]) { int n = 0; char a[1000], ch[1000]; FILE* file = fopen("1.bin", "wb"); printf("\nput string into the file '1.bin'\n"); gets(ch); fwrite(&ch, sizeof(1000), 1, file); fclose(file); FILE* read = fopen("1.bin", "rb"); for (int i = 0; i < strlen(ch); i++) { fread(&a[i], sizeof(char), 1, read); if (isdigit(a[i])) { printf("true"); } printf("%c", a[i]); } fclose(read); printf("\n"); return 0; } 

Write a string to a binary file, you need to output it character by character. The problem is that this code displays only the first 4 characters and I cannot understand why.

    1 answer 1

    sizeof(strlen(ch)) is the size of the return value of strlen(ch) , i.e. the size of the integer value - in your case exactly 4 bytes ...

    As well as sizeof(1000) is 4. You write only 4 bytes to the file ...

    How do you read them (or rather, try) - 4 bytes each - this is another, but also a very painful question :)

    • Yes, I got it. The string in the file must be written through fputs, and not through fwrite. fputs (ch, file); <- for my code - Vadim Moroz
    • You can whatever you like. Just to write and read worked in concert. - Harry
    • 2
      " int size"? In general, strlen returns size_t , not int . Accordingly, sizeof(strlen(ch)) is size size_t . On “usual” platforms - 4 or 8. - AnT
    • one
      @AnT This, of course, fundamentally changes things. - Harry
    • 2
      @Harry; Yes, and how! I would not make this remark if it were not extremely important. - AnT