0

I just created a simple program to write to a binary file in C. But the .dat or .bin file when opened through notepad or any editor it is showing the input as it was given without converting it(converting in sense is the special symbols which I have seen on other people's devices) in binary even using "wb" mode....

Please help me in this I'm sharing my code snippet here.

#include<stdio.h>

int main()
{
  FILE* binFile;
  binFile = fopen("OKEY.dat", "wb");

  if (binFile == NULL)
    printf("LOL");

  else
  {
    char username[15];
    char passwd[15];
    int i = 0;
    for (i = 0; i < 2; i++)
    {
      printf("Enter New Username:");
      scanf("%s", &username);
      printf("Enter New password");
      scanf("%s", &passwd);

      fwrite(username, sizeof(username), 1, binFile);
      fwrite(passwd, sizeof(username), 1, binFile);
    }
  }
  return 0;
}
paceHut
  • 1
  • 1
  • 1
  • 3
    Welcome to Stack Overflow. "it is showing the input as it was given without converting it in binary" *What do you think you should see* when you view the file? **Why**? – Karl Knechtel Jan 22 '22 at 13:35
  • Welcome to SO. What do you get and what **exactly** do you expect in your file? You only deal with strings. There is not much difference (except some extra bytes between the strings) in the output to be expected. – Gerhardh Jan 22 '22 at 13:36
  • @Gerhardh oh yes there is. He writes the __whole__ `username` and `passwd` buffers into the file, even if the actual strings are shorter. – Jabberwocky Jan 22 '22 at 13:37
  • @Jabberwocky yes, the length is not the same (therfore I wrote *not much* and not *no*) but there is no magical conversion happening. I updated the comment – Gerhardh Jan 22 '22 at 13:38
  • @Jabberwocky consider the possibility though that the program is being compiled in debug mode and the file is full of unprintable NUL characters that are invisible in Notepad. – Karl Knechtel Jan 22 '22 at 13:38
  • 1
    BTW: If you are (or at least intent to be) dealing with binary files the proper way to check the content is not notepad or any other plain text editor. Instead you must use an editor that is able to display the content as hex dump. Only then you can see what really is stored in the file. Any non-printable values would not be visible in a normal text editor. – Gerhardh Jan 22 '22 at 13:42
  • 1
    At least on Linux, I think it's doing what you're expecting. The name and password are being written as 15-byte fields, with a null terminator after the text. On Linux/Unix, it's helpful to look at the file contents with `od -c OKEY.dat`. If this isn't what you mean, then I think we need more clarity on what you mean when you say it isn't being written binary. – Perette Jan 22 '22 at 13:46
  • what I'm expecting is some other special symbols(which I have seen in one of my friend's device from notepad) instead of the same string which I entered as input. I know that a notepad will not be able to read the exact binary-coded strings but why it is showing the same username and passwd as it was given? @KarlKnechtel – paceHut Jan 24 '22 at 17:58
  • I explained above that I just want to know why the notepad is able to read exactly as the input was given.. Well I'm on windows@Perette – paceHut Jan 24 '22 at 18:07
  • "what I'm expecting is some other special symbols(which I have seen in one of my friend's device from notepad) instead of the same string which I entered as input." *Then you have fundamentally the wrong idea about how this works*. No symbols are "special". When you open a file in Notepad that is meant to be "binary", typically what this means is that the data is *not intended to be interpreted as text*. But *all* data is *bytes*, and you can always *try* to interpret it as *anything*. – Karl Knechtel Jan 24 '22 at 18:43
  • okay! I got your point so my notepad or any other editor is just interpreting the data as it was given and sometimes it might not bcz the data are bytes... Isn't it?? @KarlKnechtel – paceHut Jan 25 '22 at 18:39
  • The data is always bytes. The question is whether the bytes have values that make sense according to the rules that the editor wants to apply. – Karl Knechtel Jan 25 '22 at 19:46

0 Answers0