Jump to content

Problem reading separate lines from a file.

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
7 replies to this topic

#1
TheUmer

TheUmer

    Newbie

  • Members
  • PipPip
  • 27 posts
Hi all!

During my OS assignment, I'm having a little problem regarding filing. The problem is that I'm to read each line in a separate array and after that perform further operations. I've initially read the entire file in a char array and after that I intend to split it up accordingly. The file is in the following format:

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

(and so on)

I've written the following code:

(Variables definitions and all)

f=fopen ("umer.txt","r");
if (!f)
      printf ("Could not open the file.\n");
while (fgets (s,'\n',f)!=NULL)
{
      if (s[i] == 'P')
      {
            processes++;
      }
      printf ("%s", s); 
}
printf ("processes are: %d\n", processes);
printf ("\n");
fclose(f);

while (s[i]!='\n')
{
      printf ("char is: %c\n", s[i]);
      i++;
      printf ("-%d-", i);
}

But when I output the supposedly initial charaters from the array s[i] (which contains the entire file supposedly), it outputs 4,5,7,6. (It should output the first line of the file, i.e P1 (a tab) 0)

What should I do? Any help would be highly appreciated.

Thanks.

#2
runchman

runchman

    Newbie

  • Members
  • PipPip
  • 20 posts
I didn't look at your code very closely, but I do see in this section:

while (fgets (s,'\n',f)!=NULL)
{
      if (s[i] == 'P')
      {
            processes++;
      }
      printf ("%s", s); 
}

I'm presuming you have i initialized to 0?


- John
Teach-Me-C.com | Let an industry veteran teach you how to program in C

#3
runchman

runchman

    Newbie

  • Members
  • PipPip
  • 20 posts
Ok now I do see an issue. Look at your fgets function call:

while (fgets (s,'\n',f)!=NULL)

fgets reads into string s, at most '\n' characters, from file f.

in other words, you are telling to read in at most x number of characters, where x is the ascii value
of a newline.

what you probably wanted was
fgets(s, 255, f)  // 255 characters max, or whatever number is the max for your file layout




#4
runchman

runchman

    Newbie

  • Members
  • PipPip
  • 20 posts
An additional issue you'll have, is your multiple fgets calls (until you hit the NULL which I believe means you've hit EOF) are going to always be putting each line in the array S starting at S[0]; In other words, you aren't going to get the entire file contents into your array as you are expecting.

For that to work, you'd have to have some other pointer that you moved along after each fgets call, so next time you were (in principle) doing fgets(s...), fgets(s[offset1],....), fgets(s[offset2],...) etc.

That is, if I understood your intent correctly. My brain is a bit fuzzy tonite :)

- John
Teach-Me-C.com | Let an industry veteran teach you how to program in C

#5
runchman

runchman

    Newbie

  • Members
  • PipPip
  • 20 posts
I'd break it up, don't try to put it all in one contiguous array:

int line;

line = 0;
while (fgets(s[line],255,f) != NULL) {
    if (s[line][0] == 'P') {
        processes++;
    }
    line++;
}
...

for (i = 0; i < line; i++) {
    for (c = 0; c < strlen(s[line]); c++) {
        printf("--%c--",s[line][c]);
    }
    printf("\n");
}

At least in principle anyway, I certainly didn't compile the above but you get the idea.

- John
Teach-Me-C.com | Let an industry veteran teach you how to program in C

#6
runchman

runchman

    Newbie

  • Members
  • PipPip
  • 20 posts
Or if you do want the whole thing in a one-d array:

char * charPtr;
charPtr = s;
*charPtr = fgetc(f);
while ( *charPtr != EOF) {
    charPtr++;
    *charPtr = fgetc(f);
}
- John
Teach-Me-C.com | Let an industry veteran teach you how to program in C

#7
TheUmer

TheUmer

    Newbie

  • Members
  • PipPip
  • 27 posts
Thanks a lot for your response. But before I read this all, I had manipulated the code a bit more. 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:


#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?

#8
TheUmer

TheUmer

    Newbie

  • Members
  • PipPip
  • 27 posts
I've tried a bit more and I think I'm near. After this ending piece of 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:

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.