You are on page 1of 16

Hello friends... I have to submit an asginment on the following topic.

Write a program to scan a text file for unique words and store these words alphabetically in a linked list. You have to take care of all the puncutation marks . For example "my name is Sumit. What is your name? " here we have to scan the file till we find space,a comma,question mark or some other punctuation mark and all the characters before that forms a word . I have written the following code but the problem is that my code also modifies the text file. Please help me to modify my code so that it is used to scan the text file and store the unique words in a linked list in sorted order without modifying the text file....... Please help........
Expand|Select|Wrap|Line Numbers

1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32.

#include<stdio.h> #include<conio.h> #include<alloc.h> #include<process.h> #include<dos.h> struct node { char value[15]; struct node * next; }; typedef struct node * nodeptr; void addnode(nodeptr *,char []); void myinfo(); void main() { nodeptr front=NULL,temp,p,q; FILE *outfile; int c,count=1; char ch; char *filename; char *type; char str[15],tmp[15]; filename=(char *)malloc(sizeof(char)*30); type=(char *)malloc(sizeof(char)*10); myinfo(); clrscr(); puts("ENTER FILENAME "); puts("--------------"); gets(filename); puts("\nENTER MODE OF WRITING ");

33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. 61. 62. 63. 64. 65. 66. 67. 68. 69. 70. 71. 72. 73. 74. 75. 76. 77. 78. 79. 80.

puts("---------------------"); gets(type); if((outfile=fopen(filename,type))==NULL) { puts("ERROR"); exit(1); } puts("\nENTER TEXT TO THE FILE: USE ^Z TO END"); puts("-------------------------------------"); do { c=fgetc(stdin); if(c=='.'||c==','||c=='?'||c==';'||c=='"') { c=' '; fputc(c,outfile); } else fputc(c,outfile); }while(c!=EOF); rewind(outfile); outfile=fopen(filename,"r"); while(!feof(outfile)) { fscanf(outfile,"%s",str);

temp=front; if(temp==NULL) { addnode(&front,str); } else { while(temp!=NULL) { if(strcmp(str,temp->value)==0) { count=0; break; } temp=temp->next; }

81. 82. 83. 84. 85. 86. 87. 88. 89. 90. 91. 92. 93. 94. 95. 96. 97. 98. 99. 100. 101. 102. 103. 104. 105. 106. 107. 108. 109. 110. 111. 112. 113. 114. 115. 116. 117. 118. 119. 120. 121. 122. 123. 124. 125. 126. 127. 128. void addnode(nodeptr *q,char val[]) fclose(outfile); getch(); } if(count==1) { addnode(&front,str); } else count=1; } } /* SORTING PART */ for(p=front;p->next!=NULL;p=p->next) { for(q=p->next;q!=NULL;q=q->next) { if(strcmp(p->value,q->value)>0) { strcpy(tmp,q->value); strcpy(q->value,p->value); strcpy(p->value,tmp); } } }

/* DISPLAYING SORTED LIST */ puts("\nUNIQUE ELEMENTS IN ALPHABETICAL/ASCENDING ORDER"); puts("-----------------------------------------------"); p=front; while(p!=NULL) { printf("%s\t",p->value); p=p->next; }

129. 130. 131. 132. 133. 134. 135. 136. 137. 138. 139. 140. 141. 142. 143. 144. 145. 146. 147. 148. 149. 150. 151. 152. 153. 154. 155. 156. 157. 158. 159. 160. 161. 162. 163. 164. 165. 166. 167. 168. 169. 170. 171. 172. 173. 174. 175. 176.

{ nodeptr p=NULL,r; p=(nodeptr)malloc(sizeof(struct node)); strcpy(p->value,val); if(*q==NULL) { p->next=NULL; *q=p; } else { r=*q; while(r->next!=NULL) r=r->next; p->next=NULL; r->next=p; } } void myinfo() { int i; char info1[55]="A PROGRAM TO READ A TEXT FILE AND TO STORE UNIQUE WORDS"; char info2[48]="IN A LINKED LIST IN ALPHABETICAL/ASCENDING ORDER"; char info3[42]="DESIGNED BY SUMIT GAUR. MCA-I ROLL NO 4019"; clrscr();gotoxy(12,10); for(i=0;i<55;i++) { printf("%c",info1[i]); delay(100); } gotoxy(15,11); for(i=0;i<48;i++) { printf("%c",info2[i]); delay(100); } gotoxy(17,13); for(i=0;i<42;i++) { printf("%c",info3[i]); delay(100); } delay(5000);

177. 178. }

179. Hi, everyone. I am having a trouble with this program. It must read data from a file and store it in linked list. Then I want to write it back to the file. I wrote a program but it doesn't work. Can anyone help me to understand where I am wrong. Thanks in advance for your help.
01 #include <stdio.h> 02 #include<stdlib.h> 03 #define CHARS 30 04 #define NAMES 20 05 06 07 int main(void) 08 { 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 /* open the file and load the strings into 2 arrays 25 26 27 28 29 30 31 32 while (!feof(fp)) { count++; fscanf(fp,"%s %s ",list1[count],list2[count]); } if((fp=fopen("merge.dat","r"))==NULL) printf("Cannot open file fot reading"); */ }; typedef struct NODE Node; FILE *fp, *out; Node* head; Node* curr; head=(Node*)malloc(sizeof(Node)); curr=head; struct NODE { char name[CHARS]; struct NODE *next; int count=-1; int index1=0,index2=0, i=0; char list1[NAMES][CHARS],list2[NAMES][CHARS];

33 34

fclose(fp);

35 /*copys names from both lists until one of the lists is finished*/ 36 while(index1<=count&&index2<=count) 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 /*finish off the list that isn't finished*/ 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 } for(i=index2; i<=count; ++index2) { curr->next=(Node*)malloc(sizeof(Node)); curr=curr->next; strcpy(curr->name, list1[index2]); ++index2; } for(i=index1; i<=count; ++index1) { curr->next=(Node*)malloc(sizeof(Node)); curr=curr->next; strcpy(curr->name, list1[index1]); ++index1; } } else { strcpy(curr->name, list2[index2]); ++index2; } if(strcmp(list1[index1], list2[index2])>0) /*compare list1 and list2*/ { strcpy(curr->name, list1[index1]); ++index1; { curr->next=(Node*)malloc(sizeof(Node)); node*/ curr=curr->next; allocated space*/ /* point curr to new /*allocate space for next

72 73 74

curr->next=NULL; curr=head;

75 /*writing list back to the file*/ 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 } free(&curr); return 0; while(curr!=NULL) { fprintf(out, "%s\n", curr->name); curr=curr->next; } fclose(out); if((out=fopen("merge.dat","w"))==NULL) printf("Cannot open file for writing\n");

#include<stdio.h> struct record { char letter; int empNum; char lastName[20]; char firstName[12]; double hourRate; }; int main(void) { static const char filename[] = "file.txt"; FILE *file = fopen(filename, "r"); if ( file != NULL ) { char line[BUFSIZ]; struct record record; while ( fgets(line, sizeof line, file) != NULL && sscanf(line, "%d %11s %19s %lf", &record.empNum, record.firstName, record.lastName, &record.hourRate) == 4 ) { #ifndef NDEBUG /* while debugging */ printf("%10d %-20s %-12s %g\n", record.empNum, record.lastName, record.firstName, record.hourRate);

#else /* real code */ /* add 'record' to linked list */ #endif } fclose(file); } else { perror(filename); } return 0; } /* my output 12452 Smith 891756 Bloe 268174 Smith 718756 Totts 261786 Packer */ John Joe Sample Terry James 23.56 24.94 18.56 27.86 25.98

> program that reads 3 list of numbers, > which are stored in three seperate files, > and creates one sorted list. > Each file should contain not more than 15 numbers. */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <limits.h> #define #define #define #define #define NFILES 3 MAX_LINES_PER_FILE 15 LU_RAND_SEED 0LU LU_RAND(S) ((S) * 69069 + 362437 & 0XFFFFFFFFLU) NMEMB(A) (sizeof (A) / sizeof *(A))

struct list_node { struct list_node *next; void *data; }; typedef struct list_node list_type; int numcomp(const list_type *a, const list_type *b); int get_line(char **lineptr, size_t *n, FILE *stream); list_type *list_append (list_type **head, list_type *tail, void *data, size_t size); void list_free(list_type *node, void (*free_data)(void *)); list_type *list_sort(list_type *head, int (*compar)(const list_type *, const list_type *)); int list_fputs(const list_type *node, FILE *stream); static list_type *sort_node (list_type *head, int (*compar)(const list_type *, const list_type *)); static list_type *merge_lists(list_type *head, list_type *tail, int (*compar)(const list_type *, const list_type *));

static list_type *split_list(list_type *head); int main(void) { long unsigned index, lu_seed, line; int rc; char *buff; size_t size; list_type *tail, *head; char fn[L_tmpnam]; FILE *fp; puts("/* BEGIN file_sort_2.c output */"); /* ** Open temporary input text files for writing. ** Write long unsigned values to standard output ** as well as to temporary input text files. ** Close each file after filling with long unsigned values. ** Open input text files for reading. ** Represent each line of each input text file ** as a string in a node of a linked list. ** Close each temp input file after reading. */ size = 0; buff = NULL; head = tail = NULL; lu_seed = LU_RAND_SEED; tmpnam(fn); for (index = 0; index != NFILES; ++index) { fp = fopen(fn, "w"); if (fp == NULL) { fputs("fopen(fn, \"w\") == NULL\n", stderr); break; } printf("\nInput file #%lu\n", index + 1); line = lu_seed % MAX_LINES_PER_FILE + 1; while (line-- != 0) { lu_seed = LU_RAND(lu_seed); fprintf( fp, "%lu\n", lu_seed); fprintf(stdout, "%lu\n", lu_seed); } fclose(fp); fp = fopen(fn, "r"); if (fp == NULL) { fputs("fopen(fn, \"r\") == NULL\n", stderr); break; } while ((rc = get_line(&buff, &size, fp)) > 0) { tail = list_append(&head, tail, buff, rc); if (tail == NULL) { fputs("tail == NULL\n", stderr); break; } } fclose(fp); if (rc != EOF) {

fprintf(stderr, "rc == %d\n", rc); break; } } /* ** Free allocated buffer used by get_line function. ** Remove temp input file. */ free(buff); remove(fn); /* ** Sort list. ** Display list. ** Free list. */ head = list_sort(head, numcomp); puts("\nSorted Output List"); list_fputs(head, stdout); list_free(head, free); puts("\n/* END file_sort_2.c output */"); return 0; } int numcomp(const list_type *a, const list_type *b) { const long unsigned a_num = strtoul(a -> data, NULL, 10); const long unsigned b_num = strtoul(b -> data, NULL, 10); return b_num > a_num ? -1 : b_num != a_num; } int get_line(char **lineptr, size_t *n, FILE *stream) { int rc; void *p; size_t count; count = 0; while ((rc = getc(stream)) != EOF || !feof(stream) && !ferror(stream)) { ++count; if (count == (size_t)-2) { if (rc != '\n') { (*lineptr)[count] = '\0'; (*lineptr)[count - 1] = (char)rc; } else { (*lineptr)[count - 1] = '\0'; } break; } if (count + 2 > *n) { p = realloc(*lineptr, count + 2); if (p == NULL) { if (*n > count) { if (rc != '\n') {

(*lineptr)[count] = '\0'; (*lineptr)[count - 1] = (char)rc; } else { (*lineptr)[count - 1] = '\0'; } } else { if (*n != 0) { **lineptr = '\0'; } ungetc(rc, stream); } count = 0; break; } *lineptr = p; *n = count + 2; } if (rc != '\n') { (*lineptr)[count - 1] = (char)rc; } else { (*lineptr)[count - 1] = '\0'; break; } } if (rc != EOF || !feof(stream) && !ferror(stream)) { rc = INT_MAX > count ? count : INT_MAX; } else { if (*n > count) { (*lineptr)[count] = '\0'; } } return rc; } list_type *list_append (list_type **head, list_type *tail, void *data, size_t size) { list_type *node; node = malloc(sizeof *node); if (node != NULL) { node -> next = NULL; node -> data = malloc(size); if (node -> data != NULL) { memcpy(node -> data, data, size); if (*head != NULL) { tail -> next = node; } else { *head = node; } } else { free(node); node = NULL; } } return node;

} void list_free(list_type *node, void (*free_data)(void *)) { list_type *next_node; while (node != NULL) { next_node = node -> next; free_data(node -> data); free(node); node = next_node; } } list_type *list_sort(list_type *head, int (*compar)(const list_type *, const list_type *)) { return head != NULL ? sort_node(head, compar) : head; } int list_fputs(const list_type *node, FILE *stream) { int rc = 0; while (node != NULL && (rc = fputs(node -> data, stream)) != EOF && (rc = putc('\n', stream)) != EOF) { node = node -> next; } return rc; } static list_type *sort_node(list_type *head, int (*compar)(const list_type *, const list_type *)) { list_type *tail; if (head -> next != NULL) { tail = split_list(head); tail = sort_node(tail, compar); head = sort_node(head, compar); head = merge_lists(head, tail, compar); } return head; } static list_type *split_list(list_type *head) { list_type *tail; tail = head -> next; while ((tail = tail -> next) != NULL && (tail = tail -> next) != NULL) { head = head -> next;

} tail = head -> next; head -> next = NULL; return tail; } static list_type *merge_lists(list_type *head, list_type *tail, int (*compar)(const list_type *, const list_type *)) { list_type *list, *sorted, **node; node = compar(head, tail) > 0 ? &tail : &head; list = sorted = *node; *node = sorted -> next; while (*node != NULL) { node = compar(head, tail) > 0 ? &tail : &head; sorted -> next = *node; sorted = *node; *node = sorted -> next; } sorted -> next = tail != NULL ? tail : head; return list; } /* END file_sort_2.c */

-pete

Problem with linked list function in C?


I have a program that reads each character from a file and adds that to a lined list, this works fine. Then the program goes though each node, does a comparison and stores certain characters in a variable called word. The problem I have is with the function that adds each word to a linked list called words (*word_add). I get the following error message [Warning] assignment from incompatible pointer type. Below is the source code, would appreciate any help. #include <stdio.h> #include <stdlib.h> #include <ctype.h> typedef struct node { char data; struct node *next; /* pointer to next element in list */ }list; typedef struct node2 { char word[20]; struct node2 *next;

}word_list; list *list_add(list ** head, char ch) { list *newNode = malloc(sizeof(list)); newNode->data = ch; newNode->next = *head; //printf("%p\n", newNode->next); *head = newNode; return *head; }

word_list *word_add(word_list ** words, char* word[]) { word_list *newNode = malloc(sizeof(word_list)); newNode->word = word; newNode->next = *words; *words = newNode->next; return *words; } FILE *file; int i,count; char *ptr1; list *head = NULL; //linked list to store each character from file word_list *words =NULL; //linked list to store each word created taken from list above char word[30]; int main(int argc, char *argv[]) { i = 0; count = 0; int length; //print the name of the file inputted printf("%s\n", argv[1]); //open the file with the read only format file = fopen(argv[1], "r"); //goes to the end of the file fseek(file, 0, SEEK_END); //sets the character length length = ftell(file); //sets the file back to the start rewind(file); //inputs one character at a time from file to list while (count <= length) { //inputs pointer to head, an uppercase of one character list_add(&head, toupper(fgetc(file))); count++; }

if(file == NULL)//checks to see if the file opened { printf("Error:Could not open file"); return 0; } else { printf("File opened ok\n"); fclose(file);//close the file findwords(head);//call function findwords } return 0; } //this function goes through the linked list of characters to create a list of words int findwords(struct node *head) { //create temp linked list list *ptrTmp = head; i = 0; int j; while(ptrTmp != NULL) { if(ptrTmp->data) { if(((ptrTmp->data >= 65) && (ptrTmp->data <= 90)) || (ptrTmp->data == '-') || (ptrTmp->data == '\'')) { word[i] = ptrTmp->data; i++; } else { i = 0; printf("%s\n", word); //pass the words linked list and the new word into word_add function word_add(&words, &word); //sets word to NULL for(j = 0;j <= (strlen(word) - 1); j++) { word[j] = 0; } } } ptrTmp = ptrTmp->next; } /* list *ptrTmp2 = words; while(ptrTmp2 != NULL)

{ printf("%s", ptrTmp2->data); ptrTmp2 = ptrTmp2->next; } */ return 0; }


2 years ago Report Abuse

Farewell

Best Answer - Chosen by Asker


I get several errors in my version: list *newNode = malloc(sizeof(list)); needs to be list *newNode = (list *)malloc(sizeof(list)); word_list *newNode = malloc(sizeof(word_list)); => word_list *newNode = (word_list *)malloc(sizeof(word_list)); But I think the real problem is here word_list *word_add(word_list ** words, char* word[]) char word[20]; newNode->word = word; Your structure member is an array of 20 chars, but you are trying to assign to it an array of character pointers. I think you want word_list *word_add(word_list ** words, char* word) and then strcpy(word, newNode->word); You can't assign arrays or strings (strings are just arrays with a null terminator :) ). You have to copy them over.

You might also like