Current location: Hot Scripts Forums » Programming Languages » C/C++ » Re: Porting from java to c++!

Re: Porting from java to c++!

Reply
  #1 (permalink)  
Old 09-06-04, 01:01 PM
addit addit is offline
Newbie Coder
 
Join Date: Mar 2004
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Porting from java to c++!

Hello all, I've stumbled across a slight problem while trying to port some java code to c++. The following java code is:
Code:
...
DataInputStream dis = null;
try {
dis = new DataInputStream (new BufferedInputStream(new FileInputStream("fileblah"))); // create the stream
}
...
try {
int number = dis.readInt();
}
....
Now all the above is doing is reading an interger from the file right? In other words four bytes from a file. So this is the my code in C++: (i'm using stdio, yes yes yes i know, i know?!)
Code:
...
int number;
fread ((char *) &number,4,1,file);
...
However the java code returns: 435
Where as my c++ code returns: -1291780096

Whats going on? I'm still quite a new at all this. Can anyone try and help me port that java code to c++ so it returns the same number! (you don't have to use the standard library).

Thanking you in anticipation,
Adam
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 09-09-04, 03:26 PM
Sokolov Sokolov is offline
Newbie Coder
 
Join Date: Sep 2004
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Hi Adam,

Your code looks fie, nothing wrong. Maybe you need to check if you opened the file correctly. Also check the return value of the read function.

Here a simple C and C++ exaple of writing and reading a number from file (binary):
Code:
#include <stdio.h>
#include <stdlib.h>


int main(void)
{
   FILE *fp;
   int number = 9;
   char *filename = "c:\\test.bin";

   if((fp = fopen(filename, "wb")) == NULL)
   {
      perror(filename);
      return EXIT_FAILURE;
   }

   fwrite(&number, sizeof number, 1, fp);
   fclose(fp);

   if((fp = fopen(filename, "rb")) == NULL)
   {
      perror(filename);
      return EXIT_FAILURE;
   }

   fread(&number, sizeof number, 1, fp);
   printf("number = %d\n", number);
   fclose(fp);
   return EXIT_SUCCESS;
}
Code:
#include <iostream>
#include <fstream>

using namespace std;

int main(void)
{
   char *filename = "c:\\test.bin";
   int number = 9;
	
   ofstream out(filename, ios::out|ios::binary);

   if(!out.is_open()) 
   {
      cerr << "Error opening output file" << endl;
      return EXIT_FAILURE;
   }
   out.write((char *)&number, sizeof number);
   out.close();

   std::ifstream in(filename, ios::in|ios::binary);
	
   if(!in.is_open()) 
   {
      cerr << "Error opening input file" << endl;
      return EXIT_FAILURE;
   }
   in.read((char *)&number, sizeof number);
   cout << "number = "<< number << endl;
   in.close();

   return EXIT_SUCCESS;
}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 09-09-04, 04:47 PM
addit addit is offline
Newbie Coder
 
Join Date: Mar 2004
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Talking Thanks for the reply...

Thank you for the reply... However I fixed my problem by myself. It was either c++ or java reading the bytes in a fishy order... reverse I think! Well thank you anyway.

It's nice to know theres always someone there trying to help ya out!

Adam

EDITED: Oh yeh before I forget I got another problem! Do you know the equivilant of the java triple right shift operator ie. >>>?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 09-14-04, 04:11 PM
Sokolov Sokolov is offline
Newbie Coder
 
Join Date: Sep 2004
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
variable >>= 3 (or variable = variable >> 3)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
We need more java talk rooshine Everything Java 4 05-08-04 10:44 PM
Learning Java Mstrs_Tatiana New Members & Introductions 1 05-03-04 06:59 AM
Can anyone help me with Java monkeymam Everything Java 4 04-26-04 03:20 AM
java tables bwge JavaScript 1 03-09-04 05:50 AM
Java Virtual Machine lordmerlin Everything Java 4 02-17-04 01:43 PM


All times are GMT -5. The time now is 03:33 PM.
vBulletin® Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.