Guest User

Untitled

a guest
Dec 13th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.64 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <ctype.h>
  4. #include <string.h>
  5.  
  6. struct Node {
  7. int timesUsed;
  8. char* word;
  9. struct Node* next;
  10. };
  11.  
  12. struct Node* wordList(FILE *fp, struct Node* head);
  13. struct Node* createNode(struct Node* head, char* word);
  14. char* title(char* file);//creates file name based on command line input
  15.  
  16.  
  17. int main(int argc, char** argv){
  18. //Access file information---------------------------------------------
  19. FILE *fp;
  20. char* fileName = title(argv[1]);
  21. fp = fopen(fileName, "r");
  22.  
  23. //Define Head node--------------------------------------------------------
  24.  
  25. struct Node* head = (struct Node*)malloc(sizeof(struct Node));
  26.  
  27. if (head == NULL){
  28. printf("there is not enough memory on your device to continuen");
  29. }
  30. //Access words--------------------------------------------------------
  31.  
  32. head = wordList(fp, head);
  33. printf("%sn",head->word);
  34.  
  35.  
  36. free(head);
  37. free(fileName);
  38. return 0;
  39. }
  40.  
  41.  
  42. //Function that is causing problems==================================
  43. struct Node* wordList(FILE *fp, struct Node* head){
  44. char c;
  45. char wordHolder[1240];//characters
  46. int j = 0;
  47. int count = 0;
  48.  
  49. do{
  50. c = getc(fp);
  51. printf("%cn",c);
  52. if(isalpha(c) || isdigit(c) || c == '''){
  53. wordHolder[j] = c;
  54. j++;
  55. }
  56. else{
  57. if(count){
  58. char* tempWord = strdup(wordHolder);
  59. head->word = tempWord;
  60. head->timesUsed = 1;
  61. head->next = NULL;
  62. count = 1;
  63. j = 0;
  64. for(i = 0; i< strlen(wordHolder); i++){
  65. wordHolder[i] = '';
  66. }
  67. }
  68.  
  69. else{
  70. head = createNode(head, wordHolder);
  71. j = 0;
  72. for(i = 0; i< strlen(wordHolder); i++){
  73. wordHolder[i] = '';
  74. }
  75.  
  76. }
  77. }
  78. }
  79. while(c != EOF);
  80. return head;
  81. }
  82.  
  83. //Creates a new node at end of list=====================================
  84. struct Node* createNode(struct Node* head, char* word){
  85. struct Node* temp = head;
  86.  
  87. while(temp != NULL){
  88. temp = temp->next;
  89. }
  90.  
  91. temp = (struct Node*)malloc(sizeof(struct Node));
  92. temp->timesUsed = 1;
  93. char* tempWord = strdup(word);
  94. temp->word = tempWord;
  95. temp->next = NULL;
  96.  
  97. return head;
  98. }
  99.  
  100.  
  101. //Creates fileName======================================
  102. char* title(char* file){
  103. char nameOfFile[500];
  104. strcpy(nameOfFile, file);
  105. strcat(nameOfFile, ".txt");
  106. char* temp;
  107. temp = strdup(nameOfFile);
  108.  
  109. if(temp == NULL){
  110. printf("Your computer has litte memory.n");
  111. }
  112. return temp;
  113. }
Add Comment
Please, Sign In to add comment