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

Binary Output error (?)

Started by Favardin Oct 31, 2004 at 4:29 AM 6 replies 1.7k views
Original Post
Favardin
Favardin
I'm trying to output a simple binary file with c++ (for a map file, but that's not important). But my code (see below) just results in the creation of an empty file. The code looks like this:

#include <fstream.h>
#include <string>

using std::string;

struct OBJECT
{
   int number;
   char letter;
} obj;


int main () {
  string  filename = "G:/Projekte/Allegro/Isometric/ASCII Edit/test.lvl";

  ofstream level (filename.c_str(),ios::binary);

  obj.number = 15;
  obj.letter = 'M';

  level.write((char *)(&obj), sizeof(obj));

  level.close();

  system("PAUSE");
  return 0;
}

I assume the syntax is right (if not, please help me - I just can't see any error). Btw (if it's an system-related problem), I'm running Win XP (yeah, ugly) and I'm compiling with MingW (I think GCC 2.95.2). And finally: I tried the same with a c-script, and it worked without problems (but I really want to use c++ if possible - please don't argue about that ^^).
snk_kid
snk_kid
might has something to do with this line:

string  filename = "G:/Projekte/Allegro/Isometric/ASCII Edit/test.lvl";


try changing it to:

string  filename = "G:\\Projekte\\Allegro\\Isometric\\ASCII Edit\\test.lvl";


and don't use #include fstream.h is deprecated header, its #include

EDIT: well i tested your code and it works, maybe you have the your path wrong or something.
Favardin
Favardin
Hm, did the changes (thanks for the advice on string.h), still the same error.
snk_kid
snk_kid
Quote:
Original post by Favardin
Hm, did the changes (thanks for the advice on string.h), still the same error.


Your code seems to work for me, so maybe you have your path wrong.
Favardin
Favardin
I don't think so - after all, the file itself is created (if not already there) but it's EMPTY.
snk_kid
snk_kid
Quote:
Original post by Favardin
I don't think so - after all, the file itself is created (if not already there) but it's EMPTY.


sorry i can't see anything wrong, maybe try using a newer version of MinGW, i'm using MinGW candidate version (gcc 3.4.2)
mx
mx
Why you just don't use relative paths? At least for the test... I haven't tried the code, but when someone posted here, that it works fine, it seems to be file path problem... so i would try this.

m.
Favardin
Favardin
Okay, it works (after installing a newer compiler) - but I've stumbled over a mistake in my code (the newer comp. wouldn't compile it!): I had to declare fstream as either std::fstream OR use namespace std. Maybe that caused the mistake. Thanks a lot, anyway ;)

Topic Locked

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

Sign in to reply to this topic.