Design a program which reads values from a text file in c++, store and parse the
ID: 3695353 • Letter: D
Question
Design a program which reads values from a text file in c++, store and parse these value into double data types.
This is only an simulation of an Operating System, where process can be moved from one state to the other. I have most of the classes designed but I'm stuck on how to handle file input. Each process has the following data associated in a text file document:
Arrival Time of Process, Memory needed to run, CPU time needed to complete process, No. of I/O Requests, At which time does the process request for I/O, and How long will it be in I/O.
The time at which the process enters Blocked state(after requesting for I/O) and how long it will be in I/O are dependent on the number of I/O requests.
If ia process requests for an I/O, then I will need to know when that process enters the I/O (blocked state) and how long it will be in I/O (blocked state) before moving on.
So, in total if the number of times a process requests for I/O is 4, then an array should hold values for time that process enter I/O (blocked state), 4 elements in total for this process, and another array which holds the values for how long the process will be in the I/O (blocked state), 4 elements in total for this process.
A restriction: The number of I/O requests a process can make has certain boundries. For this particular program the number of I/O requests must stay within 0 to 5 range.
The format of the file is as follows:
Arrival Time of Process, Memory needed to run, CPU time needed to complete process, No. of I/O Requests, At which time does the process request for I/O, and How long will it be in I/O.
This is an exampe of what the input file might look like:
3, 5, 50, 2, 10, 25, 5, 10
4, 10, 35, 3, 10, 20, 30, 10, 5, 6
5, 7, 25, 1, 5, 3
7, 9, 15, 4, 3, 6, 9, 1, 2, 4
10, 5, 45, 5, 5, 10, 15, 20, 30, 40, 5, 3, 4, 5, 10
To make this simpler to understand I'll use the last line of data in this example of the input file, let's call it Process 5.
10, 5, 45, 5, 5, 10, 15, 20, 30, 40, 5, 3, 4, 5, 10
So,
10 will be the arrival time of the process
5 will be the amount of memory required
45 will be the amount of time required in CPU to complete this process
5 will be the No. of I/O requests this process has made
10 15 20 30 40 will be the time of when the process will enter the I/O(blocked state)
5 3 4 5 10 will be the time spent in the I/O (blocked state)
Explanation / Answer
Try the below code, I think this may help you....
#include <fstream>
#include <iostream>
#include <sstream>
using namespace std;
int main ()
{
char data[100];
string line, token;
int i = 0, j=0;
// open a file in read mode.
ifstream infile;
infile.open("samplefile.dat");
cout << "Reading from the file" << endl;
infile >> data;
while(getline(data, line)) // loops till all the os processes are done
{
stringstream s(line); // fetches a single os process
i = 0;
j = 0;
string p_arr[15]; // holds entire process
string bs_arr[5]; // holds the time of when the process will enter the I/O(blocked state)
string iotime_arr[5]; // holds the time spent in the I/O (blocked state)
string io_requestnum = 0; // number of I/O requests...
while(getline(s, token, ',')) {
// save token to your array by value
p_arr[i]= token;
if(i == 3)
{
io_requestnum = token;
}
if(i >= 4)
{
if(io_requestnum > 0)
{
if( i <= io_requestnum+3 )
{
bs_arr[j] = token;
j++;
}
else
{
iotime_arr[j] = token;
j++;
}
}
}
i++;
}// while ends..
// ........ YOU CAN WORK YOUR CODE AS HERE THE p_arr HOLDS SINGLE PROCESS ...........
} // while ends..
// write the data at the screen.
cout << "completed..." << endl;
// close the opened file.
infile.close();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.