C programming qs
Can anyone figure this out?
Got a structure:
string
int
int
I am creating a queue and passing the values for string, int and int to it. Whenever I print the queue I keep getting the same string even though I get the correct values for the 2 ints. What I mean is instead of getting:
x 1 1
y 2 2
z 3 3
I get:
x 1 1
x 2 2
x 3 3
I know its getting the correct values on the string because I am doing a print. And I know the pointer is going to the next line because the 2 ints are changing as they should.
Got a structure:
string
int
int
I am creating a queue and passing the values for string, int and int to it. Whenever I print the queue I keep getting the same string even though I get the correct values for the 2 ints. What I mean is instead of getting:
x 1 1
y 2 2
z 3 3
I get:
x 1 1
x 2 2
x 3 3
I know its getting the correct values on the string because I am doing a print. And I know the pointer is going to the next line because the 2 ints are changing as they should.
Thats the code... if anyone wants to give it a shot.
Thanks.
Thanks.
Code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//list structure
struct listNode { //self referential structure
char *object_N;
int sen_level;
int count; //used to create the 5 copies
struct listNode *nextPtr;
};
//queue structure
struct queueNode { //self referential structure
char *subject_N;
int sensitivity;
int max; //keeps a check on the max sensitivity level that the object has read
struct queueNode *next;
};
typedef struct listNode ListNode;
typedef ListNode *ListNodePtr;
typedef struct queueNode QueueNode;
typedef QueueNode *QueueNodePtr;
//write, read, check to see if list is empty, print the list
void lowercase(char[]);
void create_Subj (QueueNodePtr *, QueueNodePtr *, char[], int);
void create_Obj (ListNodePtr *, ListNodePtr *, char[], int);
int emptyL (ListNodePtr);
int isEmpty (QueueNodePtr);
void printObj(ListNodePtr);
//void read_Subj(QueueNodePtr *, ListNodePtr *, char[], char[], char[]);
//void write_Subj();
int main(int argc, char *argv[]){
FILE *inFile; //input file
int c;
ListNodePtr startPtr = NULL, endPtr = NULL; //object list start pointer set to NULL
QueueNodePtr headPtr = NULL, tailPtr = NULL; //head and tail of queue set to NULL
char data[200]; //holds the input string
char *commandPtr; //holds the command... CREATE_SUBJECT, CREATE_OBJECT, READ, WRITE, END
char *subj_Name; //holds the subject name
char *obj_Name; //holds the object name
char *sens_L; //holds the sensitivity level
int s_Level; //holds the sensitivity level in integer form
char *value; //holds the value
//check to make sure number of commands in the command line is 2... ie access filename.txt
if(argc!=2)
printf("USAGE: prog filename.txt\n");
//check to see if file can be opened
else {
if( (inFile = fopen(argv[1], "r")) != NULL){
//Bell Lapuda rules, * property and simple security
//Subject S may have read access to Object O if C(O) <= C(S)
//Subject S that has read access to Object O may have write access to P is C(O) <= C(P)
//read first line
fgets(data, 200, inFile);
//converts to lowercase
lowercase(data);
//breaks into token first one is the command name
commandPtr = strtok(data, " ");
//do while not end of file or end
while(!feof(inFile)){
//if its create_subject then enter this loop
if (strcmp(commandPtr, "create_subject") == 0){
//token to get subject name
subj_Name = strtok(NULL, ",");
//get sensitivity level and convert to int
sens_L = strtok(NULL, " ");
s_Level = atoi(sens_L);
//function call to create
create_Subj(&headPtr, &tailPtr, subj_Name, s_Level);
}
//if its create_object then enter this loop
else if (strcmp(commandPtr, "create_object") == 0){
//token to get the object name
obj_Name = strtok(NULL, ",");
//get sensitivity level and convert to int
sens_L = strtok(NULL, " ");
s_Level = atoi(sens_L);
//function call to create object
create_Obj(&startPtr, &endPtr, obj_Name, s_Level);
}
//if its read then enter this loops
else if (strcmp(commandPtr, "read") == 0){
//token to get subject name
subj_Name = strtok(NULL, ",");
//token to get object name
obj_Name = strtok(NULL, ",");
//token to store value
value = strtok(NULL, " ");
//call to the read function
//read_Subj(&headPtr, &startPtr, subj_Name, obj_Name, value);
}
//if its write then enter this loop
else if (strcmp(commandPtr, "write") == 0){
//token to store subject name
subj_Name = strtok(NULL, ",");
//token to store object name
obj_Name = strtok(NULL, ",");
//token to store value
value = strtok(NULL, " ");
//call to the write function
//write_Subj(&headPtr, &startPtr, subj_Name, obj_Name, value);
}
//get next line
fgets(data, 200, inFile);
//converts to lowercase
lowercase(data);
//first token is again the command
commandPtr = strtok(data, " ");
}
}
//if not then just exit
else
printf("File %s could not be opened. EXITING.\n", argv[1]);
}
if(startPtr==NULL)
printf("There are no objects in the list.\n");
else {
printf("The objects are: \n");
//while(startPtr!=NULL){
printf("%s\t%d\t%d\n", startPtr->object_N, startPtr->sen_level, startPtr->count);
startPtr = startPtr->nextPtr;
printf("%s\n", startPtr->object_N);
//}
}
return 0;
}
//lowercase function... simple use of tolower function from string.h
void lowercase(char string[])
{
int i = 0;
while (string[i])
{
string[i] = tolower(string[i]);
i++;
}
return;
}
//create subject is based on a simple enqueue method. New subjects are entered to the back of the queue since we really aren't taking out subjects so a queue is the simplest data structure to use
void create_Subj(QueueNodePtr *headPtr, QueueNodePtr *tailPtr, char s_Name[], int sens){
QueueNodePtr newPtr; //new temp pointer
newPtr = malloc(sizeof( QueueNode )); //allocate new memory
if(newPtr!=NULL){ //there is space
newPtr->subject_N = s_Name; //set name
newPtr->sensitivity = sens; //set sensitivity
newPtr->max = 0;
newPtr->next = NULL; //next is now NULL
if(isEmpty( *headPtr )) //if its empty
*headPtr = newPtr; //insert to head
else
( *tailPtr )->next = newPtr; //if its not empty then tail is where we insert
*tailPtr = newPtr;
}
else
printf("No space left... OOPS!!!\n");
}
//even though the objects are kept in a list creation is done as a simple queue function as new objects are placed at the end of the queuethe counter i is used to make the 5 copies of each object
void create_Obj(ListNodePtr *startPtr, ListNodePtr *endPtr, char o_Name[], int o_sens){
int i = 1; //local counter that is used to make the 5 copies of each object
ListNodePtr newP;
// for(i=1;i<6;i++){
newP = malloc(sizeof( ListNode )); //allocate new memory
if(newP!=NULL){
newP->object_N = o_Name;
printf("OBJECT NAME %s\n", newP->object_N);
newP->sen_level = o_sens;
//newP->count = i;
newP->nextPtr = NULL;
if(emptyL(*startPtr))
*startPtr = newP;
else
( *endPtr )->nextPtr = newP;
*endPtr = newP;
}
else
printf("Sorry no space left...OOPS!!!\n");
// }
}
//checks to see if the list and queue are empty
int emptyL(ListNodePtr startPtr){
return startPtr == NULL;
}
int isEmpty(QueueNodePtr headPtr){
return headPtr == NULL;
}
//prints contents of object list
/*
void printObj(ListNodePtr current){
if(current==NULL)
printf("There are no objects in the list.\n");
else {
printf("The objects are: \n");
while(current!=NULL){
printf("%s\t%d\t%d\n", current->object_N, current->sen_level, current->count);
current = current->nextPtr;
}
}
}
*/
//the read function first searches the subject queue and finds the subject, if the subject is found it checks the sensitivity value. Then it searches the object list for the first occurance of the object it is looking for. It checks the sensitivity level of the object. If the sensitivity of the object is less than or equal to the subject. If it is read the objects sensitivity value is added to the max column in the subject queue as this will be needed in write. If it cannot be read a message is displayed.
//the write function first parses the value to the correct format of object name and integer value. Next it searches the subject list to see if the subject exists. If the subject is found it checks the max column to see the highest object read. Then it searches the object list for the specified object and checks its sensitivity value. If max is less than or equal to the sensitivity of the object being written to then write executes. Write is done by a simple search on the object list for the last occurance of the specified object and the value is inserted at the end, If write cannot be done an appropriate message is displayed.
Thread
Thread Starter
Forum
Replies
Last Post
RON
Do It Yourself
47
May 24, 2025 10:28 AM



