How to use fstream or istream to read a binary file and write out what's in it?

This is a discussion on How to use fstream or istream to read a binary file and write out what's in it? within the C++ Programming forums, part of the General Programming Boards category; 1) I am trying to open a binary file. 2) With a pointer value, I am trying to read the ...

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    13

    How to use fstream or istream to read a binary file and write out what's in it?

    1) I am trying to open a binary file.
    2) With a pointer value, I am trying to read the value in memory set by the pointer. But the first value I get I want to start the pointer from 0 to a specified location.
    3) I want to get that value in memory and be able to use it (i.e. int, double) so I can do math with it!!

    4) I'm having trouble!!!! HELP!!!!!

    Code:
    		
    	fstream data;
    		
    	data.open(filename, ios::in |ios::out | ios::binary);
    	
    	if(!data.is_open())
    	{
    		cout << "Error opening file. Please try again." << endl;
    		exit(1);
    	}
    
    
           ptr5 = user_def_beg_pix_ptr = (given_line - 1)*numsamples + (given_col - 1);
    	
    	ptr1 = ptr2 - 1;
    	ptr2 = ptr5 - numsamples;
    	ptr3 = ptr2 + 1;
    	ptr4 = ptr5 - 1;
    	ptr6 = ptr5 + 1;
    	ptr7 = ptr8 - 1;
    	ptr8 = ptr5 + numsamples;
    	ptr9 = ptr8 +1;
    	
    	ptr5 = ptr5*sizeof(float);
    	ptr6 = ptr6*sizeof(float);
    	ptr4 = ptr4*sizeof(float);
    
    //here's the important part giving me trouble 
    
    	data.seekg(0,ios::beg);
    	size = data.tellg();
    	cout<< "This is the first size:" << size << endl;
    			   
    	value[0] = 0; //clearing first array just to test things.
    	value[1] = 0;
    	value[2] = 0;
    	//may erase things later
    	
    	data.seekg(ptr1, ios::beg);
    	size = data.tellg();
    	cout<< "this is the second size: " << size << endl;
    	
    	data.write((char*) &value, sizeof value);
    	data.read((char*) &value, sizeof value);
    	cout << value[0] << " -next value- " << value[1]
    	<< " -next value- " << value[2] << endl;
    Output:




    This is the first size:-1
    this is the second size: -1
    0 -next value- 0 -next value- 0
    this is what's in ptr 96876
    This is the pixel val 5: 96876
    96872
    96880

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,450
    Quote Originally Posted by Blah937
    2) With a pointer value, I am trying to read the value in memory set by the pointer. But the first value I get I want to start the pointer from 0 to a specified location.
    I don't understand what you mean by this.

    It is good that you posted code and some sample output, but how exactly does your program not work?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You are calculating results before you know what they are: the value of ptr1 depends on ptr2, but it is calculated before the value of ptr2 is known.

  4. #4
    Registered User
    Join Date
    Sep 2009
    Posts
    48
    This doesn't seem to make any sense. What are you doing with all those pointers, and what are their types? Where and to what have they been initialised? Why are you using the read position returned by tellg() as a size value?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with fstream, one variabile for write and read
    By Smjert in forum C++ Programming
    Replies: 3
    Last Post: 02-03-2009, 09:19 PM
  2. Read and write binary file?
    By Loic in forum C++ Programming
    Replies: 2
    Last Post: 10-29-2008, 05:31 PM
  3. Copy/Read/Write a binary file(.mp3)
    By swapnaoe in forum C Programming
    Replies: 9
    Last Post: 04-22-2008, 02:38 AM
  4. Replies: 16
    Last Post: 07-12-2005, 08:22 AM
  5. File Encryption & Read/Write in Binary Mode
    By kuphryn in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 05:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21