I am trying to make a small program which will write to a binary file,this is my code:

// program that writes to a binary file

#include <iostream>
#include <fstream>

using namespace std;

main() {

    fstream* bfile = new fstream("file.bin", fstream::out | fstream::binary | fstream::ate);

    int a = 2;
    int var = 0;


    bfile->write((char*)&a, sizeof(int));

    bfile->read((char*)&var, sizeof(int));

    cout << "Variable written to file contained" << var;

}

What i am trying to do is write to the binary file,and then check the files contents by printing out what is in the file,but i cant seem to grasp what i need to do in the next step,does anyone have any advice?

Thank you

You can use stringstreams e.g let us say the file to be read is rr.txt,

#include<iostream>
#include<fstream>
#include<sstream>
using namespace std;
int main()
{
//Assuming you have written to rr.txt
ifstream fin("rr.txt");
ostringstream sout;
sout<<fin.rdbuf();//Contents of file are now in stringstream
string text= sout.str()//stringstream contents are now in text
cout<<text<<endl;
return 0;
}

Please If this helped, mark this thread as solved and add to my reputation points..Thanks!!

"Show me your code and I will tell you who you are.."-Tkud

Ok you have a number of problems:
So let me (a) post some working code: (b) tell you what each line does:

int main() 
{
  std::fstream bfile("file.bin",
           std::ios::in|std::ios::out|std::ios::binary); 
  
  int a = 2;
  int var(0);
	      
  bfile.write(reinterpret_cast<char*>(&a), sizeof(int));
  
  bfile.seekg(0,std::ios::beg);
  bfile.read(reintepret_cast<char*>(&var), sizeof(int));

  std::cout << "Variable:" << var<<std::endl;

}

Note that I have (a) added an int to main.
(b) used reinterpret_cast< > instead of the c cast [good practice -- not essentual.] -- Many people use a union for this.
(c) Added a go to the beginning of the file.
(d) Add a std::endl to the output stream otherwize you might not get any output if you have more code below and it say waits or something.

I have the same problem, and I need some input on it, I'll keep everything brief and well organized:

I'm creating records with structures, this is the structure definition:

const int NAME_SIZE = 6;

struct Company
{
	char name[NAME_SIZE];
	int quarter[4];
	double quarterSales[4];
	
};

and these are the created structures:

Company northDivision;
Company eastDivision;
Company southDivision;
Company westDivision;

I have populated these structures with data, and written that data to a file that is opened for output in binary mode:

File.open("CompanySales.dat", ios::out | ios::binary);
	//write north division data to file
	File.write(reinterpret_cast<char *>(&northDivision), sizeof(northDivision));

	//write east division data to file
	File.write(reinterpret_cast<char *>(&eastDivision), sizeof(eastDivision));

	//write south division data to file
	File.write(reinterpret_cast<char *>(&southDivision), sizeof(southDivision));

	//write west division data to file
	File.write(reinterpret_cast<char *>(&westDivision), sizeof(westDivision));

	File.close();

The file is created and there is data in it (about 224 bytes or so)
and then I open the file for input in binary mode as well as create a new Company structure to hold the data being read into memory from the file:

Company division; // use for displaying file

File.read(reinterpret_cast<char *>(&division), sizeof(division));

Now here is where things went wrong, when the program encounters this while loop, the program just hangs, it doesn't fail, it never even reaches the end of the file, it just sits there:

//while not at end of file, display records
	while(!File.eof());
	{
		//Display the record.
		cout << division.name << " Division" << endl;
		cout << "Sales for Quarter " << division.quarter[0] << ":";
		cout << "$" << division.quarterSales[0] << endl;
		cout << "Sales for Quarter " << division.quarter[1] << ":";
		cout << "$" << division.quarterSales[1] << endl;
		cout << "Sales for Quarter " << division.quarter[2] << ":";
		cout << "$" << division.quarterSales[2] << endl;
		cout << "Sales for Quarter " << division.quarter[3] << ":";
		cout << "$" << division.quarterSales[3] << endl;

		cout << endl << endl;
		//read the next record
		File.read(reinterpret_cast<char *>(&division), sizeof(division));
	}

All this code is built off of examples from my programming book and I can't seem to get any more info from it, what mistakes have I made? Any help would mean a world of thanks from me.

Thanks in advance for any help anyone can offer.

This article has been dead for over six months. Start a new discussion instead.