PDA

View Full Version : [Tutorial] C/C++ tutorial for the masses


interdpth
August 3rd, 2008, 12:56 PM
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

ckk
August 5th, 2008, 07:01 AM
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
August 5th, 2008, 07:20 AM
uh............ first of all, what is this about,
and second of all, it sounded like ur a scientist.
in english, please?

Cy-Chan
August 9th, 2008, 12:31 AM
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
August 9th, 2008, 01:46 AM
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 ;)

GBChacker
August 9th, 2008, 09:10 AM
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
August 15th, 2009, 03:43 PM
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.

SharpPoint
August 15th, 2009, 07:12 PM
I'm sorry but, wheres the tutorial?

.Seth
August 15th, 2009, 07:56 PM
It's nice, but it'd be a lot more useful if you explained command by command.

DawnRyder
August 16th, 2009, 07:46 AM
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
August 16th, 2009, 04:09 PM
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.

liuyanghejerry
August 16th, 2009, 07:25 PM
If you're using C++,I prefer you use cout/cin to replace printf().Because that's what C++ suggests.

DawnRyder
August 17th, 2009, 05:12 PM
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
August 17th, 2009, 07:12 PM
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:

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
August 17th, 2009, 08:35 PM
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
August 17th, 2009, 10:06 PM
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:

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:#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.

score_under
August 18th, 2009, 04:43 AM
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
August 18th, 2009, 01:33 PM
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.

colcolstyles
August 18th, 2009, 06:38 PM
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
January 11th, 2010, 06:24 PM
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?

Umbreonic Aura
June 8th, 2010, 04:07 PM
Is the basic windows application ever going to happen?