Skip to main content
GameDev.net gamedev.net
🔒 Locked

C++ equivalent?

Started by Nerothos Feb 3, 2004 at 3:55 PM 13 replies 800+ views
Original Post
Nerothos
Nerothos
Is there a function or class in C++ that does exactly what C''s fread / fwrite does with structs? Thanks!
Jump in and crash the system...it''s quite humbling.
Sneftel
Sneftel
the istream::read() and ostream::write() functions are probably what you need. They each have versions that operate on a character array, so you can just cast your struct pointer to a char pointer and go wild.

EDIT: whoops. Yeah.

"Sneftel is correct, if rather vulgar." --Flarelocke

[edited by - sneftel on February 3, 2004 5:06:34 PM]
Oluseyi
Oluseyi
std::istream::read
std::ostream::write
SiCrane
SiCrane
Technically fread() and fwrite() are part of the C++ standard library too.

However, I'm guessing that get() and put() are *not* what you're looking for. The two paramter version of get() has an implicit \n delimeter. And ostream::put() only writes one character at a time.

edit: @!$% smily code

[edited by - SiCrane on February 3, 2004 5:05:10 PM]
SiCrane
SiCrane
quote:
Original post by Sneftel
EDIT: whoops. Yeah.


Sure just edit my post into a non sequitur

AP: If you know how to use fwrite() to output a struct, you should have no trouble using basic_ostream::write().
Oluseyi
Oluseyi
quote:
Original post by Anonymous Poster
http://www.cplusplus.com/ref/iostream/ostream/write.html
This definition allows a string and a byte size.
A char * is a pointer, not a string. When a buffer of memory''s initial address offset is cast to char *, sequential access is byte-wise (because the size of a char is typically a byte). So cast the address of your struct to char * and pass it to write.

Learn your pointers.
Enigma
Enigma
Err you're using memory you haven't allocated. Try:
#include <iostream>
#include <fstream>
using namespace std;
typedef struct {
int i;
char *p;
float d;
} ff;
int main() {
ofstream out("a.txt", (ios::out | ios::binary));
ifstream in("a.txt", (ios::in | ios::binary));
ff *test = new ff(); // allocate memory
test->i = 500;
test->p = "test";
test->d = 2.00432f;
char *t = (char *) test;
out.write(t, sizeof(ff));
out.close();
in.read(t, sizeof(ff));
// no need to assign back to test beacuse t == test
cout << test->i << endl;
in.close();
delete test; // delete memory
return 0;
}


EDIT2: oh, and as this is c++ you don't need to typedef your struct:
struct ff{
int i;
char *p;
float d;
};

will do.

Enigma

EDIT: missed a line break

[edited by - Enigma on February 3, 2004 6:22:50 PM]

[edited by - Enigma on February 3, 2004 6:24:20 PM]
Drag0n
Drag0n
Just in case you''re interested, you can handle
struct
s like a normal data type. So instead of saying


typedef struct {
int i;
char* p;
float d;
} ff;


You could just go and say:


struct ff {
int i;
char* p;
float d;
};


I just thought you might want to know!

cya,
Drag0n

-----------------------------
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to build bigger and better idiots. So far, the universe is winning..." -- Rich Cook

My future web presence: Dave''s Programming Resources
-----------------------------"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to build bigger and better idiots. So far, the universe is winning..." -- Rich Cook"...nobody ever accused English pronounciation and spelling of being logical." -- Bjarne Stroustrup"...the war on terror is going badly

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.