Can somebody please tell me how to read a text file as binary file?
Thanks in advance.
Printable View
Can somebody please tell me how to read a text file as binary file?
Thanks in advance.
fopen (fileName, "rb");
Thanks for replying.
How do I read?
can I use fgetc() function or fread() function.
Typically you use fread.
I tried to read a file like this:
I got the output as face ASCII chars.Code:#include <stdio.h>
#include <conio.h>
int main(void){
FILE *fp;
char c;
fp=fopen("tw.txt","rb");
while(=fgetc(fp)!=EOF){
printf("%c\n",c);
}
return 0;
}
Actually the input contained some text.
How to overcome this?
>Typically you use fread.
Why? The different I/O functions solve different problems. Just because fread and fwrite have traditionally been associated with binary streams doesn't mean they're always the best choice. The correct answer to the OP's question is "what are you trying to do?".
>char c;
fgetc returns int. If char is unsigned for your implementation, your loop would run forever because EOF is a negative value.
>How to overcome this?
Overcome what? Can you be more specific about what the problem is?
Indent:
What were you expecting?Code:#include <stdio.h>
#include <conio.h>
int main(void){
FILE *fp;
char c;
fp=fopen("tw.txt","rb");
while(=fgetc(fp)!=EOF){
printf("%c\n",c);
}
return 0;
}
Of course, but I did mention typically, didn't I?
Fread is good for most things.
I want the o/p to be in binary format.
And I was experimenting by printing it as character.
Please bear with my ignorance.
>but I did mention typically, didn't I?
You did, but I disagree that it's typical, which is why I asked you why. ;)
>I want the o/p to be in binary format.
You're not going to get 0's and 1's as output, if that's what you were expecting. The only difference between binary mode and text mode is that text mode supports conversions conversions such as '\n' while binary mode gives you the raw characters without any filtering.
Reading "binary" only means that certain characters won't be interpreted as "end of file characters" and that returns are translated to "\n". Nothing more.
The type of your variable that you read into determines the output you get.
Prelude: well, for binary, I have had the need of anything else than fread/fwrite (no, I don't like C++ I/O).
Actually I want the file to be printed as it is stored on the disk.
i.e in 1's and 0's.Is there any way to do it?
It's not stored in 1's and 0's. It's stored in decimal form.
But of course it's possible. By converting to binary. But that in itself is not the easiest thing.
This function can be used for print binary information:
This may be a bit above your current level of experience, as it's not that simple.Code:void PrintBits(uint32_t nNumber)
{
char Binary[sizeof(nNumber) * 8 + 1] = {0};
char Binary2[sizeof(nNumber) * 8 + 1] = {0};
for (int i = sizeof(nNumber) * 8 - 1; i >= 0; i--)
{
if (nNumber & (0x1 << i))
Binary[i] = '1';
else
Binary[i] = '0';
}
for (int i = sizeof(nNumber) * 8 - 1, j = 0; i >= 0; i--, j++) Binary2[j] = Binary[i];
/*for (int i = 0; i < sizeof(nNumber) * 8; i++)
{
//if (i == 1) cout << " ";
//else if (i == 1 + 8) cout << " ";
cout << Binary2[i];
}*/
puts(Binary2);
puts("\n");
}
FILE* f = fopen("myfile", "rb");
char c = fgetc(f);
PrintBits(uint32_t(c));
>Actually I want the file to be printed as it is stored on the disk.
Actually, you want the output to be represented as binary values (a common question). You need to understand that 'A' is just a representation of a value, 0x41 and 01000001 are also just representations of the same value. You're never going to get the file printed as it's stored on the disk unless you want to draw the molecular structure of your hard drive.
Thank you.This cleared up a few doubts i had.
I will try this and post if i had any other questions.