Notices
The Basement Non-Honda/Acura discussion. Content should be tasteful and "primetime" safe.

C programming qs

Thread Tools
 
Old Apr 25, 2004 | 05:47 PM
  #1  
axemansean's Avatar
axemansean
Thread Starter
Senior Member
 
Joined: Jun 2002
Posts: 13,634
Likes: 0
Default 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.
Reply
Old Apr 25, 2004 | 05:53 PM
  #2  
ManInCamo's Avatar
ManInCamo
Old School Crew
 
Joined: May 2000
Posts: 5,251
Likes: 0
Default

I'm struggling through a VB.net program right now - having a similar problem

good luck with that
Reply
Old Apr 25, 2004 | 05:58 PM
  #3  
axemansean's Avatar
axemansean
Thread Starter
Senior Member
 
Joined: Jun 2002
Posts: 13,634
Likes: 0
Default

Thats the code... if anyone wants to give it a shot.

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.
Reply
Old Apr 25, 2004 | 10:30 PM
  #4  
nmolinos's Avatar
nmolinos
Registered User
 
Joined: Mar 2004
Posts: 7
Likes: 0
From: Northern Virginia
Default

oh my god i hate pointers/linked lists/trees.

I feel bad for you, sorry I can't help.
Reply
Old Apr 26, 2004 | 08:07 AM
  #5  
axemansean's Avatar
axemansean
Thread Starter
Senior Member
 
Joined: Jun 2002
Posts: 13,634
Likes: 0
Default

Stupid strcpy!!!! I spent the greater part of the day trying to debug it when all it needed was a strcpy.
Reply
Old Apr 26, 2004 | 08:32 AM
  #6  
sinthetiq's Avatar
sinthetiq
.
 
Joined: Mar 2002
Posts: 10,995
Likes: 0
Default

hm, i remember taking unix C.. yah.. the recursion shit pissed me off
Reply
Related Topics
Thread
Thread Starter
Forum
Replies
Last Post
RON
Do It Yourself
47
May 24, 2025 10:28 AM
CivicPete
92+ Civic/EL & Del Sol
10
Apr 19, 2004 02:29 PM
dominic
Do It Yourself
4
Dec 9, 2003 08:10 PM
Jack
CL, TL, TLX & Vigor
0
Nov 18, 2003 10:57 AM
sphereman
Legend, RL, & RLX
0
Oct 28, 2002 09:10 PM




All times are GMT -8. The time now is 01:22 PM.