Hi Salem, thank you for been the only one to reply to my posts, I really appreciate it. This is my new code I have and is currently working.
Code:
fgets( demand_stream, sizeof deman_stream, stdin);
I use the fgets – but why should I use it to write to a file or to read to a file. That’s what I am thinking when I implement it. Or can I just use it to read from the variable demand_stream and do the algorithm. What you think is more efficient?
Can you give me some hints about how I am doing this and what I am doing wrong?
Using FIFO (with 4 main memory pages) = how do I take the first page value and read
Do you think i should keep track of your position in the string and remember that the string is actually an array and not write to any external file....
Thanks
// demand_stream.c
Code:
#include <stdio.h>
/*
* This is a program to read a file containing a demand_steam
*
* The data line format is like this:
*
* Field\tData\n
*
* Where `Field' is white-space-less, and `Data' may contain anything (up to
* a newline.) See the file `stored.data'.
*/
int main(int argc, char *argv[]) {
FILE *data_file_ptr; /* The file containing the label data. */
char line[100]; /* A line read from the data file. */
char *line_ptr; /* Used with fgets to detect an EOF or error. */
char good_file = 0; /* 0 if the data file is invalid; otherwise 1. */
char scan_count; /* Number of matches with sscanf. */
char keyword[50]; /* A keyword in the data file. */
char value[50]; /* A value corresponding to a keyword. */
char inside_record = 0; /* 1 if we're parsing inside a record. */
unsigned int labelId; /* The label unique identifier. */
unsigned int label_num; /* Index into `for' loop. */
/*
* Each label lives inside a `label' structure. Start out by allowing
* enough space for 100 of them.
*/
struct label_struct {
char demand_stream[50];
} label[4];
/* Open the data file. */
data_file_ptr = fopen("stored.data", "r");
if (data_file_ptr == NULL) {
fprintf(stderr, "%s: Failed to open file %s for reading.\n",
argv[0], "stored.data");
exit(8);
}
/* Read the records in, assigning them to `label_struct's. */
while (1) {
line_ptr = fgets(line, sizeof(line), data_file_ptr);
if (line_ptr == NULL)
break;
if (strcmp(line, "Labels\tConfiguration file\n") == 0) {
good_file = 1; /* This is a good data file. */
#ifdef DEBUG
printf("debug ==> The data file is good.\n");
#endif
} else if (good_file == 1) { /* The file is already known good. */
scan_count = sscanf(line, "%s\t%[^\n]\n", &keyword, &value);
if (scan_count != 2) {
continue; /* This must have been a blank line. */
#ifdef DEBUG
printf("debug ==> Blank line in data file.\n");
#endif
}
if (strcmp(keyword, "LabelID") == 0) {
labelId = atoi(&value); /* Index into `label' array. */
continue; /* Continue to the next line. */
}
if (strcmp(keyword, "Demand_Stream") == 0) {
strcpy(label[labelId].demand_stream, value);
continue; /* Continue to the next line. */
}
}
}
fclose(data_file_ptr);
/* Print out the formatted strings so i can generate FIFO table with page faults */
for (label_num = 0; label_num <= labelId; ++label_num) {
printf("--------------------------------------------------\n");
printf("%s\n", label[label_num].demand_stream);
}
printf("--------------------------------------------------\n");
return(0);
}
//stored.data
Code:
Labels Configuration file
Version 1
LabelID 0
Demand_Stream 1
EndLabel
LabelID 1
Demand_Stream 2
EndLabel
LabelID 2
Demand_Stream 3
EndLabel
LabelID 3
Demand_Stream 4
EndLabel
EndFile