File Reading and Writing

  • Thread starter idle_09
  • Start date
Hi! Thanks for helping me. I need to write a program (in C++) that reads the file Employee.dat as input and then outputs to 4 different files which are Managers.dat, Marketing.dat, Developers.dat, and Testers.dat. Each of the output files contains the name of the employees of its category.

The fle Employee.dat contains following data:

Sue Leon 4
Robert Wise 3
Sam Woo 1
Nathan White 3
Suzan Head 2
Henry Williams 4
Christine Mint 1
Kim Leeds 4
Elton Sue 3
Ken Latch 2
Mentor note: The number associated with each employee is apparently his or her department. The enumeration in the code shows this.

I tried to write the program but I don't know what to put in "SWITCH" loop. Here is what i have so far.
C:
#include <iostrean>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;

int main()
{
    ifstream in_stream;
    ofstream out_stream;

    in_stream.open("Employee.dat");
    if (in_stream.fail())
    {
        cout << "Input file failed to open.\n";
        exit(1);
    }

    out_stream.open(("Managers.dat", ios::app), ("Marketing.dat", ios::app), ("Developers.dat", ios::app), ("Testers.dat", ios::app);
    if (out_stream.fail())
    {
        cout << "Output file failed to open.\n";
        exit(1);
    }

    string first_name, last_name;
    enum department { Managers = 1, Marketing = 2, Developers = 3, Testers = 4 };

    im_stream >> first_name >> last_name >> department;
    while (!in_stream.eof())
    {
        switch (department)
        {
            number '1';
            out_stream << "
 
Last edited by a moderator:
32,364
4,146
The snippet in the OP is a fair start, but the main problems are that 1) you need a file stream for each of the five files the program is dealing with, and 2) each file stream must be opened with a separate call to the open() function.

Here's my solution. I don't have any comments, but the code should be pretty much self-explanatory. The basic algorithm is to iterate through the input file, and send output to the appropriate output file, based on the department the current employee belongs to.
C:
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>

enum department { Managers = 1, Marketing = 2, Developers = 3, Testers = 4 };

int main()
{
    std::ifstream in_stream;
    std::ofstream out_streamMgr, out_streamMkt, out_streamDev, out_streamTest;

    in_stream.open("Employees.dat");
    if (in_stream.fail())
    {
        std::cout << "Input file failed to open.\n";
        exit(1);
    }

    out_streamMgr.open("Managers.dat");
    out_streamMkt.open("Marketing.dat");
    out_streamDev.open("Developers.dat");
    out_streamTest.open("Testers.dat");

    if (out_streamMgr.fail() || out_streamMkt.fail() ||
        out_streamDev.fail() || out_streamTest.fail())
    {
        std::cout << "Output file failed to open.\n";
        exit(2);
    }

    std::string first_name, last_name;
    int deptVal;

    while (!in_stream.eof())
    {
        in_stream >> first_name >> last_name >> deptVal;
        switch (deptVal)
        {
        case Managers: out_streamMgr << first_name << ' ' << last_name << ' ' << Managers << std::endl;
            break;
        case Marketing: out_streamMkt << first_name << ' ' << last_name << ' ' << Marketing << std::endl;
            break;
        case Developers: out_streamDev << first_name << ' ' << last_name << ' ' << Developers << std::endl;
            break;
        case Testers: out_streamTest << first_name << ' ' << last_name << ' ' << Testers << std::endl;
            break;
        default: std::cout << "Invalid department";
            break;
        }
    }
    in_stream.close();

    out_streamMgr.close();
    out_streamMkt.close();
    out_streamDev.close();
    out_streamTest.close();
}
 

pasmith

Homework Helper
1,668
369
Might be helpful to tell the user which output file(s) failed to open.

I would naturally parse each line as a single string followed by an integer, since otherwise the logic fails when you need to distinguish between John Adam Doe and John Brian Doe or Anna Maria von Bonn joins the organisation.
 

Want to reply to this thread?

"File Reading and Writing" You must log in or register to reply here.

Physics Forums Values

We Value Quality
• Topics based on mainstream science
• Proper English grammar and spelling
We Value Civility
• Positive and compassionate attitudes
• Patience while debating
We Value Productivity
• Disciplined to remain on-topic
• Recognition of own weaknesses
• Solo and co-op problem solving
Top