#include <stdio.h>
struct my_struct {
char text[100];
} e;
int main() {
FILE *file;
file = fopen("filename", "ab+");
if (file == NULL) {
file = fopen("filename", "wb+");
}
printf("Input text: ");
fflush(stdin);
gets(e.text);
fwrite(&e, sizeof(e), 1, file);
fclose(file);
return 0;
}
What I'm trying to do here is create a binary file and write the file by text input from the user. The code works fine! The only problem is that the file contains spaces, which I believe is due to the array size of struct my_structure
that fwrite
passes while writing the file. I cannot find a good way to remove spaces or replace fwrite
. Thank you! for answering this question.
Program Output:
Input text: holiday
File Output:
686f 6c69 6461 7900 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000