my code is supposed to read in a binary file and then save the input to a text file. when i cout << coach i get the output and jibberish

#include <iostream>
#include <string>
#include <cctype>
#include <fstream>
#include <cstdlib>
using namespace std;

int main()
	char coach[500];
	int i=0;
	fstream textFile("coaches.txt",ios::out),
		dataFile("binaryFile.dat",ios::in | ios::binary);
	if(dataFile.fail() || textFile.fail())
		cout << "failed to open! you lose" << endl;
		dataFile.read(reinterpret_cast<char *>(&coach),sizeof(coach));
			textFile << coach[i];
	return 0;

Edited by jigglymig1: n/a

8 Years
Discussion Span
Last Post by WaltP

you do a single read of binaryFile.dat using the read() method called on the fstream called dataFile. Whether the data read in is actually the size of coach or not may be a problem. Whether the read actually found, or better yet tried to read beyond, EOF or not may be a problem. I suspect the most likely problem is the single read didn't try to read beyond EOF so eof() will always return false, meaning that you will end up reading of the end of coach into who knows what in that while loop, but I haven't run the code to see what actually happens.


that data in the binary file is from a struct which contains spaces, chars and ints. Here is the data that is in each struct containing name, school, salary, and year. .... i need help reading in a structured binary file

Bob Stoops Oklahoma University $1,975,000.00
Woody Hayes Ohio State University $400,000.00
Tom Osborne Nebraska $1,000,000.00
Mack Brown University of Texas $1,450,000.00
Joe Paterno Penn State $1,000,000.00
Bear Bryant Texas A&M University $350,000.00
Mike Leach Texas Tech Univ. $1,000,000.00
Les Miles OSU $675,000.00


To the computer, and to the most basic, behind the scenes part of the program, all data is binary. The program can take binary information from the file and change it into readable form using intake functions like >> or get() or getline() or read(), etc. Unless you are required to do so you need not necessarily need to explicitly use binary mode and read/write to do this. Different options would include:

Option 1: read in one line at a time
char coach[80];
while(fin.getline(coach, 80))
{ //do something with the information read in}

Option 2: If you are breaking the information in each line into separate pieces of data and storing them in member variables of an object then maybe something like this would work:

struct Coach
    char firstName[81];
    char lastName[81];
    char university[81];
    char salary[81];
Coach coach;
while(fin.getline(coach.firstName, 80,  ' ')
   fin.getline(coach.lastName, 80, ' ');
   fin.getline(coach.universtiy, 80, '$');
   length = strlen(coach.university);
   coach.university[length - 1] = '\0';//drop the space behind university and before '$'
  strcopy(coach.salary, "$"); //add dollar sign back to salary
   fin.getline(temp, 80);
   strcat(coach.salary, temp);
   //do something with coach

Both of these options could be modified to use STL strings instead of C style strings if desired.

Option 3: You might be able to read all the data into individual variables using read(), but it is not something I've tried before given that the variable size of the strings making up each object makes the size of each object of different as well.

Note: If you are writing the data to the file, then I would consider using a semicolon (or some other char not used in any field of the data) delimited file to make it easier to identify given fields.

Woody;Hayes;Ohio State University;$400,000.00

That would be a snap to read.

Edited by Lerner: n/a


The problem I see is:

dataFile.read(reinterpret_cast<char *>(&coach),sizeof(coach));
	// you just read 1 record

	while(!dataFile.eof())    // Start a loop until you reach end 
	{                         // of file.  Output every character 
	                          // from the start of 'coach' to the 
	                          // end of memory since you never 
	                          // read another thing from the 
	                          // file.  EOF is never seen.
		textFile << coach[i];
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.