Small byte-writer

Programming, for all ages and all languages.
Post Reply
Kon-Tiki

Small byte-writer

Post by Kon-Tiki »

I'm looking for a way to make an executable that, when run, does something like this to a file the user chose:
Change:
00012FC: 00 23 45 11 FD 3A 41 etc
to:
00021FC: 00 23 FF FF FD 3A 41 etc

The programming tool that I have, is DevC++, but only things I can make with it, are 'Hello World'-thingies and Windows-programs where you navigate through lots of dialog boxes (or whatever those things with 'ok' and 'cancel' are called) I want to be able to do this myself. It would be a giant breakthrough in my programming skills ;)

-Kon-Tiki-
Tim

Re:Small byte-writer

Post by Tim »

You should use fopen (to open the file), fseek (to seek to the correct location), fwrite (to write the new data) and fclose (to close the file when you have finished).
Kon-Tiki

Re:Small byte-writer

Post by Kon-Tiki »

Thanks. Now I'm going to look for a small, primitive C-compiler, because that DevC++ doesn't accept those commands ::) It doesn't even compile some files that work or codes from books.

Btw: How're those commands used? An explanation like fopen(filename); is good enough.

-Kon-Tiki-
Tim

Re:Small byte-writer

Post by Tim »

Waitaminute... DevC++ will accept those functions, since they're part of every compiler's C library. fopen etc. are fundamental parts of the C standard; they're supported everywhere, and documented anywhere that C is documented.
sonneveld

Re:Small byte-writer

Post by sonneveld »

Raf's probably getting warnings saying that those functions haven't been defined.

Have you included the stdio.h library header? It contains definitions of those io functions so when it comes to link the code together with the library code, it knows what to look for.

Code: Select all

#include <stdio.h>
- Nick
Kon-Tiki

Re:Small byte-writer

Post by Kon-Tiki »

Woops. I'll try that. Anyway, got an explanation of how those functions work. First looked for it in the help-file of Dev-C, but it got me nowhere.

Btw: how can I do this with fseek?
Change:
0000180: xx xx xx xx xx xx etc
To:
0000180: xx xx xx FF FF xx etc

Back to the programming table...

-Kon-Tiki-
Tim

Re:Small byte-writer

Post by Tim »

I'm not going to say anything, because it will be more helpful for you to get hold of a general C reference and look these things up for yourself. I can guarantee you will find documentation on the standard C functions within 5 minutes of searching the web.
Kon-Tiki

Re:Small byte-writer

Post by Kon-Tiki »

Code: Select all

FILE* pFilePointer = fopen( "savestate.zst", "r+b" ); // open the file
fseek( pFilePointer, 0xAE, SEEK_SET ); // seek to the offset we want to change
unsigned char gold = 0xFF;
fwrite( &gold, 1, 1, pFilePointer ); // write our new gold value to the file
fclose( pFilePointer ); // close the file when we're done
That's my code for now (asked the question on another board too) There's just a few things that I doubt will do what it needs to do.

1) It doesn't make the user choose which file to write to.
2) 0xAE is a set of bytes (AFAIK), not something that looks like this:

Code: Select all

000000AE: 54 0E 2A 44 etc
It still seem to look for something that looks like this:

Code: Select all

xxxxxxxxxx: xx xx xx AE 00 xx xx etc
Is that correct, and if it is, how can I make it look for the large row of numbers and a variable byte in that row (which is on a fixed spot)?

-Kon-Tiki-
Kon-Tiki

Re:Small byte-writer

Post by Kon-Tiki »

Woops, wrong there. Nick explained how it works and now it's clear to me. Thanks :)

Now, I made a little test and after seeing that it didn't compile because stdio.h wasn't included, it works. Now I have a program that changes every test in the textfile to Vest ;D

The program, the textfile and the source is included in attached zip.

Man, this stuff's fun. Can't wait to learn the next step (having the user inputting the filename, storing it as a string and changing the filename in fopen(); to the string)

Edit: can't seem to get the syntaxis for the array in fopen(); right. It's now this: and gives a 'parse error before character constant'-error.

Code: Select all

char Filename[20]
cout << "blabla";
cin.getline (Filename, 20, '/n');
FILE* pFilePointer = fopen ( &Filename '/n' , "r+b" );
That's the part of the code that's wrong somewhere (I think somewhere in the &Filename-section.

-Kon-Tiki-

[attachment deleted by admin]
Tim

Re:Small byte-writer

Post by Tim »

Change '/n' to '\n', and remove the &.

You really ought to buy or download a C book or something. You're not doing yourself a favour learning from a web message board.
Kon-Tiki

Re:Small byte-writer

Post by Kon-Tiki »

I must admit that this guy made me understand alot of what I am trying to do. From what he and Nick has tought me, it won't take long before I can make a tiny program (like a Dos Editor or so) That's the basic that I need. I make lots of syntax mistakes, yep, but I consider this as a language like any other spoken language. You first have to understand how the basics work (which I do now, thanks to Disch) before you can start learning it. I think I'm ready now to start reading a book a friend sent me some time ago (that is, if you can tell me if it's what'll give me the further basic knowledge of C++. Don't need to know what iterators (or what the **** they're called) are, just need to be able to program (which means, being able to use them)

Look at it like this: you can learn a language by studying it into so many details that you get lost in all the exceptions on the exceptions of the exceptions or you can learn it from a practical point of view, learn the basics and learn what you need to know later, when you're needing it.

Thanks anyway.

-Kon-Tiki-
Post Reply