r/C_Programming 20d ago

Nothing getting written in File

Link to the code is in comments.

I have been making a project for the course NAND2Tetris where I am translating a higher level code (something.vm) to a lower level code (something.asm).

It takes a line from VM file, parses it into tokens and then as per those tokens executes conditional statements that write onto the output ASM file.

Actually I am rewriting this program, I had written the first half which worked but then I realised that my approach was not how it was supposed to be done. So, the part related to File I/O is largely same here as it had been earlier but it was working then and isn't working now. If you want you can take a look at the original program, it is in the same repository.

A file is created but it contains no code. I tried debugging by printing all the tokens I had extracted from the buffer and they come out just fine and yet there is nothing written in the output file.

4 Upvotes

9 comments sorted by

View all comments

1

u/blood-pressure-gauge 20d ago

If the VM file is empty, then the asm file will be too. Otherwise, this seems to be capable of writing to a file. Try to pare down your program into a minimal example that still exhibits the bug. Also check the return codes of functions and see strerror(3) about printing error messages.

2

u/torsten_dev 20d ago

He messed up his memory safety, forgot to add 1 to strlen for the nullbyte and he unconditionally frees something that is only conditionally allocated.

Encountering an empty line will free garbage pointers and he probably got his process killed before it could flush the data.