473,666 Members | 2,284 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Reading & Writting Binary File

72 New Member
Got a question about reading and writing binary files.

I know that to write a value(integer) to a binary file I can use the fileoutputstrea m to do it.
Expand|Select|Wrap|Line Numbers
  1. try 
  2. {
  3.         // Create an output stream to the file.
  4.         FileOutputStream file_output = new FileOutputStream (outFile);                                                              
  5.  
  6.         //for(int i = 0; i < cInt.length; i++)
  7.  
  8.         file_output.write(100);                
  9.         file_output.write(200);                
  10.         file_output.write(300);                
  11.  
  12.         // Close file when finished with it..
  13.         file_output.close ();
  14. }
But I cant seem to read back from the file these values using the fileinputstream

Expand|Select|Wrap|Line Numbers
  1. int bInt = 0;
  2. try 
  3. {
  4.     // Wrap the FileInputStream with a DataInputStream            
  5.     FileInputStream file_input = new FileInputStream (inFile);
  6.     DataInputStream data_in    = new DataInputStream (file_input);
  7.  
  8.     while(true) 
  9.     {
  10.         try 
  11.         {                    
  12.             bInt = data_in.readInt();
  13.         }
  14.         catch (EOFException eof) 
  15.         {
  16.             //end of file
  17.             break;
  18.         }
  19.  
  20.         System.out.println("Test: "+bInt);
  21.     }
  22.  
  23.     data_in.close ();
  24. catch(IOException e) 
  25. {
  26.     System.out.println ( "IO Exception =: " + e );
  27.     System.exit(0);
  28. }
Any idea where I went wrong? Thanks in advance :)
Nov 14 '07 #1
1 2902
KWSW
72 New Member
ok nvm... solved it :)
Nov 14 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
1262
by: fdunne2 | last post by:
How do you write/read a list of siganl points into a c program? each point is on a seperate line.
50
4932
by: Michael Mair | last post by:
Cheerio, I would appreciate opinions on the following: Given the task to read a _complete_ text file into a string: What is the "best" way to do it? Handling the buffer is not the problem -- the character input is a different matter, at least if I want to remain within the bounds of the standard library.
7
6055
by: John Dann | last post by:
I'm trying to read some binary data from a file created by another program. I know the binary file format but can't change or control the format. The binary data is organised such that it should populate a series of structures of specified variable composition. I have the structures created OK, but actually reading the files is giving me an error. Can I ask a simple question to start with: I'm trying to read the file using the...
30
4570
by: siliconwafer | last post by:
Hi All, I want to know tht how can one Stop reading a file in C (e.g a Hex file)with no 'EOF'?
6
5261
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
6
7841
by: jjhall | last post by:
I need to open and read a file that has a combined ascii and binary format. Don't ask me why it is this way; I think it is kind of stupid, but I didn't write the program that generated the file. Anyway, there is a short header that is written in ascii, then the data is in binary. I have tried opening the file in ascii format first with ifstream data("filename", ios::in); Then I read to a buffer until I reach the end of the header. ...
5
1775
by: Vijay | last post by:
Hi All, I have written below code to read and write a file (Big file). But every time, one extra character(may be NULL char) has been written in output file. Can any one please tell me why it is happing and how we can solve this? CString temp;
3
2823
by: The Cool Giraffe | last post by:
Regarding the following code i have a problem. void read () { fstream file; ios::open_mode opMode = ios::in; file.open ("some.txt", opMode); char *ch = new char; vector <charv; while (!file.eof ()) { do {
3
2160
by: Ahmad Jalil Qarshi | last post by:
Hi, I have a text file having size about 2 GB. The text file format is like: Numeric valueAlphaNumeric values Numeric valueAlphaNumeric values Numeric valueAlphaNumeric values For example consider following chunk of actual data:
0
8443
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8781
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8639
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6192
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5663
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4198
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4366
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2769
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1772
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.