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:
- Why on seeing the file in Notepad, it is not showing some random characters but the exact string ?
- Can we read a file in text mode which was written in binary mode and vice versa ?
- Not exactly related to above question but can we use functions like
fgetc,fputc,fgets,fputs,fprintf,fscanfto work with binary mode and functions likefread,fwriteto work with text mode ?
Edit: Forgot to mention that i am working on Windows platform.
fgets()break the input into lines, but if you usefread()you'll have to do that yourself.0or a1. Of possible interest.