i have 3 files one main.cpp, functions.cpp, and a header.h
im using

#include<fstream>

in the main.cpp and in the functions.cpp but im not sure where to define the ofstream datatype in, so that the main.cpp and the function.cpp share the same ofstream variable.


where do i put this "ofstream outf;" between the 3 files


main.cpp

#include <fstream>

#include "cs.h"
using namespace std;

ofstream outf;

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

#include <fstream>
#include "cs.h"

using namespace std;



	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";

	}

}

void P( int a)//Wait
{
	while(a<=0);
	a--;
}

void V(int a)//Signal
{
	a++;
}



bool testandset(bool *target)
	
{
	bool rv = *target;
	*target = true;
	return rv;

}

header.h

#ifndef CS_H
#define CS_H



	void reader1();
	void reader2();
	void reader3();
	void reader4();
	void reader5();
	void writer1();
	void writer2();
	void writer3();
	void P(int);//wait
	void V(int);//signal
	bool testandset(bool);
	void test();

	


#endif

Edited 5 Years Ago by imgregduh: n/a

I suggest you dont use global variables, as they tend to be messy and you tend to run into problems like this one. Instead make your Reader/Writer functions take an ofstream variable and use it.

you mean something like this ?

reader1(ofstream outf);

and i have to use global variables for the experiment to work, the project is to have the processes clash with eachother when trying to access the same data


i tried using the this

reader1(ofstream outf);

but im getting errors in the header file when i put this

void reader1(ofstream );
error C2065: 'ofstream' : undeclared identifier
 error C2182: 'reader1' : illegal use of type 'void'

Edited 5 Years Ago by imgregduh: n/a

You have to realize that ofstream is a class so you need to use void reader1(ofstream outf); Also I think that fstream tries to protect your programs from clashing like you describe. If you want a stripped down access to a file with no C++ failsafes I suggest a small interrupt in assembly language.

ok i tried a different way and im getting errors
i removed the ofstream outf also the include for it from the main.cpp
placed the include and

extern std::ofstream outf;

in the header file and the functions and the main does read it from the header but when i compile i get these errors

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

You need to put

extern std::ofstream outf;

in a header file, and include that header file in any source file that needs access to outf. That just says that outf is of that type, and will actually be allocated -somewhere-. Then in -one- of the source files, you still need to specify:

std::ofstream outf;

in the global scope (outside of any function, otherwise it's a local variable of that function), e.g. as you previously had it in main.cpp

But whether outf is a global variable or passed into each function which needs it, is irrelevant to your "experiment". Unless you're using a specialized concurrent version of C++, your code as written will only execute one reader or writer, all the way through, and then another. The "processes" will never execute concurrently.

Go back to the assignment and understand how to implement a "process" as directed, so that it each time it is called, it executes only the next instruction and immediately returns, to approximate concurrency.

This article has been dead for over six months. Start a new discussion instead.