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.
That is, of course, how you want to interpret it. Obviously all data is stored in 0 and 1, because of how electronics work, but generally, everything "higher" then the hardware works in decimals, mostly base 10 or 16.
You've completely lost me here. All digital electronics, with 0.01% exceptions [such as multilevel flash memory] works with the principle of two levels of input, and two levels of output. It is binary, all the way throug. It may be stored in lumps of 4, 8, 16, 32, 64, 128, 1024, or less often 3, 7, 24 or some other number of binary digits (bits) together. Occassionally, groups of 4 can be directly translated to "hex", but it's still 4 bits that just happen to mach base 16.
--
Mats
That pretty much sums it up. When "working" with raw data, all we "see" is numbers, not digits. Perhaps that's clearer. Though it still hides the underlying concept.
Sure, working with binary streams of numbers becomes very unpractical very soon, so representing those binary streams with something more human readable and managable is a good idea. It's much easier to remember/deal with 0x1267 (hex), 011147 (octal) or 4711 (decimal) than it is to remember 0001001001100111. Or to deal with 'a' than 0x61 (hex), 0141 (octal), 97 (decimal), or 01100001. But they are all the same binary form inside the machine - we just represent the sequence of bits in various ways to make it more readable.
And MOV EAX, ECX is much easier to read than 0x89 0xC8 [or 10001001 11001000 as the processor sees it].
--
Mats