Reading Separate lines from a file (C)

Click For Summary
SUMMARY

This discussion focuses on reading process data from a file in C, specifically the arrival times and burst times of processes. The provided code successfully counts the number of processes and their arrival times but struggles with correctly reading and storing burst times in an integer array. The expected output format requires the bursts to be printed in the correct order, which the current implementation fails to achieve. Key issues include incorrect handling of character reading and array indexing.

PREREQUISITES
  • C programming language fundamentals
  • File I/O operations in C
  • Dynamic memory allocation using malloc
  • Understanding of structures in C
NEXT STEPS
  • Debug the character reading logic in the burst extraction loop
  • Implement proper error handling for file operations in C
  • Explore the use of fgets for reading lines instead of fscanf for better control
  • Learn about using strtok for parsing strings in C to simplify burst extraction
USEFUL FOR

C programmers, software developers working with process scheduling, and students learning file handling and data structures in C.

Peon666
Messages
107
Reaction score
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?
 
Physics news on Phys.org
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.
 
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.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 1 ·
Replies
1
Views
11K
  • · Replies 23 ·
Replies
23
Views
9K