0

I am trying to read a file which contains the arrival time and burst time of 22 processes in every line.I am trying to store these values into an array of structs so I can update them everytime a process gets a CPU cycle.The read is not working successfully though.Help me figure out what I am missing. The file which is a text file looks like: 30 0.78 \n 54 17.28 \n 97 32.82 \n . . . .

#include<iostream>
#include<fstream>

using namespace std;        
const int process_cnt=22;

struct process{        
          int at;
          float bt;
          float rt;
};

process init_q[process_cnt],ready_q[process_cnt];        
void getData(ifstream& inData,process init_q[]);

int main(){        
    ifstream inData;        
    getData(inData,init_q);                
    cout<<"Test";        
    return 0;
}

void getData(ifstream& inData,process init_q[]){                        
    inData.open("input.txt");        
    while(inData){          
        inData>>init_q->at>>init_q->bt;
        cout<<init_q->at<<" "<<init_q->bt<<endl;//check if read was succesful           
    }
    inData.close();         
}
5
  • 1
    You need to show what the file looks like. Commented Nov 7, 2016 at 13:42
  • 1
    Is this a binary or text file? Are there new lines at the end? Have you tried this code, if so what happens? Commented Nov 7, 2016 at 13:49
  • 1
    You're using a somewhat unfortunate shortning of "number" to "no". Consider using process_cnt instead of no_process. It confused me (unnecessarily) for a couple of seconds. Commented Nov 7, 2016 at 13:53
  • 1
    If you are filling queues why don't you use appropriate datatypes? This code doesn't "feel" like C++ at all... Commented Nov 7, 2016 at 14:02
  • Simon Kraemer I am new to programming and I have been reading c++ just for the past 1 month.So any datatype recommendation, criticisms, corrections are absolutely acceptable.That will make me a better future developer. Commented Nov 7, 2016 at 14:20

1 Answer 1

2

Probably what you are missing is the index of current array entry:

void getData(ifstream& inData, process init_q[]) {
    inData.open("input.txt");
    int index = 0;
    while (inData) {
        process *entry = init_q[index++];
        inData >> entry->at >> entry->bt;
        cout << entry->at << " " << entry->bt << endl;//check if read was succesful
    }
    inData.close();
}

I skipped the part checking whether the current index is lower than your const 22.

Sign up to request clarification or add additional context in comments.

3 Comments

I am getting an error "cannot convert process to process in initialization" for the pointer though.Is that legit?
So, a new error message now you have changed the code? WHat exactly does the error say, and are you sure this isn't a new question?
I am still just trying to figure out how to store the values in each line of the text file to the arrival time and burst time of the init_q.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.