Output file has no information

"Write a program that will create userids for email addresses of a company. Names of employees will be contained in a file. Your task is to read the file character by character."

- input file: "employee.dat" has the following names = SS van der Merwe;PJ Ferreira;HW du Plessis;DF Kodisang;AA Papoudopolous;G Mhlanga;TRF Schoeman;LJ Marais-Le Roux;CG Roux;B Nicholaidis;TT Tshabalala;RV Mississipi;

When the user enters any of those names, the output should be a user ID, for example: SS van der Merwe = ssvandme.

My program is able to ask for a name but it just doesn't save anything to the output file, userid.dat. Please help.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <iomanip>

int main()
{
    using namespace std;
    ifstream in_stream;
    ofstream out_stream;

    in_stream.open("employee.dat");
    out_stream.open("userid.dat");

    if (in_stream.fail( ))
    {
        cout << "Input file opening failed.\n";
        exit(1);
    }

    if (out_stream.fail( ))
    {
        cout << "Output file opening failed.\n";
        exit(1);
    }

    char name [9];

    cout << "Enter your name: ";
    cin.getline(name, 9);

    ifstream infile ("employee.dat");
    infile.open(name);
    std::cout << std::setw(0);
    ofstream outfile ("userid.dat");
    in_stream.close( );
    out_stream.close( );
    return 0;
}
Last edited on
@StrangerThings

You open a file, but you never try writing to it. Here is a small snippet of a program I wrote, that saves data to a file. Use it to make changes to your code.

1
2
3
4
5
6
7
8
ofstream SaveFile;
SaveFile.open("Mystery Mania Clues.txt");

SaveFile << "Mystery Mania Clues" << endl << endl;
for (int x = 0; x < 8; x++)
	SaveFile << s$[x] << endl;

SaveFile.close();

Hello StrangerThings,


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button.

You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.



As whitenite1 has pointed out you have opened two input files, but never read anything from them and one output file that you never wright to.


Here is you code in code tags with some changes and comments.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <iomanip>
#include <string>
//#include <cstdlib>
#include <fstream>

int main()
{
	using namespace std;

	ifstream in_stream("employee.dat");
	ofstream out_stream("userid.dat");

	//in_stream.open("employee.dat");
	//out_stream.open("userid.dat");

	if (!in_stream)
	{
		cout << "Input file opening failed.\n";
		return 1;
	}

	if (!out_stream)
	{
		cout << "Output file opening failed.\n";
		return 2;  // use exit if not in main.
	}

	char name[9]; // <--- Is this big enough?

	cout << "Enter your name: ";
	cin.getline(name, 9);

	//ifstream infile("employee.dat"); // <--- Already open.
	//ifstream infile(); // <--- Did you mean this?

	ifstream infile(name); // <--- Why a new input file?

	// <--- Are you sure the file is open? Where is your check?

	std::cout << std::setw(0);

	//ofstream outfile("userid.dat"); // <--- Already open.

	in_stream.close();
	out_stream.close();

	return 0;
}


Andy
Andy:
You really should try to avoid the C function exit(), it doesn't know about C++ classes and can cause data loss if used inappropriately. Since this is C++ start thinking about using exceptions when you need to "abort" the program.

You should also start thinking about using RAII techniques, which in this case, means letting the destruction of objects be done automatically, the stream.close() functions should be rarely needed let the destructor do it's job when the instance goes out of scope.


@jlb,

Point taken thank you,

Andy
Registered users can post here. Sign in or register to post.