Am I doing thid completely wrong? Reading data from Data file into Binary and back out.

closed account (4G3bpfjN)
I am very new to c++ and I have recently come upon a task dealing with IO. I am currently wondering I have begun doing this completely the wrong way.

Problem:
Write a program that will read records from a
file (create your data file by using data from sample below)
and store them in a binary file. That file will then be used
as input to create an output file of the information. The
data file contains employee information consisting of name,
social security, department ID, years employed, and salary.
In addition to displaying the information of each record, the
program will also calculate the average salary and years employed
of all the records. This additional information is stored in the
same output file.

My Solution:
#include <iostream>
#include <fstream>
#include <ostream>
using namespace std;

void averageSalary();
void yearsEmployed();

int main()
{
string name;
long int socialsecurity, depID, yearsEmployed, salary;




fstream input("employeedata.txt", ios::in);
while(!input.eof()){
input >> name >> socialsecurity >> depID >> yearsEmployed >> salary;
}
input.close();

input.open("employee.txt", ios::out|ios::binary);
while(!input.eof()){
input << name << socialsecurity << depID << yearsEmployed << salary;
}
input.close();

input.open("newfile2.txt", ios::in);

while(!input.eof()){
input >> name >> socialsecurity >> depID >> yearsEmployed >> salary;
}
input.close();



return 0;
}
Last edited on
Normally when dealing with a binary file you would be using the read()/write() istream/ostream member functions not the insertion and extraction operators.

Please use code tags when posting code.


Topic archived. No new replies allowed.