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 - porting VB.NET code to C++ help | |
Add to favorites | "RSS" Feed | Next newer thread | Next older thread
User Post
elixirnova

Red Paratroopa
Level: 22

Posts: 122/177
EXP: 56507
For next: 1843

Since: 04-05-04
From: Midgar

Since last post: 1 day
Last activity: 1 day
Posted on 11-29-04 09:42 AM Link | Quote
Herm.. I'm fairly new to C++ and im using windows API. Ive got my window set up and such but need to load a level to an array of 64bytes in VB.NET it looked something like this:

byte Byte1,Byte2; //for temporary storage
FileOpen(FileNumber, FileName, OpenMode.Binary, OpenAccess.Default)
For i = 0 To 63 Step 1
--FileGet(FileNumber, Byte1, Offset)
--FileGet(FileNumber, Byte2, Offset + 1)
--MegaTiles(i) = (Byte2 * &H100) + Byte1
--Offset = Offset + &H2
Next
FileClose(FileNumber)

I think instead of byte i should be using short though im not too understanding of how im supposed to use fget? and those to read from the "stream" as its called in C++
and in my C++ so far i have:

FILE * romStream;
char buffer[64];
int i=0;

//get File and put it into romStream
romStream = fopen ("rom.smc","rb");
if (romStream!=NULL){
for(i=0; i <=63; i++){
fgets(buffer,64,romStream);//this is the line im not sure about
}
fclose (romStream);}
}

im not too sure about this code... because for one it seams fgets is meant for Char is that what i want to store each byte individually in the buffer array any help appreciated


(edited by elixirnova on 11-29-04 12:44 AM)
sloat

Level: 16

Posts: 17/85
EXP: 18044
For next: 2212

Since: 05-21-04
From: South Central Delaware

Since last post: 19 days
Last activity: 5 hours
Posted on 11-29-04 10:01 PM Link | Quote
erm...well streams are something else in C++. you're using the older C file functions, but there's nothing wrong with that. Some people will argue that they're better.

But anyway, fgets will get a certain number of bytes from a file at the current position. you don't need the loop there. you could loop it and just get one byte at a time, but that'd be a bit of a waste.

after you get the data, you can access it like any other array. x = buffer[12]; or whatever.


about the size of the data, is each element supposed to be one byte or two? if it is two, you would have to cast. either make the buffer of type short and cast to char * when you're calling fgets, or keep it the way it is and make a variable of type short * that you'd use to access the array.
Dish

Spiny
Level: 38

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

Since: 03-15-04
From: Disch

Since last post: 18 days
Last activity: 18 days
Posted on 11-29-04 10:04 PM Link | Quote
bah @ fgets

fread and fwrite are the only f___ functions I really every use for file i/o due to their simplicity and control.

To read a 64 byte array from a file:

unsigned char buffer[64];
FILE* romStream;
romStream = fopen("rom.smc","rb");
if( romStream )
{
  fread( buffer, 1, 64, romStream ); /* reads 64 bytes from the start of the file, puts it in buffer */
  fclose( romStream );
}


it looks like you were doing some weird byte swapping in your VB code (looks like you want 16-bit words instead of individual bytes). If this is the case... you can use fread in the same manner to read 16-bit variables. all you'd need to do is redefine your 'buffer' array:

unsigned short buffer[32];

the fread line could look the same way... but I think you're supposed to change it to:

fread( buffer, 2, 32, romStream );

either will work on a little endian system. On a big endian system you'll have to swap the byte order by hand (unless you're planning on porting to Mac or some other big endian system, I wouldn't worry about it).

Final notes:

- after you fopen... fseek to your desired offset before you fread
- if fopen fails and you get NULL... do NOT call any other file i/o function.... not even fclose (after all... if nothing was successfully opened, nothing needs to be closed).


And kudos for making the switch =). The fewer VB coders the better.


erm...well streams are something else in C++. you're using the older C file functions


meh... C++ includes all of C. fopen and family are C... and as such they are also C++... so the "that's technically not C++" technicality is moot... since it is C++


(edited by Disch on 11-29-04 01:12 PM)
(edited by Disch on 11-29-04 01:13 PM)
neotransotaku

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

Posts: 1790/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 11-29-04 11:23 PM Link | Quote
that is one of the few good qualities about C++: still being able to program in C
elixirnova

Red Paratroopa
Level: 22

Posts: 123/177
EXP: 56507
For next: 1843

Since: 04-05-04
From: Midgar

Since last post: 1 day
Last activity: 1 day
Posted on 11-30-04 04:24 AM Link | Quote
Ill try this stuff tonight just got home from swim practice. Yes I like the C++ API for Windows but i still use C coding because i dont feel like learning a whole buncha new C++ stuff when its useless to me...
Dish

Spiny
Level: 38

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

Since: 03-15-04
From: Disch

Since last post: 18 days
Last activity: 18 days
Posted on 11-30-04 08:12 AM Link | Quote
Originally posted by neotransotaku
that is one of the few good qualities about C++: still being able to program in C


It's the reason why I just don't understand why ANYONE would code in straight C (cue Parasyte). I mean most of what C++ offers is stupid... yes. But if you code in C++ anyway, you can take what you want (and it does have a lot of great stuff to offer), but still fall back to vanilla C when perferable.
sloat

Level: 16

Posts: 18/85
EXP: 18044
For next: 2212

Since: 05-21-04
From: South Central Delaware

Since last post: 19 days
Last activity: 5 hours
Posted on 11-30-04 10:42 AM Link | Quote
Disch, I was just splitting hairs with the stream thing. Personally, when i think of streams i think of the fstream class. Then I vomit.

I like the C style file functions better, but i like the Windows API the best. Not portable, but whatever. more flexible, like taking a void pointer instead of char. And I'm pretty sure the C Runtime with VC++ doesn't have functions for memory mapped files. It's so much simpler to use a pointer to walk through large files.
Add to favorites | "RSS" Feed | Next newer thread | Next older thread
Acmlm's Board - I2 Archive - Programming - porting VB.NET code to C++ help | |


ABII


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



Page rendered in 0.022 seconds.