• Our software update is now concluded. You will need to reset your password to log in. In order to do this, you will have to click "Log in" in the top right corner and then "Forgot your password?".
  • Welcome to PokéCommunity! Register now and join one of the best fan communities on the 'net to talk Pokémon and more! We are not affiliated with The Pokémon Company or Nintendo.

C/C++ tutorial for the masses

interdpth

I've seen things, man.
275
Posts
19
Years
  • Seen Jun 8, 2021
Open up VC++ or any other C/C++ compiler make a new commandline project
open a blank c/cpp file paste this into it

#include <stdio.h>//Basic headers I always use
#include <windows.h>




int main(){
FILE *fp = NULL;// The file we'll be workin with
char FilePath[512]={0};//The zero is to make sure it's all zeroed out otherwise there could be weird values this is an array BTW
unsigned char Var1=0;//Our 8 bit variable set it to zero read above
unsigned short Var2=0;//unsigned is for values 0 to other stuff no negatives
unsigned long Var3=0;
printf("This program will grab a\n 8 bit variable, a 16 bit variable, a 32 bit variable\n"); // \n tells it to go to a new line
while(1){ // Infinite loop
printf("Please enter a file path\n");
scanf("%s",FilePath);//It knows to expect a string for input
fp = fopen(FilePath,"r+b");//Open for reading and writing to binary
if(fp){//Check to make sure the file opening succeeded
fseek(fp,5,SEEK_SET);//Hop to offset 5
Var1=fgetc(fp);
fread(&Var2,2,1,fp);
fread(&Var3,4,1,fp);
printf("Var1: 0x%X\n",Var1);//Print in hexadecimal output
printf("Var2: 0x%X\n",Var2);
printf("Var3: 0x%X\n",Var3);
printf("Yay we made it thus far,\nnow on each line please input a new value in hexadecimal output\n");
scanf("%8x",&Var1);//read from the commandline
scanf("%16x",&Var2);
scanf("%32x",&Var3);
printf("You entered for Var1: 0x%X\n", Var1);
printf("You entered for Var2: 0x%X\n", Var2);
printf("You entered for Var3: 0x%X\n", Var3);
printf("Now inserting into ROM\n");
fseek(fp,5,SEEK_SET);
fputc(Var1, fp);
fwrite(&Var2,2,1,fp);
fwrite(&Var3,4,1,fp);
fclose(fp);
}else{
printf("File opening failed");
}

printf("Ok, we're done please type 0 to exit anything else will restart the program\n");
scanf("%d",Var1);
if(Var1==0) return 0;
}


return 0;

}


compile it and run it


i'll do a basic windows application later
enjoy =p
 
Last edited by a moderator:

ckk

23
Posts
15
Years
  • Seen Aug 28, 2009
This tutorial.......
i dont understand a thing......
why don't you start with simple commands and then add on instead of tyin up one huge one and explaining i all
that way our brains can absorb little by little and it'll be easier to learn wats bein told
 

X-aveon

IS BACK!!!!!!!!!!!!!!!!!!!
233
Posts
16
Years
  • Seen Jun 25, 2012
uh............ first of all, what is this about,
and second of all, it sounded like ur a scientist.
in english, please?
 

Cy-Chan

GSC Hacker
152
Posts
19
Years
It's a Dos program made in C++ to create some sort of hacking utility.

As far as I recognise, it's designed to grab an 8-bit, 16-bit and 32-bit variable. I assume it's simply showcasing the different code required, so that people can use it as reference for creating hacking tools in C++.

It's quite enlightening for me; I always assumed C++ was big, scary and made no freaking sense, but this is quite simple. Great tutorial.
 

D-Trogh

Dead
439
Posts
18
Years
I assume it's simply showcasing the different code required, so that people can use it as reference for creating hacking tools in C++.

It's quite enlightening for me; I always assumed C++ was big, scary and made no freaking sense, but this is quite simple. Great tutorial.
That's exactly what this is all about ;)
 
18
Posts
16
Years
  • Seen Aug 11, 2008
Thats the C way not C++. It works in C++, but why use C++ with C's bad ways? In C++ we use the fstream and seekp and seekg for getting the pointers. And in C++ we definately don't use printf. Using the C++ way is much simpiler and uses a great synax. This program isn't cross platform either. We mine aswell use the crappy vb or something of the like lol. If your trying to learn to make programs or whatever, stop. Buy a book or read tutorials, theres always more than one way to do things. Its nice you posted this, but not many people will get it and the people that do will have a harder time writting a new program than if they knew what they were doing and wrote it from scratch.
 

DawnRyder

I cannot believe I'm back -.-
79
Posts
14
Years
  • Seen Nov 12, 2011
Well Done

This is an excellent tutorial. Thank you for sharing this, especially the offsets part (never got that part before..)

I'll probably try to write an app in c++ that adds the shiny hack.
 

DawnRyder

I cannot believe I'm back -.-
79
Posts
14
Years
  • Seen Nov 12, 2011
It's nice, but it'd be a lot more useful if you explained command by command.

Why would he have to explain it command by command?

Any c/c++ at the beginner/intermediate level would understand this in a snap.

If you are not into c/c++ you should not be reading this thread.
 

.Seth

.explorer.
1,644
Posts
15
Years
Hmm, could of sworn a tutorial was designed to teach you a subject. "C/C++ tutorial for the masses". Sounds like a C/C++ tutorial to me. He didn't mention any background information required, so technically, it's not a tutorial. More of a reference.
 

DawnRyder

I cannot believe I'm back -.-
79
Posts
14
Years
  • Seen Nov 12, 2011
Hmm, could of sworn a tutorial was designed to teach you a subject. "C/C++ tutorial for the masses". Sounds like a C/C++ tutorial to me. He didn't mention any background information required, so technically, it's not a tutorial. More of a reference.

It's entitled, 'C/C++ tutorial for the masses', the masses that program in c/c++.

Like I said, If you don't program in c/c++ DONT post in this thread.
 

colcolstyles

Yours truly
1,588
Posts
15
Years
char FilePath[512]={0};//The zero is to make sure it's all zeroed out otherwise there could be weird values this is an array BTW
I'm new to C++ so forgive me if I make absolutely no sense but I don't believe you can perform aggregate assignment to an array in C++. Therefore, this assigns only the first item in the array to 0. Wouldn't something like:
Code:
for (count = 0; count < 512; count++)
    FilePath[count] = 0;
be more appropriate?

I did some tests and I'm pretty sure that your code won't initialize the array properly but I'm a little hesitant to question your code given your reputation as a programmer of ROM Hacking tools :)
 

SharpPoint

Suicune used Surf!
477
Posts
14
Years
It's entitled, 'C/C++ tutorial for the masses', the masses that program in c/c++.

Like I said, If you don't program in c/c++ DONT post in this thread.

That still doesn't explain why the word 'tutorial' is used in the title, regardless whether or not I program in C/C++.
 

IIMarckus

J946@5488AA97464
402
Posts
16
Years
  • Seen Feb 21, 2024
I'm new to C++ so forgive me if I make absolutely no sense but I don't believe you can perform aggregate assignment to an array in C++. Therefore, this assigns only the first item in the array to 0. Wouldn't something like:
Code:
for (count = 0; count < 512; count++)
    FilePath[count] = 0;
be more appropriate?

I did some tests and I'm pretty sure that your code won't initialize the array properly but I'm a little hesitant to question your code given your reputation as a programmer of ROM Hacking tools :)
Since the filename is never read before being changed through scanf, why zero the array at all?

Personally, I would do it this way:
Code:
#include <stdio.h>

int main(int argc,char* argv[]){
	FILE *fp = NULL;

	if(argc != 2)
	{
		printf("Usage: progname filename\n");
		exit(1);
	}

	/* assuming argv[1] is a file path */

	unsigned int eightBit = 0, sixteenBit = 0, thirtytwoBit = 0;

	fp = fopen(argv[1],"rb");

	if(fp == NULL)
	{
		perror("Error opening file");
		exit(1);
	}

	fseek(fp,5,SEEK_SET);

	/* read and write bytes ... */

	fclose(fp);

	return 0;
}
And encapsulate it in a batch file if people really want looping.
 
Last edited:

score_under

Inactive; Former ROM hack tool author, ❤️
526
Posts
18
Years
Thats the C way not C++. It works in C++, but why use C++ with C's bad ways?
C has no "bad ways". In fact, the way C++ works pretty much blinds the programmer to what's going on behind the scenes. The fact that you can use = on a string in C++ shows a lot about how abstracted it is.

In C++ we use the fstream and seekp and seekg for getting the pointers. And in C++ we definately don't use printf. Using the C++ way is much simpiler and uses a great synax. This program isn't cross platform either.
It is if you remove #include <windows.h> , because that's completely unnecessary in this program.

We mine aswell use the crappy vb or something of the like lol. If your trying to learn to make programs or whatever, stop. Buy a book or read tutorials, theres always more than one way to do things. Its nice you posted this, but not many people will get it and the people that do will have a harder time writting a new program than if they knew what they were doing and wrote it from scratch.
I never read a book on C++, but I am willing to bet that I can code faster, smaller, more efficent, and generally less buggy code than Interdpth, given his example in this forum.
Sorry for being so blunt, but seeing people who think they know C or C++ completely abuse it and refuse to think that they might just be a little fundamentally wrong really gets to me.
If you know C, you will know that...
- "long" is not a type
- Assigning a char* to another char* will not copy the string, it will copy the reference.
- fopen() is not descriptive enough on Windows - you can get all sorts of errors on Windows, it's best to use the API with GetLastError().
- if() executes for anything but 0, and a faster way of checking if(x==NULL) or if(x==0) is simply if(!x).
- There are easy ways to make a program compile on Windows, Linux, Mac, for Unicode or ASCII, using windows or command-line interface, all with the same source.
- You can do anything from C if you know how. You can even access .NET if you know how.
 

DawnRyder

I cannot believe I'm back -.-
79
Posts
14
Years
  • Seen Nov 12, 2011
That still doesn't explain why the word 'tutorial' is used in the title, regardless whether or not I program in C/C++.

This 'TUTORIAL' is showing 'C/C++ PROGRAMMERS' how to access certain offsets an obtain bit variables.
Like 'OFFSETS'.

Jeeze...

And BTW, listen to score_under he's good.
 
Last edited:

colcolstyles

Yours truly
1,588
Posts
15
Years
Since the filename is never read before being changed through scanf, why zero the array at all?
Good programming practice?
Yes, technically it isn't necessary to initialize the array but if interdpth ever writes a program where he does need to zero out an array, he might run into some problems. Here, it's not needed but hey, it can't hurt, can it?

Anyways, I would just like to say that, for what it's worth, this tutorial helped me out quite a bit. I've been able to familiarize myself with some of the basic C commands (which, unfortunately, were not covered (as you can probably imagine) in my C++ class) and with that knowledge, I was able to write a simple command line utility that edits ROM headers. Thanks to interdpth for the tutorial and IIMarckus for clarifying a few things for me.
 

OM3GA Umbr30n

Was Mazot2
123
Posts
14
Years
  • Age 26
  • UK
  • Seen Mar 3, 2010
Attention

If you done understand you dont need to know and seeing me here you are thinking,
1. Why revive an old topic?
to give help
2.What is a noob doing here?
:P well im not so noobish now as i have decided to learn C++ i actually understand alot of this and as i said if you dont understand you dont need to,
this i a tutorial for the masses of people that use C++ on the basic code for a Commandline/Console Application for Hacking,
I cant wait for the basic GUI version.
Also out of curiosity what compiler do you use?
 
Back
Top