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.
#include <fstream>
#include "cs.h"
usingnamespace 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");
}
#include <fstream>
#include "cs.h"
usingnamespace 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";
}
}
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.
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]'