Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

getchar for binary files

Status
Not open for further replies.

ushtabalakh

Programmer
Jun 4, 2007
132
Hi there,
I've written the following code to print contents of a file,

I use it in command line like this.

toHex < sample.txt

it works for text files but not for binary files, I have to get the binary file as input stream; any ideas on how this could be possible? I'm looking for a getchar like function that would get binary characters too.


while((c=getchar()) != EOF && lines <= 1000) {
// Display 16 bytes per line
if (limit % 16 == 0)
{
if (limit!=0) //Printing a crlf after each 16 bytes except the first line
printf("\n");

//Printing a 4 digit line number in hex format
printf("%.4X: ",limit);
}
//Print the given character
printf("%.2X ", (int)c);
limit++;
}
 
stdin is opened as a text file. I don't know how you would force it to be a binary file. Easier if you pass the filename as a parameter and open it as a binary file.
 
Perhaps if you clarified "doesn't work" as well.

For example, did you declare your 'c' variable as a char (bad) or as an int (good).

Or perhaps you're getting a whole bunch of fffff printed when you try to print things with the high bit set?
In which case you should be casting your 'c' variable to unsigned.


--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
 
thanks for your answers

When c was defined as character it wasn't showing anything for binary files, now that I have changed it to unsigned it shows characters till a bye that is equal to 1A, and then stops going further.

I can't accept parameters and open the binary files it must be written this way.

Here's my new code:


unsigned c;
int limit = 0;
int lines=1000;

while((c=getchar()) != EOF && limit <= lines -1) {
// Display 16 bytes per line
if (limit % 16 == 0)
{
if (limit!=0) //Printing a crlf after each 16 bytes except the first line
printf("\n");

//Printing a 4 digit line number in hex format
printf("%.4X: ",limit);
}
//Print the given character
printf("%.2X ", (int)c);
limit++;
}

return 0;
}

 
I think you're out of luck then, since it seems your stdin is opened in "text mode". Historically, the "1A" byte value was used to mark the end of a text file, so I'm guessing that as soon as that appears, then EOF is signalled.

Which OS/Compiler are you using? There may be some non-standard API call to flip stdin into binary mode before you start reading from it.

BTW, c should be declared int, then cast to unsigned for printing.

Please use [code][/code] tags when posting code.


--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
 
Hi there
currently I'm using microsoft visual c++
but the final program will run under gcc and linux

thanks for the code tag I was really looking for that.
 
I suggest you try your program in the final environment then.
It should work just fine there.


--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top