-1

I really tried not resorting to asking this but after days of searching here it goes.

I need to write a program that opens a file called "input.bin" and prints it in binary beginning to end on screen.

While it does that for each byte it adds 00000101 to each byte and prints it in "result.bin"

    (for example 01001010
                +00000101
                =01001111
and 11111111
   +00000101
   =00000100)
  • Can you show us what have you have tried? – David G Jan 13 '14 at 01:10
  • i really haven't gotten anywhere. This is the code from where I've took the most http://stackoverflow.com/questions/10195343/copy-a-file-in-an-sane-safe-and-efficient-way but somehow I think I should have been using unsigned char instead of char. This bit manipulation is pretty damn hard to find online, as though no one is interested in it nowadays. This is my assignment so I really cant ask the professor for help. – user1398593 Jan 13 '14 at 01:18
  • There's not really any bit manipulation needed here. According to your description, you're reading bytes, adding 5, and writing bytes. The code to do this is probably a lot simpler than you are imagining. – Greg Hewgill Jan 13 '14 at 01:27
  • I agree, still there remains to print it out onscreen. One more thing that confuses me is what if I have 0xfa would 0xfa+0x05=0xff make EOF character? – user1398593 Jan 13 '14 at 01:32
  • No, there is no such thing as an "EOF character". Functions such as `fgetc()` can return the special value `EOF`, but that isn't a valid character (that is, you can't put it in a file and you can't read it from a file). – Greg Hewgill Jan 13 '14 at 01:40
  • thanks for the help. I'll try and figure the rest out on my own then. still could you please show me how to get the bits from char?? – user1398593 Jan 13 '14 at 01:47
  • Do you mean you actually want to print out the binary 1's and 0's that represent the bytes of your input file? If so, perhaps you could edit your question to provide a simple example of some input and expected output. – Greg Hewgill Jan 13 '14 at 02:59

1 Answers1

1

It's too simple for me to explicitly write the code but this should help you:

  • Open "input.bin" for read as binary
  • Open "output.bin" for write
  • Read each byte, printing the binary value on the screen using an auxiliary function
  • Add 5 (no need to do anything special just literally add 5 and make sure to use the type unsigned char) and write the byte's value to output.bin
  • When EOF, remove("input.bin"); and rename("output.bin", "input.bin");

For a good hint on reading a single character at a time and checking for EOF: http://www.cplusplus.com/reference/cstdio/fgetc/

For printing a number in binary the question has been answered many times: Is there a printf converter to print in binary format?

Community
  • 1
  • 1
austere
  • 157
  • 7