I am currently writing a basic program scheduler (just started) which we have a text file with format of Process(id) (space) (quantum) (space) (priority).
I am working on the first bit of the program actually reading in the text file and am trying to assigned the process ids, quantum and priority. ( I think this is the right way to start)

this is what i have so far

#include <stdio.h>
  
int main ( int argc, char *argv[] )
   
{
   
	int i;
   	char current[14]; /* the string for reading in characters from txt file */

	/*assigned now but used in different part of program not written yet*/
	int ProcessID;
	int Quantum;
	int Priority;
    
  
      // We assume argv[1] is a filename to open
  
      for(i = 1; i < 4; i = i+1)     // for loop (starting; while_true; do_this).
  
      {  
      	FILE *file = fopen( argv[i], "r" );
 
      /* fopen returns NULL  on failure */
  
      		if ( file == NULL ) 
      		{
      			printf( "Could not open file\n" );
      		}
  
     		 else
      		{
  
      /* read one character at a time from file, stopping at EOF, which
         indicates the end of the file. Note that the idiom of "assign
         to a variable, check the value" used below works because
         the assignment statement evaluates to the value assigned. */
  
      			while ( ( current[14] = fgetc( file ) ) != EOF )
  
      			{
  
      				/* Assign 8th letter as the process id*/
					if (current[8] >= '0' && current[8] <= '9') 
					{
						Current == ProcessID;					
					}
					/* Assign 10th and 11th characters as the quantum*/
					else if (current[10,11] >= '0' && current[10,11] <= '9') 
					{
						current = Quantum;					
					}
					/* Assign the 13th and 14th character as the priority */
					else if (current[13,14] >= '0' && current[13,14] <= '20') 
					{
						current = Priority;
					}	
    				}
    			}
   
      		fclose( file );
  
      }
  
}

As my understanding goes, this program is reading in the file name, then looping through each character at a time. Then im trying to assign the 8th character as id, 10th and 11th as quantum and 13th and 14th as the priority.

I just seem to be stuck at this point in time. Any help is greatly appreciated.

while ( ( current[14] = fgetc( file ) ) != EOF)

The program (repeatedly) reads the whole file, one character at a time, into the same byte, which also happen to be outside of the current array. What you want is

for(i = 0; (current[i] = fgetc(file)) != '\n'; i++)

provided that current has enough room to accomodate the whole line including the newline character.

The body of the loop is beyond criticism. Can you explain, line by line, what are you trying to achieve?

#include <stdio.h>
  
int main ( int argc, char *argv[] )
   
{
   
	int i;
   	char current[14]; /* the string for reading in characters from txt file */

	/*assgined now but used in different part of program not written yet*/
	int ProcessID;
	int Qauntum;
	int Priority;
    
  
      // We assume argv[1] is a filename to open
  
      for(i = 1; i < 4; i = i+1)     // for loop (starting; while_true; do_this).
  
      {  
      	FILE *file = fopen( argv[i], "r" );  // opens the file and reads
 
      /* fopen returns NULL  on failure */
  
      		if ( file == NULL ) 
      		{
      			printf( "Could not open file\n" );  // returns a error if file cannot be opened/found
      		}
  
     		 else // when file is open do this
      		{
  
     
  
      			   
     			for(i = 0; (current[i] = fgetc(file)) != '\n'; i++)  // loops through each character
  
      			{
  
      				/* Assign 8th letter as the process id*/
					if (current[8] >= '0' && current[8] <= '9') 
					{
						current[8] = ProcessID;	// sets the value at current[8] as the process id number				
					}
					/* Assign 10th and 11th characters as the quantum*/
					else if (current[10,11] >= '0' && current[10,11] <= '9') 
					{
						current[10,11] = Quantum; // sets the value at current 10 and 11 as the quantum number				
					}
					/* Assigne the 13th and 14th character as the priority */
					else if (current[13,14] >= '0' && current[13,14] <= '20') 
					{
						current[13,14] = Priority;  // sets value at current 13 and 14 as the priority que number
					}	
    				}
    			}
   
      		fclose( file );  // closes the file and ends program
  
      }
  
}

I've some extra comments to try explain what i am trying to do.

Basically the text file read in while have number of processes in, in the format described firstly.
So i want the file to be read in, the numbers in the file to be assigned to write place, i.e file will say ProcessID2 10 0 , which will mean the process id is 2, the quantum is 10 and the priority is 0. So i want all of these to be assigned,
so then i can write the rest of the process scheduler, to run the processes one by one depending on there priority.

P.S. you can proberly tell im not very good at programming and struggling! i have general knowledge of whats going on i just never seem to be able to do it myself.

What language's syntax is this: current[13,14] ? In C and C++, you'll need to do it the long way.

Process(id) (space) (quantum) (space) (priority)

A sample of the actual text would be helpful as well.

current[8] = ProcessID;

Is this backwards or something? 'Cuz ProcessID doesn't look like you've given it a value yet.

All in C.
the current[13,14] i am trying to read the 13th and 14th character of the file to assign as the priority.

And no process id , quantum or priority are not set yet, the point i am trying to do is set the value of these 3 to the value set in the text

sample text:

Process1 20 7
Process2 12 6
Process3 94 0

What language's syntax is this: current[13,14] ? In C and C++, you'll need to do it the long way.

A sample of the actual text would be helpful as well.

current[8] = ProcessID;

Is this backwards or something? 'Cuz ProcessID doesn't look like you've given it a value yet.

All in C.
the current[13,14] i am trying to read the 13th and 14th character of the file to assign as the priority.

In C, that's not what it means. That's why I said you'll have to do it the long way. As in,

else if ( (current[10] >= '0' && current[10] <= '9' ) &&
                      (current[11] >= '0' && current[11] <= '9' ) )

And no process id , quantum or priority are not set yet, the point i am trying to do is set the value of these 3 to the value set in the tex

And again, this is backwards.

Thanks for the sample input.

Thanks for the else if correction :)

For the assigning of the quantum etc..
Im not really understanding this is backwards? do i have to assign them to something first then i can assign them to the proper value after with the else if statements?
i.e i just do Quantum = 0 etc.. etc.. at the top then once if statements are run the value will be reassigned????

i have also edited a file i had that gave hex dump of the txt to see if i could alter that to do the thing i want, is this a better way of doing it? i got it to compile but its giving me the wrong output.

#include <stdlib.h>
 #include <stdio.h>
 #include <fcntl.h>     /* required for open */
 #include <unistd.h>    /* required for read write  */
 #include <string.h>    /* required for strcpy  */

 main(int argc, char ** argv)
 {
         int i;
         size_t blocksize = 16;
         char filename[256];
         int  file;
         unsigned char buffer[blocksize];
         ssize_t    status;

		int ProcessID;
		int Quantum;
		int Priority;
             
         strcpy(filename, argv[1]);
         file = open(filename, O_RDONLY);  /* open for reading */
         status = 99;
         while (status > 0)             /* while not EOF or error */
         {
                 status = read(file,buffer,blocksize);
                 if (status < 0)
                 {
                         printf("oops2\n"); exit(1);
                 }
                 for (i=0; i < status; i++)
                 {
                       /* Assign 8th letter as the process id*/
					if (buffer[8] >= '0' && buffer[8] <= '9') 
					{
						buffer[8] = ProcessID;	// sets the value at current[8] as the process id number				
					}
					/* Assign 10th and 11th characters as the quantum*/
					else if ( (buffer[10] >= '0' && buffer[10] <= '9' ) &&
                                 (buffer[11] >= '0' && buffer11] <= '9' ) ) 
					{
						buffer[10,11] = Quantum; // sets the value at current 10 and 11 as the quantum number				
					}
					/* Assigne the 13th and 14th character as the priority */
					else if ( (buffer[13] >= '0' && buffer[13] <= '9' ) &&
                                 (buffer[14] >= '0' && buffer[14] <= '9' ) ) 
					{
						buffer[13,14] = Priority;  // sets value at current 13 and 14 as the priority que number
	    		  		}	
						
			  }  

				printf("process id = %d\n", ProcessID);
				printf("quantum = %d\n", Quantum);
				printf("priority = %d\n", Priority);
               
                 printf("\n");
         } 
         close(file);
 }

In C, that's not what it means. That's why I said you'll have to do it the long way. As in,

else if ( (current[10] >= '0' && current[10] <= '9' ) &&
                      (current[11] >= '0' && current[11] <= '9' ) )

And again, this is backwards.

Thanks for the sample input.

buffer[8] = ProcessID;

In C, x = y assigns a value of y to x. You assign a value of ProcessId to buffer[8]. That's what Dave Sinkula means by backward.

O i get it :) i just swopped it round so its

ProcessID = buffer[8]

Im still getting the wrong output. im getting

process id = -163754450
quantum = 0
priority = -1078953040

process id = -163754450
quantum = 0
priority = -1078953040


???

In C, x = y assigns a value of y to x. You assign a value of ProcessId to buffer[8]. That's what Dave Sinkula means by backward.

Im still getting the wrong output

Of course. Now it is time to get the debugger and look closely to what buffer[8] and others are equal to. Hint: in C index of a first element is 0.

#include <stdio.h>

int main(void)
{
   const char filename[] = "file.txt";
   FILE *file = fopen(filename, "r");
   if ( file )
   {
      char line[20];
      int ProcessID, Quantum, Priority;
      /* Read one line at a line. */
      while ( fgets(line, sizeof line, file) )
      {
         if ( sscanf(line, "Process%d %d %d", 
                     &ProcessID, &Quantum, &Priority) == 3 )
         {
            printf("Process: %d -> Quantum = %d, Priority = %d\n", 
                   ProcessID, Quantum, Priority);
         }
      }
      fclose(file);
   }
   else
   {
      perror(filename);
   }
   return 0;
}

/* file.txt
Process1 20 7
Process2 12 6
Process3 94 0
*/

/* my output
Process: 1 -> Quantum = 20, Priority = 7
Process: 2 -> Quantum = 12, Priority = 6
Process: 3 -> Quantum = 94, Priority = 0
*/

If im understand it right, should it be buffer[7] not [8] as in c 1 is actually 0????

Of course. Now it is time to get the debugger and look closely to what buffer[8] and others are equal to. Hint: in C index of a first element is 0.

Thank you :) got it working fine for me too!

All i need to do is write the process scheduler now :) So expect more posts when i struggle with that!

thanks again!


#include <stdio.h>

int main(void)
{
   const char filename[] = "file.txt";
   FILE *file = fopen(filename, "r");
   if ( file )
   {
      char line[20];
      int ProcessID, Quantum, Priority;
      /* Read one line at a line. */
      while ( fgets(line, sizeof line, file) )
      {
         if ( sscanf(line, "Process%d %d %d", 
                     &ProcessID, &Quantum, &Priority) == 3 )
         {
            printf("Process: %d -> Quantum = %d, Priority = %d\n", 
                   ProcessID, Quantum, Priority);
         }
      }
      fclose(file);
   }
   else
   {
      perror(filename);
   }
   return 0;
}

/* file.txt
Process1 20 7
Process2 12 6
Process3 94 0
*/

/* my output
Process: 1 -> Quantum = 20, Priority = 7
Process: 2 -> Quantum = 12, Priority = 6
Process: 3 -> Quantum = 94, Priority = 0
*/

Thank you :) got it working fine for me too!

All i need to do is write the process scheduler now :) So expect more posts when i struggle with that!

thanks again!

Do you understand the sscanf call which I've used to replace your if-else tree of attempting to examine characters and then to attempt to build a number? (I'm hoping so since you didn't ask questions. This was intended more as a heavy hint than a freebie.)

I had started messing about with isdigit for those characters at specific locations -- but I wondered what might be the case if you had a single-digit 2nd field instead of the two-digit ones in your example text. Building the integers from the characters is rather a pain anyways. Parenthetically, it was looking like this:

if ( isdigit(current[7]) )
      {
         ProcessID = current[7] - '0';
      }
      if ( isdigit(current[9]) && isdigit(current[10]) )
      {
         Quantum = 10 * (current[9] - '0') + current[10] - '0';
      }
      if ( isdigit(current[12]) )
      {
         Priority = current[12] - '0';
         if ( isdigit(current[13]) )
         {
            Priority = 10 * Priority + current[13] - '0';
         }
      }

The call to sscanf makes it easier and probably better for handling non-2-digit values in the 2nd and 3rd fields. But *scanf directives can be confusing at first.

Yer dont i used scanf a fair bit last year just had to crack my book out on it and read up bit to fiddle with the program you gave me :) But hopefuly now it will work for what i want it too. Only problem is first line where its
file = "filename"
is there a way to make it accept any file name ?

As far as the isdigit goes iv never used this before, just having a quick man page read.
As far as im aware if input of 2nd parameter (quantum) is <10 it should have 0 before the digit, and im pretty sure it wont be over 100 in my case, although saying that there is no reason it couldn't be.

Do you understand the sscanf call which I've used to replace your if-else tree of attempting to examine characters and then to attempt to build a number? (I'm hoping so since you didn't ask questions. This was intended more as a heavy hint than a freebie.)

I had started messing about with isdigit for those characters at specific locations -- but I wondered what might be the case if you had a single-digit 2nd field instead of the two-digit ones in your example text. Building the integers from the characters is rather a pain anyways. Parenthetically, it was looking like this:

if ( isdigit(current[7]) )
      {
         ProcessID = current[7] - '0';
      }
      if ( isdigit(current[9]) && isdigit(current[10]) )
      {
         Quantum = 10 * (current[9] - '0') + current[10] - '0';
      }
      if ( isdigit(current[12]) )
      {
         Priority = current[12] - '0';
         if ( isdigit(current[13]) )
         {
            Priority = 10 * Priority + current[13] - '0';
         }
      }

The call to sscanf makes it easier and probably better for handling non-2-digit values in the 2nd and 3rd fields. But *scanf directives can be confusing at first.

Only problem is first line where its
file = "filename"
is there a way to make it accept any file name ?

Of course. Take command-line input, as it seemed you were earlier attempting, or prompt for user input if you choose (I'm partial to this sort of thing for user input -- but I tend to avoid user input in these little test programs I write because it's a pain in the ass).

Yer i now what you mean being a pain, but in long run accepting stdin from terminal just makes it easier if i want to accept other files :)

Of course. Take command-line input, as it seemed you were earlier attempting, or prompt for user input if you choose (I'm partial to this sort of thing for user input -- but I tend to avoid user input in these little test programs I write because it's a pain in the ass).

Yer i now what you mean being a pain, but in long run accepting stdin from terminal just makes it easier if i want to accept other files :)

I'm much more in favor of the "config file" approach over user input, because it's frankly easier to change there than with user input and much easier to repeat.

Yer after all user input is dependent on the user and lets face it, us users can rubbish sometimes!

I'm much more in favor of the "config file" approach over user input, because it's frankly easier to change there than with user input and much easier to repeat.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.