Creating and writing to a file (Ofstream)

Write your question here.

it a very simple question

Turn out, im not very good with doing stuff like this for exporting data to a txt file, if you havent done it in a while you'll forget (which is my case). So what im asking is, when I run the program, where should I Code in my OFstream to store the data for my FCFS & MLQF? Im trying to store all the data after it run. CODE is correct and run perfectly. So you can copy and paste in your visual studio. Thank you

PS: could not put in the whole code in, but was able to put in the important stuff down 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
  #include <iostream>
#include <fstream>
using namespace std;

class pnode
{
public:
    int c;
    pnode *pnext;
};

class PROCESS
{
public:
    PROCESS(); //constructor
    //~PROCESS(); //destructor
    void setProcess(int data[], int);
    void AdjustTime();
    pnode *time;
    PROCESS *next;
    int process_number;
    int current_burst;
    int IOtime;
    int responsetime;
    int waitingtime;
    int turnaroundtime;
};

class QUEUE : public PROCESS
{
public:
    QUEUE();
    //  ~QUEUE();
    void currentprocess(PROCESS *);
    void IOprocess(PROCESS *, int);
    void Dequeue(PROCESS *);
    void run(int&, int&, QUEUE *);
    void calculation(int&, int&);
    void display(int, QUEUE *);
    bool currentIsEmpty();
    bool waitingIsEmpty();
private:
    PROCESS *first;
};


PROCESS::PROCESS()
{
    process_number = current_burst = IOtime = 0;
    waitingtime = turnaroundtime = 0;
    responsetime = -1;
}


void PROCESS::setProcess(int data[], int num)
{
    process_number = num;
    pnode *ptr = new pnode;
    time = ptr;
    current_burst = data[0];
    for (int i = 0; i < 21; i++)
    {
        ptr->c = data[i];
        if (data[i + 1] == 0)
        {
            ptr->pnext = NULL;
            return;
        }
        ptr->pnext = new pnode;
        ptr = ptr->pnext;
    }

}


void QUEUE::calculation(int &time, int &idle)
{
	double avgresponsetime = 0.0, avgwaitingtime = 0.0, avgTurnaroundTime = 0.0, util = 0.0;
	PROCESS *temp = first;
	cout << "\nProcess\t\tResponse Time\tWaiting Time\tTurnaround Time";

	for (int i = 0; i < 8; i++)
	{
		avgresponsetime += temp->responsetime;
		avgwaitingtime += temp->waitingtime;
		avgTurnaroundTime += temp->turnaroundtime;
		cout << "\nP" << temp->process_number << "\t\t" << temp->responsetime << "\t\t" << temp->waitingtime << "\t\t" << temp->turnaroundtime;
		temp = temp->next;
	}
	avgwaitingtime /= 8;
	avgresponsetime /= 8;
	avgTurnaroundTime /= 8;
	util = (1 - ((double)idle / (double)time))*100.0;
	cout << "\n\nTotal time: " << time;
	cout << "\nAverage response time: " << avgresponsetime;
	cout << "\nAverage turnaround time: " << avgTurnaroundTime;
	cout << "\nAverage waiting time: " << avgwaitingtime;

	cout << "\nCPU Utilization: " << util << "%\n\n";


}

int main(int argc, const char * argv[]) {
	PROCESS p1, p2, p3, p4, p5, p6, p7, p8;
	QUEUE readyQueue, waitingQueue;
	int timer = 0;
	int idle = 0;
	int stop = 100;
	bool end = false;

	int data1[14] = { 6, 17, 8, 19, 12, 31, 11, 18, 9, 22, 8, 26, 10, 0 };
	p1.setProcess(data1, 1);
	readyQueue.currentprocess(&p1);

	int data2[18] = { 19, 38, 11, 24, 15, 21, 12, 27, 12, 34, 11, 34, 9, 29, 9, 31, 7, 0 };
	p2.setProcess(data2, 2);
	readyQueue.currentprocess(&p2);

	int data3[18] = { 3, 37, 14, 41, 8, 30, 4, 19, 7, 33, 5, 18, 4, 26, 5, 31, 16, 0 };
	p3.setProcess(data3, 3);
	readyQueue.currentprocess(&p3);

	int data4[16] = { 15, 35, 14, 41, 16, 45, 18, 51, 14, 61, 13, 54, 16, 61, 15, 0 };
	p4.setProcess(data4, 4);
	readyQueue.currentprocess(&p4);

	int data5[18] = { 9, 24, 7, 21, 15, 31, 6, 26, 7, 31, 3, 18, 6, 21, 6, 33, 3, 0 };
	p5.setProcess(data5, 5);
	readyQueue.currentprocess(&p5);

	int data6[18] = { 4, 38, 3, 41, 5, 29, 4, 26, 7, 32, 4, 22, 3, 26, 5, 22, 8, 0 };
	p6.setProcess(data6, 6);
	readyQueue.currentprocess(&p6);

	int data7[16] = { 14, 36, 17, 31, 16, 32, 15, 41, 14, 42, 17, 39, 16, 33, 15, 0 };
	p7.setProcess(data7, 7);
	readyQueue.currentprocess(&p7);

	int data8[18] = { 5, 14, 4, 33, 6, 31, 4, 31, 6, 27, 5, 21, 4, 19, 6, 11, 6, 0 };
	p8.setProcess(data8, 8);
	readyQueue.currentprocess(&p8);

	while (end == false)
	{
		readyQueue.run(timer, idle, &waitingQueue);

		if (readyQueue.currentIsEmpty() && waitingQueue.waitingIsEmpty())
		{
			end = true;
		}
	}


	cout << "\n\t\t    Processes are complete\n\n";



	waitingQueue.calculation(timer, idle);

	system("pause");
	return 0;
	
}
Last edited on
Your ending code tag is broken. It should contain a backslash.
Topic archived. No new replies allowed.