ofstream question

i have a main.cpp, function.cpp and a header.h , i want to use ofstream to send message to a txt. messages are located in the functions on the function.cpp. Where should i place the "oftsream variable;" that defines the variable of the fileout datatype to be shared between the main.cpp and the function.cpp since in the main file is where i have the open and close function for the file out text cant just define it in the function.cpp alone also i cant define it both in the function and main or i get an error.
Be more explicit. Put a code, for instance.
Main.cppp

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
#include <fstream>
#include "cs.h"
using namespace std;

ofstream outf;//where do i place this part so that the main.cpp and functions.cpp can use it 


int main ()

{
	outf.open("Results.txt");
	/*for (int i = 0;i < 500;i++ )
	{
	int random = rand() %8;

		switch (random)
		{
		case 0 : reader1();
				 break;
		case 1 : reader2();
				 break;
		case 2 : reader3();
				 break;
		case 3 : reader4();
				 break;
		case 4 : reader5();
				 break;
		case 5 : writer1();
				 break;
		case 6 : writer2();
				 break;
		case 7 : writer3();
				 break;
		}
	
	}*/
	test();

	outf.close();







return 0;
system("pause");

}




functions.cpp


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
82
83
84
85
86
87
88
89
90
91
#include <fstream>
#include "cs.h"

using namespace std;


ofstream outf; // where do i place this part so that the main.cpp and functions.cpp can use it 
	int mutex = 1;
	int wrt = 1;
	int readcount=0;
	int writecount=0;


void test()
{
outf << "@@@@@@@@@@@@@@@@@@@";
}

void reader1()
{
	outf << "Reader1 wants to access Critical Section";
	P(mutex);
	readcount ++;

	if(readcount ==0)
	{
		outf << "Reader1 is the first and only reader";
		P(wrt);
	}
	while(readcount>=3);
	outf<<"Reader1 is entering the Critical Section";
	V(mutex);
	// Crictal Section
	outf<<"Reader1 is inside the Critical Section";
	if(readcount>=1)
	{
		outf << "Reader1 sees "<<readcount<<"Readers inside the Critical Section";
	}
	if(readcount>=4)
	{
		outf << "PANIC:Reader1 sees four or more Readers";
	}

	if (writecount>=1)
	{
		outf << "PANIC:Reader1 sees a Writer";
	}
	
	//Crictal Section

	P(mutex);
	readcount --;
	if (readcount==0)
	{
		V(wrt);
		outf << "Room is empty after Reader1 leaves";
	}
	outf << "Reader1 is exitting the Critical Section";
	V(mutex);

}

void writer1()
{
	while (true )
	{
	outf <<" Writer1 wants to access the Critical Section";
	writecount++;
	P(wrt);
	outf << "Writer1 is entering the Critical Section";

	//Critical Section
	outf << "Reader1 is inside the Critical Section";
	if(readcount>=1)
	{
		outf << "PANIC:Writer1 found a Reader inside the Critical Section";
	}

	if(writecount>=2)
	{
		outf << "PANIC:Writer1 found an another Writer inside the Critical Section";
	}
	//Critical Section

	V(wrt);
	writecount--;
	outf << "Writer1 has left the Critical Section";

	}

}


header.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef CS_H
#define CS_H



	void reader1();
	void reader2();
	void reader3();
	void reader4();
	void reader5();
	void writer1();
	void writer2();
	void writer3();



#endif  


im concern about "ofstream outf;" where do i place it so that the main.cpp and functions.cpp is sharing it
What are you doing is a bit complicated. As far as I know, you must put that line with ofstream in main.cpp but also you have to include the #include<functions.cpp> so that the main function can see the functions declared in functions.cpp.
yea that doesnt work #include<functions.cpp>
Don't use #include on a .cpp file.

Instead leave outf in main.cpp and add

extern std::ofstream outf; // avoid using namespace std in headers

to your header, so the functions in functions.cpp can see the same ofstream instance.

Or, if you want to avoid using a global, you could declare outf in main() and pass the ostream to your test functions by reference.
Last edited on
i placed extern std::ofstream outf;
in the header file and i get these errors

1
2
3
4
5
error C2039: 'ofstream' : is not a member of 'std'

 error C2146: syntax error : missing ';' before identifier 'outf'
 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

and like one of these for each "<<" i used in the cpp
1
2
3
 error C2297: '<<' : illegal, right operand has type 'const char [20]'

#include <fstream> in the header ( I guess it is 'cs.h' )
that doesnt work ^
weird! ofstream, along with ifstream, definitely lives in <fstream> and the std namespace.
http://www.cplusplus.com/reference/iostream/

So if you've included <fstream> before the extern statement, it should be fine.
ok i just checked i missed a character in the header so now i get these errors


1
2
1>cs.obj : error LNK2001: unresolved external symbol "class std::basic_ofstream<char,struct std::char_traits<char> > outf" (?outf@@3V?$basic_ofstream@DU?$char_traits@D@std@@@std@@A)
1>main.obj : error LNK2019: unresolved external symbol "class std::basic_ofstream<char,struct std::char_traits<char> > outf" (?outf@@3V?$basic_ofstream@DU?$char_traits@D@std@@@std@@A) referenced in function _main
Last edited on
Do you still have

ofstream outf;

at the top of main (just below using namespace std;) ??
Last edited on
no i took out both from main.cpp
Topic archived. No new replies allowed.