Hello. I've been having the most non sense problem ever in a C program.
This program is a simple compiler that compiles a sequence of 0's and 1's and creates a binary file that contains these sequences.
An example of what it compiles is this:
0x19: 00001010;
It reads the 0x19, and records it. It's the instruction code. Then it starts reading the sequence of 4 blocks of 0's and 1's.
It saves these into arrays, and then uses fwrite to write the sequences to a binary file. The dilema is that the array itself is correct, but when the binary file is created the error occurs. The array has all the correct elements in order. I've tried writing one char at a time with a loop rather than writing the whole array at once with fwrite, but the same happens.
My problem is that the function writes the binary file correctly up to a point, but it writes the last bytes incorrectly. It puts a 0D in front of the numbers it is supposed to write. I have no idea why this is happening and I been trying to solve this for days now.
The corresponding line in the binary file is: 0D 0A
As you can see there's a 0D in front of 0A. That should not be there at all.
I don't know if fwrite is making a confusion and writing an integer rather than a char to the file at that point. All my variables are unsigned chars, and I declare them as chars in fwrite.
Can anyone please shed me a light on what might be happening?
My code is a bit too complex to post here. It's an interpreter to interpret the text and write the bin file.
Thank you a lot if you can help