ofstream not written to file

hi guys I have a problem which is confusing me so I seen someones challenge on the forum and decided to see if I could help and try it myself anyway I have come into a problem

my writeToFile function doesn't seem to write anything to my file,I have declared my ofstream object as a global so it can be accessed from multiple functions and I have even tried adding flush to the end of my outputs thinking maybe I had to flush the buffer but to no avail still nothing,

anybody know why this is the case??


thanks
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
  #include <iostream>
#include <fstream>
#include <sstream>
using namespace std;

class Employee {

public:

	string name;
	int ID;
	int daysMissed;

	Employee(string name, int ID) :
			name(name), ID(ID), daysMissed(0) {
	}

	Employee() {

		name = " ";
		ID = -1;
		daysMissed = 0;
	}

	Employee(const Employee& other) {

		name = other.name;
		ID = other.ID;
		daysMissed = other.daysMissed;
	}

	Employee& operator=(const Employee& other) {

		name = other.name;
		ID = other.ID;
		daysMissed = other.daysMissed;
	}

};

string fileName = "missed.txt";
ofstream missed(fileName);


bool openFile(){

    missed.open(fileName,ofstream::app);

    if(!missed.is_open()){

    	cout << "could not open the file" << endl;
    	return false;
    }

    return true;
}

void writeToFile(){

	cout << "enter employee name and ID" << endl;
	string name;
	string ID;
	string missedDays;
	cin >> name >> ID;
	cout << "enter days employee has missed" << endl;
	cin >> missedDays;

	missed << name << flush;
	missed << ":" << flush;
	missed << ID << flush;
	missed << ":" << flush;
	missed << missedDays << flush;
	missed << "\n" << flush;

}

int main() {

   openFile();
   writeToFile();
}



also tried closing the stream but still nothing
Last edited on
A couple of problems:

1. The file is opened twice without it being closed in between.
line 42 declares and opens the file.
line 47 tries to open it again, but since it is already open, this fails.

2. The fail status isn't checked.

Line 49
 
    if(!missed.is_open()){


try changing it to
 
    if ( !missed ) {

and the diagnostic message should be printed:
could not open the file


Now you have the program reporting the problem, change line 42 to
 
ofstream missed;

and the program should work.

Also a suggestion, not a cause of any great problem here, global variables are usually best avoided. You could do that by declaring the ofstream and filename at the start of main() and then pass the ofstream by reference to the other functions.
Last edited on
very good point chevril I will change my code to do that =)

thanks for the help I also have run into another problem with the following block of code,

I added a get info() function to get the peoples info but it seems to be stuck in an infinite loop that keeps printing id and daysMissed and also for some reason to my confusement does not print the first variable name

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

bool getInfo(){

	cout << "in this block " << endl;

	ifstream in;
	string fileName = "missed.txt";
	stringstream ss;

	in.open(fileName);

	if(!in.is_open()){

		cout << "not open" << endl;
		return false;
	}
	string name;
	string id;
	string daysMissed;

	while(missed){

	getline(in,name,':');
	ss << name;
	getline(in,id,':');
	ss << id;
	getline(in,daysMissed);
	ss << daysMissed;


	cout << name << " " << id << " " << daysMissed << endl;

	}

	in.close();

	cout << ss.str();
}


I did not expect this I used getline with the delimiter ':' to get up to the : character then it gets thrown away then I said getline() again with : as the delimiter so it reads up to ':' and throws away the : then I say getline again with no delimiter so I would expect that getline to be to consume the next line but I seem to have to add cin.get(),

when I add cin.get()

it seems to work but not in the way I want or expect it to it prints the first name of the textfile then when end the program thats WHEN it prints out all the names of the file but it doesn't stop there it then prints ID and missedDays a couple more times before ending

here is the output before I terminate the program for some reason the program doesn't terminate by itself which it SHOULD but instead prompts me for infinite input

enter employee name and ID
bill 9 3
enter days employee has missed
enter employee name and ID
eva 8 2
enter days employee has missed
in this block
jeff 6 1

then when I terminate the program forcefully this is the output

in this block
jeff 6 1
steve 6 2
jim 66 6
brock 88 3
sarah 14 4
lisa 88 4
brett 7 5
matt 6 2
darren 4 5
conor 8 2
tima 5 5
jessica 4 4
bill 9 3
eva 8 2
8 2
8 2
8 2
8 2
8 2
8 2
8 2
8 2
8 2
8 2
8 2
8 2
8 2
8 2
8 2
8 2
8 2
8 2
8 2
8 2
8 2
8 2
8 2
8 2
8 2
8 2
8 2
8 2
8 2
8 2
8 2
8 2
8 2
8 2
8 2
8 2
8 2
8 2
8 2
8 2
8 2
8 2
8 2
8 2
8 2
8 2
8 2
8 2
8 2
8 2
8 2
8 2
8 2
8 2
8 2
8 2
8 2
8 2
8 2
8 2
8 2
8 2
8 2
8 2

thanks
On line 42 you have global object that opens the file before main() starts. Not a good idea. Better keep it local an pass a reference.

On line 47 you reopen the file. Since you do not provide a path the question is where. Somewhere near the exe or project file. You might search for it.
Looks like those global variables have bitten you.

In function getInfo(), line 21
 
    while(missed){
should be using in, not missed

In addition, as I seem to repeat (to different people) very frequently:

you need to check the file status after reading it, not before.


That loop might be better written like this:
1
2
3
4
5
6
7
	while (  getline(in,name,':') && getline(in,id,':') && getline(in, daysMissed)  )
	{
	    ss << name;
	    ss << id;	    
	    ss << daysMissed;
	    cout << name << " " << id << " " << daysMissed << endl;
	}

thanks guys that was indeed my problem changed it to in I'm just wondering

and still this happens,

why does my program still only prints all the names and basically all the info once I forcibly end my program,

also it still prints the first name out and waits for my input which is strange and that input seems to be infinite
also my string stream doesn't work in the way I expected it to

it seems to combine both numbers from my integers lets say 5 and 5 = 55 and prints this yet it doesn't print eName just the two integers but prints them as a combined number

I wanted the variables to be separated

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
50
51
52
53
54
55
56
57
58
59
60

bool getInfo(){

	cout << "in this block " << endl;

	ifstream in;
	string fileName = "missed.txt";
	stringstream ss;

	in.open(fileName);

	if(!in.is_open()){

		cout << "not open" << endl;
		return false;
	}
	string name;
	string id;
	string daysMissed;

	while(in){

	getline(in,name,':');
	ss << name;
	ss << " ";
	getline(in,id,':');
	ss << id;
	ss << " ";
	getline(in,daysMissed);
	ss << daysMissed;
	ss << " ";

	if(!in){

		break;
	}


	cout << name << " " << id << " " << daysMissed << endl;

	}

	in.close();

	cout << ss.str();
	cout << endl;

	string eName;
	int number;
	int days;

	while(ss >> eName >> number >> days){

		cout << "string stream : " << name << number << days << endl;

	}

	return true;
}









nter employee name and ID
marv 6 3
enter days employee has missed
enter employee name and ID
harry 2 1
enter days employee has missed
in this block 
brad 6 6
tadd 5 5
steve 4 1
bryan 3 3
jack 3 3
jordan 3 2
hu 33 3
jacky 3 3
tom 9 9
rob 2 2
marv 6 3
harry 2 1
brad 6 6 tadd 5 5 steve 4 1 bryan 3 3 jack 3 3 jordan 3 2 hu 33 3 jacky 3 3 tom 9 9 rob 2 2 marv 6 3 harry 2 1  2 1 
string stream : 66
string stream : 55
string stream : 41
string stream : 33
string stream : 33
string stream : 32
string stream : 333
string stream : 33
string stream : 99
string stream : 22
string stream : 63
string stream : 21






thanks
my bad ignore the last post it was working correctly and doing what I wanted it to do the problem was that I mixed up the variable names I have been programming for going on 7 hours now today so yeah mistakes are creeping in with stress haha

thanks guys
OK - I understand it has been a long day. maybe best to ignore this for now and maybe have a look another day.

it was working correctly and doing what I wanted it to do

Actually it wasn't.

Did you not stop to ask yourself why this line:
brad 6 6 tadd 5 5 steve 4 1 bryan 3 3 jack 3 3 jordan 3 2 hu 33 3 jacky 3 3 tom 9 9 rob 2 2 marv 6 3 harry 2 1  2 1 


... has the number " 2 1 2 1" repeated at the end of the line?

At the risk of repeating myself and alienating people, you need to check the file (stream) status after reading from it.
I previously showed one way to do that. Here's another:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
    while (in)
    {
        if ( getline(in,name,':') )
            ss << name << " ";
            
        if ( getline(in,id,':') )
            ss << id << " ";
            
        if ( getline(in,daysMissed) )
            ss << daysMissed << " ";
            
        if (in) 
            cout << name << " " << id << " " << daysMissed << endl;
    }


thanks Chevril =)

I fixed it up
Topic archived. No new replies allowed.