0

I am trying to store a simple string in a file opened in wb mode as shown in code below. Now from what i understand, the content of string should be stored as 0s and 1s as it was opened in binary mode but when i opened the file manually in Notepad, I was able to see the exact string stored in the file and not some binary data. Just for curiosity I tried to read this binary file in text mode. Again the string was perfectly shown on output without any random characters. The below code explains my point :

#include<stdio.h>
int main()
{
    char str[]="Testing 123";
    FILE *fp;
    fp = fopen("test.bin","wb");
    fwrite(str,sizeof(str),1,fp);
    fclose(fp);
    return 0;
} 

So i have three doubts out of this:

  1. Why on seeing the file in Notepad, it is not showing some random characters but the exact string ?
  2. Can we read a file in text mode which was written in binary mode and vice versa ?
  3. Not exactly related to above question but can we use functions like fgetc, fputc, fgets, fputs, fprintf, fscanf to work with binary mode and functions like fread, fwrite to work with text mode ?

Edit: Forgot to mention that i am working on Windows platform.

LocalHost
  • 910
  • 3
  • 8
  • 24
  • ASCII characters are also binary zeroes and ones.... – Paul Ogilvie Sep 06 '20 at 13:15
  • Binary mode does not mean that: it means there is no text processing of newlines or tabs etc. If you want to work with text then functions like `fgets()` break the input into lines, but if you use `fread()` you'll have to do that yourself. – Weather Vane Sep 06 '20 at 13:15
  • @Weather Vane So apart from newlines and tabs, the chars will be stored as it is ?? Just like they are stored in text mode ? – LocalHost Sep 06 '20 at 13:19
  • As I said, binary mode does not mean that each byte is either a `0` or a `1`. Of [possible interest](https://stackoverflow.com/questions/25823310/is-there-any-difference-between-text-and-binary-mode-in-file-access). – Weather Vane Sep 06 '20 at 13:22
  • @ Paul Ogilvie Yes they are but after all when storing in binary formats we wont see the actual character, Right ? but the combination of bytes. – LocalHost Sep 06 '20 at 13:23

1 Answers1

1
  • In binary mode, file API does not modify the data but just passes it along directly.
  • In text mode, some systems transform the data. For example Windows changes \n to \r\n in text mode.
  • On Linux there is no difference between binary vs text modes.
  • Notepad will print whatever is in the file so even if you write 100% binary data there is a chance that you'll see some readable characters.
perreal
  • 94,503
  • 21
  • 155
  • 181
  • Regarding the first point you mentioned, in binary mode all the numerical data will be stored as binary data and the text or chars will be stored as it is. Am i right ?? – LocalHost Sep 06 '20 at 15:05
  • 1
    How the data is stored depends on the functions you call and way you store things. This does not depend on the mode of the file. If you do `fwrite(f, &number, sizeof(number))` you will write binary data, if you do `fprintf(f, "%d", number)` you will write text. This is regardless of the mode. – perreal Sep 06 '20 at 15:09