Reading Separate lines from a file (C)

In summary, the code is trying to read the number of processes, their arrival time, and then in the next line, their bursts. However, it's not working correctly. The author is trying to get the bursts but is not sure how to do it.f
  • #1
108
0
What I want to do is to to read the number of processes, their arrival time, and then in the next line, their bursts. Here's the file format:

P1 0
12 2 21 2 12 32 18
P2 9
13 17 3 21 45 67 21 2

(1st line) Process Arrivaltime
(2nd line) Bursts
(3rd line) Process Arrivaltime
(4th line) Bursts

and so on.

What I've done so far is to count the number of processes, and pick up their arrival times as integers. I've also calculated the number of bursts for each processes. For example, in the above two processes, the bursts for P1 are 7 and for P2 bursts are 8 (so far so, good). Now I want to read their bursts from the next line in another int array. The code so far is:

Code:
#include<stdio.h>
#include <malloc.h>
struct os
{
	char process ;
	int arrival_time;
	int *burst;
	int l;
};


int main()
{
	char a;
	int b=0;
	struct os fast[6];
	FILE *far;
	FILE *f;
	int m;
	char s[1000];
	int pro=0;
	far=fopen("data.txt","r");
/////////////////////////////This will read the entire file and see the number of processes///////////////////////////////

	while (fgets(s,1000,f)!=NULL)
	{
		if (s[m] == 'P')
		pro++;
	}
	printf ("Process are: %d ",pro);
	fclose(f);

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

////////////////////////// Open the file again and get the process and respective arrival time ////////////////////

	far=fopen ("data.txt","r");
	int i,j;
	for(i=0;i<pro;i++)
	{
		fast[i].l=0;
		fscanf(far,"%c%d",&fast[i].process,&fast[i].arrival_time);
		//printf("%c   ",fast[i].process);
		//fast[i].burst=malloc(fast[i].l*sizeof(int));          
		fscanf(far,"%c",&a);}	

//////////////////////////////// This would calculate the number of bursts in the line following each process/////////////
		while (a!='\n')
		{
			fscanf(far,"%c",&a);
		}
		fscanf(far,"%c",&a);
		while (a!='\n')
		{
			if (a>='0')
			{
				b++;
			}
			else if (a=' ')
			{
				b=0;
			}
			if (a!=' '&& b==1)
			{
				fast[i].l++;
			}
			fscanf(far,"%c",&a);
		}
		printf("%d ",fast[i].l);
		fast[i].burst=malloc(fast[i].l*sizeof(int));  // dynamically allocate the int bursts array for each process
	}
	fclose(far);
}

Now, I can't figure how to get the bursts in this array. I've tried a couple of things but they're not working good. What should I do?
 
  • #2
I've tried a bit more and I think I'm near. After this ending piece of code:

Code:
fscanf(far,"%c",&a);
		}
		printf("%d ",fast[i].l);
		fast[i].burst=malloc(fast[i].l*sizeof(int));  // dynamically allocate the int bursts array for each process
	}
	fclose(far);

I've tried this one to get the bursts:

Code:
FILE *far2;
int q, zo;
char on;
far2=fopen("data.txt", "r");
for (q=0; q<pro; q++)
{
      fscanf (far2, "%c", &on);        // to get the P from first line, I don't need it now
}
while (on != '\n')
{
      fscanf (far2, "%c", &on);            // get all the characters following P. Don't need chars from this line.
}
fscanf (far2, "%c", &on);                   // To move to the next line
for (zo=0; zo<7; zo++)                    // 7 is just random. will change it later
{
      fscanf (far2,"%d", &fast[q].burst[zo]);             // to get the bursts in this line
      printf ("%d", fast[q].burst[zo]);
}
}

For this file:
P1 0
12 2 21 2 12 32 18
P2 9
13 17 3 21 45 67 21 2
P3 34
12 3 43 23 3 43 23

It generates the following output:
2 2 21 2 12 32 18
2 9 3 17 3 21 45
3 34 12 3 43 24 3

While, the expected output is:

12 2 21 2 12 32 18
3 17 3 21 45 67 21
12 3 43 24 3 43 23

there's a bit mistake somewhere and I'm not yet able to figure it out.
 
  • #3
There might be some in-efficiencies in the code by right now my primary concern is to get the work done, i.e, the pick the lines in the required order, so kindly don't taunt about the coding skills.

Thanks.
 

Suggested for: Reading Separate lines from a file (C)

Replies
14
Views
2K
Replies
1
Views
651
Replies
2
Views
610
Replies
8
Views
881
Replies
1
Views
668
Replies
3
Views
3K
Replies
1
Views
624
Replies
2
Views
843
Back
Top