Advertisement Jump to content


This topic is now archived and is closed to further replies.


char* binary i/o

This topic is 6342 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I''ve been trying this binary in/output out and it all works, except with char* ! I''ve been hammering on it for hours and it just won''t work! I wrote 2 simple programs to test it out:
// cain.cpp

#include <fstream>
#include <iostream>

int main()
	char* tmp;

	ifstream ifs("ca", ios::binary | ios::in); // the extra flag is due to a bug in gcc up til v2.95*)&tmp, sizeof(char)*8);

	cout << tmp << endl;
	return 0;
... and one for the writing.  
// caout.cpp #include #include int main(int argc, char* argv[]) { char* tmp = "tjip"; ofstream ofs("ca", ios::binary | ios::out); ofs.write((char*)&tmp, sizeof(char)*8); ofs.close(); return 0; } [/source] The only thing it does is return a string used somewhere before, so it''s probably garbage it reads in... Please help me out, I''m desperate!

Share this post

Link to post
Share on other sites
OK, that code looks very wacked up.
I don't think you should read in 8 bytes (64 bits) into a pointer of type char
#include <fstream.h>

int main(void)
char c;
ifstream("foo", ios::binary);
ifs.get(&c); // read in first byte, and store it in c

return 0;

Edited by - Clash Rocker on September 8, 2001 1:37:46 PM

Share this post

Link to post
Share on other sites
A variable of type "char *" is a pointer to data of type "char." Each "char" is a byte. takes a pointer to allocated memory of type "char" and the number of bytes to read from the file sequentially in memory. So, for example, here's some code:

char * ptrChars; //Create pointer

ptrChars = new char[8267]; //Allocates 8267 (just a random number I chose) of bytes (or "chars") in memory and makes ptrChars point to them

ifstream fileobject("abc.dat", ios::binary | ios::in); //Creates an ifstream object and opens the file "abc.dat" with it for binary input to the program., 8267); //Reads in the first 8267 (the same number of chars I allocated in memory) and places them in memory, starting at the memory location pointed to by ptrChars.

ifs.close(); //Closes file

delete [] ptrChars; //De-allocates the memory pointed to by ptrChars. The "[]" is added to tell the "delete" operator to delete all 8267 chars that were allocated, instead of just the one at the memory location pointed to by ptrChars

What your source did, on the other hand, was read in 8 bytes from the file, and place them in memory starting at the memory location storing the uninitialized pointer to memory. What you would WANT to do is read in data and put it at the memory location POINTED TO by your pointer and not the memory location OF the pointer.

Edited by - TerranFury on September 8, 2001 7:47:50 PM

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!