Advertisement
Guest User

strtok and strcpy

a guest
Apr 8th, 2016
614
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.90 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. typedef struct DATA
  6. {
  7.     char firstName[10];
  8.     char lastName[20];
  9.     int N; //number of phone numbers
  10.     char update[1]; //checks to see if a person should be added/removed
  11.   char number[50];
  12.     struct DATA* next;
  13.   struct DATA* prev;
  14. }DATA;
  15.  
  16. struct DATA *head;
  17.  
  18. struct DATA* GetNewNode(char first[10], char last[20], char update[1], int N, char number[50]) {
  19.   struct DATA* newNode = (struct DATA*)malloc(sizeof(struct DATA));
  20.   newNode->N = N;
  21.   strcpy(newNode->firstName, first);
  22.   strcpy(newNode->lastName, last);
  23.   strcpy(newNode->update, update);
  24.   newNode->number;
  25.   newNode->prev = NULL;
  26.   newNode->next = NULL;
  27.   return newNode;
  28. }
  29.  
  30. void InsertAtHead(char first[], char last[], char update[], int N, char number[]) {
  31.   struct DATA* newNode = GetNewNode(first, last, update, N, number);
  32.   if(head == NULL) {
  33.     head = newNode;
  34.     return;
  35.   }
  36.   head->prev = newNode;
  37.   newNode->next = head;
  38.   head = newNode;
  39. }
  40.  
  41. //load the data from the hw3.data file
  42. DATA* loadData(FILE* fp, int *size)
  43. {
  44.     int i = 0;
  45.     DATA *record = NULL;
  46.  
  47.     //needed for string token
  48.     char *token;
  49.     const char s[2] = " "; //delimiter for string token
  50.  
  51.     //needed for finding the contents of each line
  52.     char * line = NULL;
  53.   size_t len = 0;
  54.   ssize_t read;
  55.   int tokCount = 0;
  56.   char *tempN; //used as a temporary storage for N before converting it to an int
  57.   char *checkEOF = "Q";
  58.   int N, numberCount = 0;
  59.  
  60.   //used to copy the data
  61.   char firstName[10];
  62.   char lastName[20];
  63.   int intN; //number of phone numbers
  64.   char update[1]; //checks to see if a person should be added/removed
  65.   char number[50] = "blarg";
  66.   while ((read = getline(&line, &len, fp)) != -1)
  67.   {
  68.     token = strtok(line, s);
  69.     while( token != NULL )
  70.     {
  71.       tokCount = 0;
  72.      
  73.         if (tokCount == 0)
  74.         {
  75.             strcpy(update, token);
  76.         printf("%s\n\n", token);
  77.           token = strtok(NULL, s);
  78.         printf("%s\n\n", token);
  79.         token = token + '\0';
  80.         //strcpy(firstName, token);
  81.         token = strtok(NULL, s);
  82.         tokCount++;
  83.         }
  84. /*      else if (tokCount == 1)
  85.         {
  86.             strcpy(firstName, token);
  87.         printf("%s\n\n\n\n\n\n", token);
  88.           token = strtok(NULL, s);
  89.         tokCount++;
  90.         }
  91.         else if (tokCount == 2)
  92.         {
  93.             strcpy(lastName,token);
  94.         printf("%s\n\n\n\n\n\n", token);
  95.           token = strtok(NULL, s);
  96.         tokCount++;
  97.         }
  98.         else if (tokCount == 3)
  99.         {
  100.             tempN = token;
  101.             intN = atoi(tempN);
  102.         printf("%s\n\n\n\n\n\n", token);
  103.           token = strtok(NULL, s);
  104.         tokCount++;
  105.         }*/
  106.       /*
  107.       else if (tokCount >= 4)
  108.       {
  109.         while( token != NULL )
  110.         {
  111.           strcpy(number, token);
  112.           token = strtok(NULL, s);
  113.           numberCount = numberCount+7;
  114.           number[numberCount] = " "; //so we can delimit the numbers by spaces.
  115.           numberCount++;
  116.         }
  117.       }*/
  118.       else if (strcmp(update, "R") == 0)
  119.       {
  120.       }
  121.       else if (strcmp(update, line) == 0)
  122.       {
  123.         exit(0);
  124.       }
  125.  
  126.       InsertAtHead(firstName, lastName, update, intN, number);
  127.         token = NULL;
  128.       //tokCount++;
  129.     }
  130.   i++;
  131.   }
  132. /*  *size = i;
  133.     return (record);
  134.     if (line)
  135.     {
  136.         free(line);
  137.     }*/
  138.   return record;
  139. }
  140.  
  141. void DISPLAY_INORDER() {
  142.   struct DATA* temp = head;
  143.   while(temp != NULL) {
  144.     printf("lastName: \t%s\n",temp->lastName);
  145.     printf("firstName: \t%s\n",temp->firstName);
  146.     printf("update: \t%s\n", temp->update);
  147.     printf("N: \t%d\n", temp->N);
  148.     printf("number: \t%s\n", temp->number);
  149.     temp = temp->next;
  150.   }
  151.   printf("\n");
  152. }
  153.  
  154. int main(void)
  155. {
  156.     FILE * fp;
  157.     DATA *record = NULL;
  158.     int size;
  159.     fp = fopen("hw8data.txt", "r");
  160.     if (fp == NULL)
  161.       exit(EXIT_FAILURE);
  162.     else
  163.       record = loadData(fp, &size);
  164.  
  165.     DISPLAY_INORDER();
  166.     fclose(fp);
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement