Solved

Reading C++ created binary file in Java...

Posted on 2004-04-14
13
867 Views
Last Modified: 2008-02-26
For my Networking class, my partner and I have been assigned the task of creating a client/server application.  The details of the project are thus:

-Our instructor wrote a small C++ program that writes flight data in a binary file.  His program takes an ascii file of the data, and writes a binary version.

Code for program that creates the binary file:  http://www.atthefrontsystems.com/scheduler.c
Header for the flitdata structure:  http://www.atthefrontsystems.com/sched.h
ASCII sample datafile: http://www.atthefrontsystems.com/sched.ascii
Binary sample datafile: http://www.atthefrontsystems.com/sched.db

-Our task is to create a Java server and client app.  The client will pass fairly simple queries to the server (i.e. list all flights out of a certain airport).    The server will then return the results to the client, who displays the data on the screen.

Details on project: http://www.atthefrontsystems.com/project.pdf

My problem is this.  I can't seem to properly read the binary data.  I'm still a novice programmer in both Java and C++, but it seems to me that the C++ program just wrote most of the file as it's primitive data types.  Taking this into account, I tried to read it in using the "equivalent" Java data types.  For instance, the beginning of the datafile is the Flight#, written (seemingly) as a C-string.  So to test my theroy, I wrote some small code to just read the first char, and print it:

----
import java.io.*;

public class readit {
      public static void main (String args[]) throws Exception {
            FileInputStream fis = new FileInputStream("sched.db");
            DataInputStream dis = new DataInputStream(fis);

            System.out.println(dis.readChar());
      }
}
----

Instead of printing the expected 'A', I get a '?'

Undoubtdly, I'm going about this completely wrong, and would really appreciate some help with this problem.  Should I be RandomAccessing the file instead of using a Stream?  Is there some trick to manual type conversion that I'm overlooking?

If someone could please look through the linked material contained in this rambling post, and "show me the way", I'd be forever in their debt.

P.S.  (It's Friday, April 23d... so I'm not waiting till the COMPLETELY last minute.)  ;)
0
Comment
Question by:MTXperience
  • 5
  • 5
  • 3
13 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 10828508
>>Instead of printing the expected 'A', I get a '?'

It's probably not a char (two-byte value) in the sense of a letter code and therefore won't decode properly. If it's a two-byte number then you could do:

System.out.println((int)dis.readChar());
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 130 total points
ID: 10828527
In point of fact, it looks like the flight number is four bytes. You could therefore do

byte[] flightNum = new byte[4];
dis.read(flightNum);
System.out.println(new String(flightNum));

0
 
LVL 92

Expert Comment

by:objects
ID: 10828584
how to read it is a little system dependant.
also C strings are zero terminated, whereas Java strings are not.
0
 
LVL 92

Expert Comment

by:objects
ID: 10828617
why exactly does your Java server need to use the binary file created by the C program.
0
 

Author Comment

by:MTXperience
ID: 10829057
CEHJ, your code for reading the chars worked splendidly.  Any insight into how to read the ints?  (3 of them... 2 in the Time structure... 1 other)  How about the float?
0
 

Author Comment

by:MTXperience
ID: 10829067
>>why exactly does your Java server need to use the binary file created by the C program.

Answer:  Because our professor told us too.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 92

Expert Comment

by:objects
ID: 10829074
try readInt() and readFloat()
0
 
LVL 92

Expert Comment

by:objects
ID: 10829076
> Because our professor told us too.

they are annoying like that aren't they :)

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10829080
You can read various primitives with DataInputStream. See

http://java.sun.com/j2se/1.5.0/docs/api/java/io/DataInputStream.html
0
 
LVL 92

Expert Comment

by:objects
ID: 10829083
C data types can vary in size though, so you'll need to determine exactly what size is being used.
0
 

Author Comment

by:MTXperience
ID: 10829185
Thanks guys, we knew about the primitive data type reading... we just made a small oversight in our tests that skewed things at first.  But that all seems to be working fine now.  Again... thanks much for your help.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10829242
:-)

You seem to be getting on fine, but you should probably create a class of type, say, FlightInformation and read the binary file such that you're left with a collection of FlightInformation objects
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10829369
>>In point of fact, it looks like the flight number is four bytes.

But is declared as 5 in sched.h
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Eclipse Neon and jdk 1.8.0 11 138
Core Java. What output will be and why ? 6 66
eclipse formatting 6 73
factorial example 4 35
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now