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 - C++ question | |
Add to favorites | "RSS" Feed | Next newer thread | Next older thread
User Post
interdpth

Rex
Level: 36

Posts: 366/527
EXP: 294398
For next: 13712

Since: 03-20-04

Since last post: 10 days
Last activity: 31 days
Posted on 11-12-04 06:04 AM Link | Quote
#include
int main()
{
long a;
int b;
int c = 0xAD;
FILE* fp;
fp = fopen("E:\\Roms\\Em\\BG2.GBA","rb");
fseek(fp, c, "setcurr" );
gets(fp);

printf("%l",&a);
scanf("%i", &b);
fclose(fp);
}

I keep getting the error 9 C:\Documents and Settings\sartain\My Documents\k.cpp cannot convert `_iobuf' to `FILE*' for argument `1' to `int fseek(FILE*, long int, int)'

Please help
Dish

Spiny
Level: 38

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

Since: 03-15-04
From: Disch

Since last post: 18 days
Last activity: 18 days
Posted on 11-12-04 06:35 AM Link | Quote
fseek takes no strings.

1st param is the file pointer
2nd param is the offset
3rd is where you're offsetting from (by use of defines... SEEK_SET, SEEK_END, or SEEK_CUR are the common 3)

so I'd assume you want your fseek line to look like:

fseek( fp, c, SEEK_SET );

---

of course you'll want to return something from main() too ;P unless you declare it as void. And I'm not sure why your compiler is complaining about the 1st param when it should be complaining about the 3rd. Ah well.


(edited by Disch on 11-11-04 09:37 PM)
interdpth

Rex
Level: 36

Posts: 367/527
EXP: 294398
For next: 13712

Since: 03-20-04

Since last post: 10 days
Last activity: 31 days
Posted on 11-12-04 06:41 AM Link | Quote
Thanks Disch. Also you see that long a; I got? How would I get a couple values? Like in VB the command Get #1, , Like that I know fgetc wont work for what I want. So how would I do this?
Dish

Spiny
Level: 38

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

Since: 03-15-04
From: Disch

Since last post: 18 days
Last activity: 18 days
Posted on 11-12-04 06:47 AM Link | Quote
The only i/o functions I really ever use are fread and fwrite due to their total control and simplicity. For example... say you want to read 16 bytes from a file:

----------
BYTE somebuffer[16];
fread( somebuffer, 1, 16, fp ); //reads 16 bytes from 'fp' and fills somebuffer with them
---------

4 params are simple:

1 - pointer to the buffer to receive the data
2 - size of each element (1 above because we're reading individual bytes, but this might be 2 if reading WORDs, or 4 if reading DWORDs)
3 - number of elements to read (this param * param 2 will be the total number of bytes from the file --- ie, fread( blah, 2, 5, fp ) <-- reads 5 elements, each 2 bytes in size making 10 bytes total read)
4 - of course the file pointer from which we're reading

fwrite works identically, except it writes instead of reads.
interdpth

Rex
Level: 36

Posts: 368/527
EXP: 294398
For next: 13712

Since: 03-20-04

Since last post: 10 days
Last activity: 31 days
Posted on 11-12-04 08:06 AM Link | Quote
THANK YOU DISCH!!!! A new problem arises.
I get it to display the variable but it comes out as AXVE░ " I have the a set char a[4]; But I just want it to display the first 4. Please help
Acmlm

Torosu
heh
Level: 51

Posts: 932/1173
EXP: 981994
For next: 31944

Since: 03-15-04
From: Somewhere that isn't outside of Sherbrooke, Québec, Canada

Since last post: 39 days
Last activity: 3 hours
Posted on 11-12-04 08:11 AM Link | Quote
char a[5];

And then, after reading the 4 bytes:
a[4]=0;


Or another way, keeping it as a 4 char array:

for(int i=0; i<4; i++)
printf("%c", a[i]);


(edited by Acmlm on 11-11-04 11:25 PM)
interdpth

Rex
Level: 36

Posts: 369/527
EXP: 294398
For next: 13712

Since: 03-20-04

Since last post: 10 days
Last activity: 31 days
Posted on 11-12-04 08:54 AM Link | Quote
Now so I got that down how would I make a table file and put it to use?
Dish

Spiny
Level: 38

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

Since: 03-15-04
From: Disch

Since last post: 18 days
Last activity: 18 days
Posted on 11-12-04 09:28 PM Link | Quote
Properly loading a table file and using it is somewhat tricky. There are shortcuts you can use if you want to "cheat" though. For example a table file might contain something like:

7F32=cha

(where 2 bytes = 3 characters). Implimenting something like that (along with the rest of the 'normal' entries) can be rough. But if you 'cheat', you can easily impliment a simple table which only has 1-byte entries and ignore the multi-byte entries.

Also... tables files can have DTE/MTE ("7F=ch", "24=ill"), getting all the characters in a single entry can be a little rough (but not all that hard). If you cheat here, you can just assume each byte will represent no more than 2 chars (however, I don't recommend doing this).

Anyway... what you could do is have 256 char pointers, and put a string in each one, which will give you an array of strings which represents your table. I don't think I'm explaining this well.... say you have a table file with these entries:

1F=be
9F=D
9A=o

to represent this in your array, you'd have:

your_table[0x1F] <- "be"
your_table[0x9F] <- "D"
your_table[0x9A] <- "o"

This way, to display text, you can just run each byte through your array and output the string which each byte represents.

However, since the length of each string is unknown (a table file could have an entry with one byte representing, say, 6 or 7 characters, or even more for all your program knows).. it'd be best to dynamically allocate memory for each string rather than work with fixed arrays.

Anyway... it seems like I'm rambling. I just woke up so sorry if this post is hard to follow hahaha
interdpth

Rex
Level: 36

Posts: 370/527
EXP: 294398
For next: 13712

Since: 03-20-04

Since last post: 10 days
Last activity: 31 days
Posted on 11-12-04 10:24 PM Link | Quote
Alrighty then thanks.
Also. what does each one represent? Like int = 2 byte, long = 4 byte what is the command for 1 byte?
Dish

Spiny
Level: 38

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

Since: 03-15-04
From: Disch

Since last post: 18 days
Last activity: 18 days
Posted on 11-12-04 10:37 PM Link | Quote
I'm not 100% sure if these values are always this size... I know on different platforms, different types have different sizes... but this is the case afaik:

char - 8-bit
short - 16-bit
long - 32-bit
int - whatever your system implies (on 32-bit windows, int is 32-bit)

All values are signed by default. If you want an unsigned var you can prepend 'unsigned' to the declaration (ex: "unsigned char myvar;")

If you're #including windows.h, there's also a few other types:

BYTE - unsigned 8-bit
WORD - unsigned 16-bit
DWORD - unsigned 32-bit

edit:

if you ever want to check to be sure... you can use the sizeof operator. try doing:

printf( "%d", sizeof( int ) );

it should output '4', signaling that the size of 'int' is 4 bytes


(edited by Disch on 11-12-04 01:48 PM)
interdpth

Rex
Level: 36

Posts: 371/527
EXP: 294398
For next: 13712

Since: 03-20-04

Since last post: 10 days
Last activity: 31 days
Posted on 11-12-04 10:57 PM Link | Quote
Here's my new code. Which isn't compiling so some help me? yes this is pokemon
#include
using namespace std;
class cTrainer {
char bGroup,bMusic,bSprite;
char sName[13];
int Item1,item2,item3,item4;
long lDuo;
long lFilller,lHeld,lPntr;
};
cTrainer MyTrainer;
int main()
{
char a[5];
char newfile[256];
int k;
char c[7] = "Yes \n";
char b[6] = "No \n";
cout << "Enter The location of the rom \n";
cin.getline(newfile,255);
cout << "is it ruby \n";
cin >> k;
if (k == 1)
{
cout << c;
FILE* fp;
fp = fopen(newfile,"rb");
fseek(fp, 0x1F0525, SEEK_SET);
fread( MyTrainer, 1, 40, fp );
cout << MyTrainer;
//printf("%l",&a);
cin >> b;
fclose(fp);
}
else
{

cout << c;
cin >> k;
}
}


(edited by interdpth on 11-12-04 01:59 PM)
Dish

Spiny
Level: 38

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

Since: 03-15-04
From: Disch

Since last post: 18 days
Last activity: 18 days
Posted on 11-12-04 11:33 PM Link | Quote
1) if you're using fopen and related fuctions, you need to #include <stdio.h>

2) you're still not returning anything from main(). If you declare it as 'int', it has to return a value. Either switch it to 'void' or return something.

3) This line is problematic for a few reasons: fread( MyTrainer, 1, 40, fp );
a) 'MyTrainer' is not a pointer. If you want to supply a pointer to your MyTrainer slap a '&' before it.
b) your cTrainer class seems to be bigger than 40 bytes. Assuming there's no padding at all, I count 48 bytes. With padding (which is probably the case), I count about 52 bytes. This wouldn't cause a compiler problem but you won't get the results you might be expecting.
c) If the compiler pads your cTrainer class to start vars on 4-byte boundaries (which it probably will), you'll get unexpected results. Don't read a full struct/class at a time unless it's tightly packed and your sure it won't produce padding. Instead, read each individual value you want from the file one at a time.

4) Another problem with this line: cout << MyTrainer;. There is no way cout is overloaded to output your whole class. If you want to examine the individual values in MyTrainer, print them one at a time.

5) Not a compile problem... but " fp = fopen(newfile,"rb");" <--- if 'newfile' can't be found/opened, fopen will return NULL. Your code doesn't check for a NULL return, so if the user specifies a path to a file that doesn't exist (or can't be opened) the program will crash.

6) Not a real problem... but I dispise cout/cin ;P. printf/scanf fo-evah! If there were other problems relating to your cout/cin lines I wouldnt've caught them... since I'm totally unfamiliar with cout/cin's yukkiness.


(edited by Disch on 11-12-04 02:59 PM)
Add to favorites | "RSS" Feed | Next newer thread | Next older thread
Acmlm's Board - I2 Archive - Programming - C++ question | |


ABII


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



Page rendered in 0.010 seconds.