help reading from a text file.

hello im trying to read from a text file. but does not open the file and does the else statement? does the location of the file matter? or is the format wrong?
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
 /[code]void readFromFile()
{
	int pIDIndex = 1;

	int totalRunTimeTemp = 0;
	//Task tempTask;
	ifstream queueFile ("queue.txt");

	//queueFile.open("queue.txt");

	while (!queueFile.eof())

	{
		if (queueFile.is_open())
		{

			Task tempTask;
			tempTask.pID = pIDIndex;
			pIDIndex++;

			queueFile >> tempTask.initialPriority;
			queueFile >> tempTask.totalRunTime;

			tempTask.currentPriority = tempTask.initialPriority;
			tempTask.remainingRunTime = tempTask.totalRunTime;

			PriorityQueues[tempTask.initialPriority].push(tempTask);
			cout << " it works" << endl;
		}
		else
			cout << "plz check file" << endl;

	}

	queueFile.close();

}
Last edited on
You have the queueFile.open command commented out. That's why the file is never opened.
You should check if the file is opened.

1
2
3
4
5
6
7
8
9
void readFromFile()
{
   ifstream queueFile ("queue.txt");
   if(!queueFile)
   {
       perror("readFromFile ");
       return;
   }
}


Also don't use !queueFile.eof(). iT does not work as expected.
Use this instead.
1
2
3
4
5
   Task tempTask;
   while(queueFile >> tempTask.initialPriority >>  tempTask.totalRunTime)
   {
       // your logic here
   }
kurisutofaa lve tried that and thomas yea the file wont open.
What was the error message?
thomas it was a dumb mistake. when the program ran with would recreate a txt file and i would just rename it, instead of making a new one.
Topic archived. No new replies allowed.