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.
  • #1
Peon666
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?
 
Physics news on Phys.org
  • #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.
 

1. How do I read separate lines from a file in C?

To read separate lines from a file in C, you can use the fgets() function. This function reads a line from the specified file and returns a string. You can then use a loop to read multiple lines from the file.

2. What is the difference between fgets() and gets() in C?

Both fgets() and gets() are used to read input from a file or from the keyboard. However, fgets() is safer to use because it allows you to specify the maximum number of characters to be read, preventing buffer overflows. Gets(), on the other hand, does not have this feature and can lead to security vulnerabilities.

3. How do I open a file in C?

To open a file in C, you can use the fopen() function. This function takes two arguments: the name of the file to be opened and the mode in which the file will be accessed (e.g. "r" for read, "w" for write). It returns a file pointer that can be used to read or write to the file.

4. How do I check if a file exists in C?

To check if a file exists in C, you can use the access() function. This function takes the name of the file and a mode as arguments and returns 0 if the file exists or -1 if it does not. Alternatively, you can also use the stat() function to get more information about the file, such as its size and permissions.

5. How do I close a file in C?

To close a file in C, you can use the fclose() function. This function takes a file pointer as an argument and closes the file associated with it. It is important to close files after using them to free up memory and prevent any potential issues with file access.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
17
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
854
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
8K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
Back
Top