Register | Login
Views: 19364387
Main | Memberlist | Active users | ACS | Commons | Calendar | Online users
Ranks | FAQ | Color Chart | Photo album | IRC Chat
11-02-05 12:59 PM
0 user currently in Programming. | 3 guests
Acmlm's Board - I2 Archive - Programming - Getting data from an offset in C++ | |
Add to favorites | "RSS" Feed | Next newer thread | Next older thread
User Post
Icy Guy

Red Goomba
Level: 11

Posts: 3/36
EXP: 5112
For next: 873

Since: 04-21-04
From: The Matrix...er, CA, U.S.A.

Since last post: 247 days
Last activity: 246 days
Posted on 04-21-04 09:05 AM Link | Quote
I've seriously been considering learning C++ (I've heard so many good things about it, and I also like the way it handles INIs), but before I devote a good bit of my free time to that, I thought I'd better ask one question that's been really nagging at me: how do you get data from an offset using said programming language? Is it as easy as VB's "Get #filenumber, offset, variable", or is it something more complicated (I'm hoping it's not)?

If anyone chooses to answer this, please keep in mind that I'm C++-illiterate, and I'm not quite up to speed on what farting around with iostream.h or whatever can let me do. Heck, I found out about gdi32/BitBlt in VB only yesterday.
neotransotaku

Baby Mario
戻れたら、
誰も気が付く
Level: 87

Posts: 472/4016
EXP: 6220548
For next: 172226

Since: 03-15-04
From: Outside of Time/Space

Since last post: 11 hours
Last activity: 1 hour
Posted on 04-21-04 10:18 AM Link | Quote
can you give an example of what do you mean by "offset". the meaning of offset right now depends on the context of what you mean by it.
Euclid

Cheep-cheep
Level: 23

Posts: 30/193
EXP: 65528
For next: 2195

Since: 03-15-04
From: Australia

Since last post: 24 days
Last activity: 7 days
Posted on 04-21-04 05:42 PM Link | Quote
some simple code to read bytes from file:

FILE* in = fopen(filename, "rb");
char somebyte;
fseek(in, 0x10000, SEEK_SET);
fscanf("%c", &somebyte);

That should get you the byte at offset 0x10000... I'm pretty sure.
Kegan Marius

Micro-Goomba
Level: 8

Posts: 15/18
EXP: 1866
For next: 321

Since: 03-15-04
From: Chico, California, United States of America

Since last post: 553 days
Last activity: 339 days
Posted on 04-21-04 07:37 PM Link | Quote
If you're using C++, it's better to use filestreams:

#include
std::ifstream in(filename, std::ios::binary);
in.seekg(some_offset, std::ios_base::beg);

std:: is the namespace (ALWAYS use this when dealing with the standard library), std::ios::binary is an optional parameter that means the fille will be opened as binary rather than text (omit this to open as text), and seekg sets the read file pointer. some_offset is some offset (can be negative) and std::ios_base::beg says to start from the beginning of the file. Use std::ios_base::end to seek from the end.
Dish

Spiny
Level: 38

Posts: 41/596
EXP: 355646
For next: 14801

Since: 03-15-04
From: Disch

Since last post: 18 days
Last activity: 18 days
Posted on 04-21-04 08:17 PM Link | Quote
meh. filestreams are one of the goofy and unnecessary things C++ added to C. The C method not only works well.. but is easier to understand and uses less oddball syntax (compare "std::ios_bas::beg" to "SEEK_SET" and "std::ios::binary" to "b").

I say go with whatever you find to be easier. Neither is "better" than the other. personally... I prefer FILE* to filestreams... much like I prefer printf to cout. There were just some things C++ didn't need to mess with
Icy Guy

Red Goomba
Level: 11

Posts: 4/36
EXP: 5112
For next: 873

Since: 04-21-04
From: The Matrix...er, CA, U.S.A.

Since last post: 247 days
Last activity: 246 days
Posted on 04-22-04 07:44 AM Link | Quote
Originally posted by Euclid
some simple code to read bytes from file:

FILE* in = fopen(filename, "rb");
char somebyte;
fseek(in, 0x10000, SEEK_SET);
fscanf("%c", &somebyte);

That should get you the byte at offset 0x10000... I'm pretty sure.


Hmmm...seems easy enough, but there's something I'm wondering about. Namely, what I've quoted. Not to sound like some arrogant, know-it-all n00b, but wouldn't the code be like this, instead?


FILE * in;
in = fopen(filename, "rb");
char somebyte;
fseek(in, 0x10000, SEEK_SET);
fscanf(in, "%c", &somebyte);


Or at least judging from this, which is a handy-dandy resource I found. Something tells me that the first two lines are just a matter of me being anal/playing by rigid requirements. Also, said resource says that using the "c" type would only grab a character, and judging from what it says, I'd have to use something like "hc" or "%c %h." Is this just something coming from someone who probably doesn't know about the ROM hacking tricks of the trade, or is this actually true?

I'm trying to get my questions out in as few topics as possible, because I'd imagine that you guys'd get sick of seeing topics asking about C++ tips again and again. Unless, of course, I stuck them all in one topic and bumped the topic when I had another question, but I know how most communities feel about that.

I think I'll start some reading now, seeing as I can sit around asking questions all day, or I can actually do something to develop my skill.
neotransotaku

Baby Mario
戻れたら、
誰も気が付く
Level: 87

Posts: 474/4016
EXP: 6220548
For next: 172226

Since: 03-15-04
From: Outside of Time/Space

Since last post: 11 hours
Last activity: 1 hour
Posted on 04-22-04 07:59 AM Link | Quote
those two codes do the exact same thing--the code from Euclid intiailizes a variable on declarations while the code you put up declares a variable and then initializes it.

most, if not all ROM data, is stored in bytes. as a result, wider grabbing specifiers like %d, %ld, %f, etc would be useless. the only way in C/C++ to grab just 1 byte at a time is to use %c.

Originally posted by Icy Guy
I'm trying to get my questions out in as few topics as possible, because I'd imagine that you guys'd get sick of seeing topics asking about C++ tips again and again.


well, most of this board, or the people who come in here program in VB...there are few C/C++ programmers and even less Java programmers (I believe I'm the only one here on the board who prefers Java over C/C++ and VB
Euclid

Cheep-cheep
Level: 23

Posts: 31/193
EXP: 65528
For next: 2195

Since: 03-15-04
From: Australia

Since last post: 24 days
Last activity: 7 days
Posted on 04-22-04 05:52 PM Link | Quote
I'm not really a c++ programmer, i'm more of a C programmer.

imo you should start reading about C then go to c++.

And yes %c gets 8 bits (1 byte) which is mostly used in romhacking (no point getting 16 bits in little endian form with %d )

You can also read up on %X(%x), it suppose to read hex from keyboard, however i have never got it to work.
Dish

Spiny
Level: 38

Posts: 42/596
EXP: 355646
For next: 14801

Since: 03-15-04
From: Disch

Since last post: 18 days
Last activity: 18 days
Posted on 04-22-04 07:58 PM Link | Quote
I was always sketchy on %c... since it's not really defined as 1 byte... it's 1 character (which could be 2 bytes if you're using Unicode, if I understand correctly).

That's why i pretty much always use fread() when pulling data from a file... and fwrite() when writing to a file because you can specify exactly how many bytes you want to pull/write. No guesswork.

So instead of:

fscanf(in, "%c", &somebyte );

I'd do:

fread(&somebyte, 1, 1, in);

That way there's no question that you will be pulling 1 and only 1 byte, no matter what your compiler settings are.


Euclid:

%X and %x don't really read from a keyboard, they're just key characters that you can use with formatting functions (scanf, printf, etc). Also, %d would probably get 32 bits if used with fscanf
neotransotaku

Baby Mario
戻れたら、
誰も気が付く
Level: 87

Posts: 475/4016
EXP: 6220548
For next: 172226

Since: 03-15-04
From: Outside of Time/Space

Since last post: 11 hours
Last activity: 1 hour
Posted on 04-22-04 08:49 PM Link | Quote
well, if you use Visual Studio, you are supposed to use _T macro for strings anyways. For other compilers, it is assumed you are not working in unicode because most example C books (at least the most famous one) works in an ANSI environment.

fread...is that in stdio.h?
Icy Guy

Red Goomba
Level: 11

Posts: 6/36
EXP: 5112
For next: 873

Since: 04-21-04
From: The Matrix...er, CA, U.S.A.

Since last post: 247 days
Last activity: 246 days
Posted on 04-23-04 07:27 AM Link | Quote
Originally posted by neotransotaku
well, most of this board, or the people who come in here program in VB...there are few C/C++ programmers and even less Java programmers (I believe I'm the only one here on the board who prefers Java over C/C++ and VB


I'm beginning to see why VB is the preferred language here...1) it's visual and 2) it seems to be more straightforwards and readable. Granted, there are still some speed issues with VB, but...yeah. I might just stick with that, until I master it. Then I can try my hand at something else.

Still, thanks for the help, guys.
Dish

Spiny
Level: 38

Posts: 43/596
EXP: 355646
For next: 14801

Since: 03-15-04
From: Disch

Since last post: 18 days
Last activity: 18 days
Posted on 04-23-04 08:06 AM Link | Quote
Originally posted by Icy Guy
I'm beginning to see why VB is the preferred language here...1) it's visual and 2) it seems to be more straightforwards and readable. Granted, there are still some speed issues with VB, but...yeah. I might just stick with that, until I master it. Then I can try my hand at something else.

Still, thanks for the help, guys.


Meh... it seems simple to me.

fopen to open the file
fseek to seek
fread to read
fwrite to write
and fclose to close

I don't see how VB could be simpler
Kegan Marius

Micro-Goomba
Level: 8

Posts: 16/18
EXP: 1866
For next: 321

Since: 03-15-04
From: Chico, California, United States of America

Since last post: 553 days
Last activity: 339 days
Posted on 04-23-04 09:46 AM Link | Quote
VB might be preferred because it's easier to learn, easier to read, and compensates for programmer error much more than C++ does (maybe that's not so true in VB.Net). It's a rapid prototyping language, not a systems language.

C++ streams are pretty easy too:

create an ifstream object
.seekg to seek
.read to read in binary or >> in text
.write to write in binary or << in text
.close to close (or just let the ifstream object go out of scope, which autocloses it for you)

Might not be as readable, and it might bloat some, but it is MUCH safer.
Add to favorites | "RSS" Feed | Next newer thread | Next older thread
Acmlm's Board - I2 Archive - Programming - Getting data from an offset in C++ | |


ABII


AcmlmBoard vl.ol (11-01-05)
© 2000-2005 Acmlm, Emuz, et al



Page rendered in 0.024 seconds.