Platform-dependent files?
Ron Ofir

Hi all,

I have tried making a small Allegro app for my Java game that takes a bitmap and writes out all of the coordinates of non-white pixels to a file. It writes the pixels in pairs of two integers, no spaces, no tabs, no newlines, just integers. The app runs fine (after some debugging of course), so I went on to try and read the file in my Java game, however the results seem very wrong. When I should get a pair of 4 4 I get 67108864 67108864, and so on. I tried reading the file using the Allegro app and the numbers come out fine (eexcept for the last pair that comes twice for some reason, but I think I'm doing wrong EOF checks). The application is running on an Ubuntu Linux and the Java game is running on Windows XP, so I thought it might be some endian problems, and here comes my problem: How can you choose different byte order in Java for reading files? I would also be glad for any other endian-issues feedback/random info.

(I'm using simple fwrite-fread in order to write and check the file, and DataInputStream in order to read it, oh, and both applications read/write the same number of pairs)

Cookies promised.

Fladimir da Gorf

I wouldn't rely on any specific format when using DataInputStream. You should use your own stream handler if you make any assumptions.

Peter Hull

I agree with Fladimir; DataInputStream should probably only be used with files created by DataOutputStream.

However you should be able to find the format used with Google. Assuming it's simply a big endian format, you can use the relevant Allegro functions, for example

#include <allegro.h>

int main(int argc, char* argv[])
  PACKFILE* f = pack_fopen(argv[1], F_WRITE);
  int i = 4;
  pack_mputl(i, f);
  return 0;

produces a file with a 4 in it, that can be read by this program

3class Fr
5 public static void main(String[] args)
6 {
7 System.out.println(args[0]);
8 try {
9 FileInputStream is = new FileInputStream(args[0]);
10 DataInputStream ds = new DataInputStream(is);
11 int i = ds.readInt();
12 System.out.println("Answer " + i);
13 } catch(Exception xep) {System.err.println(xep);}
14 }

Random info: I had leeks for dinner today


Fladimir da Gorf

For general communication between programs made with different languages, I'd use XML.

Ron Ofir

I'll check the pack_mputl solution. I don't want to use XML as it will make the files about 20 times bigger and complicate the writing (not so much, but still).

BTW, don't you have to initialize allegro (in console mode, SYSTEM_NONE) in order to use it's routines?


For general communication between programs made with different languages, I'd use A SIMPLE TEXT FILE.

Ron Ofir

Java uses big-endian so Peter's solution works. I've just tried it with a couple of things and it looks great (I'll post screenshots later when I add some more effects).

James Stanley

I had leeks for dinner today

Leeks are horrible!
Coincidentally, my Grandads vicar steals leeks from the fields.

Thread #588082. Printed from