943,648 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 4608
  • C RSS
Nov 12th, 2006
0

Using strcpy and structs

Expand Post »
This block of code will compile, but gives me a runtime error:

before the first comment is a header that is included in the file that you may need to analyze my code.

c Syntax (Toggle Plain Text)
  1. typedef char *string;
  2. typedef struct
  3. {
  4.  
  5. string fieldName;
  6. string value;
  7.  
  8. }oneField;
  9.  
  10. /* this starts the function*/
  11.  
  12. link tokenize (string input_string)
  13. {
  14. int numOfSpaces;
  15. string next, temp;
  16. oneField thisRecord;
  17. /* used for head of linked list */
  18. link head;
  19. int field, counter = 0;
  20. void insert(link*, oneField);
  21. head = 0;
  22. while(1)
  23. {
  24. numOfSpaces = 0;
  25. next = next_word(input_string, &numOfSpaces);
  26. /* next = the word which we will work with here */
  27. if(counter == 0){
  28. strcpy(thisRecord.fieldName, next);
  29. }
  30. if(counter == 1){
  31. strcpy(thisRecord.value, next);
  32. }
  33.  
  34. /* if the word returned by next_word was the last word
  35. in the input string, then the element at that length
  36. should be the null character, in which case we will
  37. break from the loop */
  38. counter++;
  39. if(input_string[strlen(next)] == '\0')
  40. break;
  41. input_string += strlen(next) + numOfSpaces;
  42. }
  43. return head;
  44. }

If I comment out the strcpy methods, it will run fine, but for some reason it will not copy the value of the string into the struct. Any help would be greatly appreciated.

Reguards,

Tyler S. Breton
Last edited by TylerSBreton; Nov 12th, 2006 at 8:25 pm.
Similar Threads
Reputation Points: 25
Solved Threads: 3
Junior Poster in Training
TylerSBreton is offline Offline
89 posts
since Oct 2006
Nov 12th, 2006
1

Re: Using strcpy and structs

Why are you using the strcpy function? These are C++ strings. Use the = operator.

thisRecord.fieldName =next
Moderator
Reputation Points: 572
Solved Threads: 115
Mentally Challenged Mod.
WolfPack is offline Offline
1,559 posts
since Jun 2005
Nov 14th, 2006
0

Re: Using strcpy and structs

No, this is a C program. string is defined as a char* up top.

PS. I figured out the problem. Thanks
Reputation Points: 25
Solved Threads: 3
Junior Poster in Training
TylerSBreton is offline Offline
89 posts
since Oct 2006
Nov 14th, 2006
0

Re: Using strcpy and structs

Good. Mind posting the solution so that someone else can learn from it?
Thank you.
Moderator
Reputation Points: 572
Solved Threads: 115
Mentally Challenged Mod.
WolfPack is offline Offline
1,559 posts
since Jun 2005
Nov 14th, 2006
1

Re: Using strcpy and structs

Click to Expand / Collapse  Quote originally posted by WolfPack ...
Good. Mind posting the solution so that someone else can learn from it?
Thank you.
  1. /**************************
  2. * cgilib.h *
  3. ***************************/
  4. typedef char *string;
  5. typedef struct
  6. {
  7. string fieldName;
  8. string value;
  9. }oneField;
  10. struct element {
  11. oneField data;
  12. struct element *next;
  13. };
  14. typedef struct element node;
  15. typedef node *link;
  16. link tokenize(string input_string);
  17. string cgi_val(link head, string field);
  18. void print_table(link head);
  19. string next_word(char *, int *);
  20.  
  21.  
  22.  
  23. /**************************
  24. * cgilib.c *
  25. ***************************/
  26. #include "cgilib.h"
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30.  
  31. /*
  32.  * Used to parse an input string, and returns a linked
  33.  * list of tokens. Maintained by alphabetical order of
  34.  * the field name, and the data stored in each node
  35.  * is a record representing one field
  36.  */
  37. link tokenize (string input_string)
  38. {
  39. int numOfSpaces;
  40. string next;
  41. oneField thisRecord;
  42. /* used for head of linked list */
  43. link head;
  44. int counter = 0;
  45. void insert(link*, oneField);
  46. head = 0;
  47. while(1)
  48. {
  49. numOfSpaces = 0;
  50. next = next_word(input_string, &numOfSpaces);
  51. /* next = the word which we will work with here */
  52. if(counter == 0){
  53. thisRecord.fieldName = (char *) calloc(strlen(next), sizeof(char));
  54. strcpy(thisRecord.fieldName, next);
  55. }else if(counter == 1){
  56. thisRecord.value = (char *) calloc(strlen(next), sizeof(char));
  57. strcpy(thisRecord.value, next);
  58. insert(&head, thisRecord);
  59. }else if(counter % 2 == 0){
  60. thisRecord.fieldName = (char *) calloc(strlen(next), sizeof(char));
  61. strcpy(thisRecord.fieldName, next);
  62. }else{
  63. thisRecord.value = (char *) calloc(strlen(next), sizeof(char));
  64. strcpy(thisRecord.value, next);
  65. insert(&head, thisRecord);
  66. }
  67. /* if the word returned by next_word was the last word
  68.   in the input string, then the element at that length
  69.   should be the null character, in which case we will
  70.   break from the loop */
  71. counter++;
  72. if(input_string[strlen(next)] == '\0')
  73. break;
  74. input_string += strlen(next) + numOfSpaces;
  75. }
  76. return head;
  77. }
  78. /*
  79.  * Steps through the linked list pointed to by the head
  80.  * and finds the node pointed to by the given field
  81.  */
  82. string cgi_val(link head, string field)
  83. {
  84. void toUpper(string);
  85. link start = head;
  86. while(head)
  87. {
  88. toUpper(head->data.fieldName);
  89. toUpper(field);
  90. if(!strcmp(head->data.fieldName, field))
  91. {
  92. return head->data.value;
  93. }
  94. head = head->next;
  95. }
  96. head = start;
  97. return "ERROR! FIELD NAME NOT FOUND!";
  98. }
  99. /*
  100.  * Used to print the table
  101.  * Given: head of linked list
  102.  */
  103. void print_table(link head)
  104. {
  105. link start = head;
  106. printf("\n%s%26s\n%s\n", "Field Name", "Value",
  107. "----------------------------------------------");
  108. while(head)
  109. {
  110. printf("%-20s%-40s\n", head->data.fieldName, head->data.value);
  111. head = head -> next;
  112. }
  113. printf("----------------------------------------------\n");
  114. head = start;
  115. }
  116. /*
  117.  * Used in order to put strings of any length in
  118.  * the linked list (dynamically generates the string)
  119.  */
  120. string next_word(char * pos, int *numOfSpaces){
  121. /* count is the number of total characters in the string,
  122.   whereas trueCount counts special 3-digit characters as
  123.   only one 'true' character*/
  124. int count = 0, trueCount = 0, i;
  125. char * b = pos;
  126. int forConvert;
  127. string a;
  128. char current;
  129. if(pos[0] == '\0')
  130. return NULL;
  131. /* still check for spec. char in input */
  132. while(((current = *b) != '=') && (current != '&') && (current != '\0')){
  133. trueCount++;
  134. count++;
  135. b++;
  136. if(current == '%')
  137. trueCount = trueCount - 2;
  138. }
  139. a = (char *) calloc(trueCount + 1, sizeof(char));
  140. /* check for spec chars */
  141. for(i = 0; i < trueCount; i++)
  142. {
  143. if(*pos == '%')
  144. {
  145. pos++;
  146. switch(*pos)
  147. {
  148. case '0': forConvert = 16*0;
  149. break;
  150. case '1': forConvert = 16*1;
  151. break;
  152. case '2': forConvert = 16*2;
  153. break;
  154. case '3': forConvert = 16*3;
  155. break;
  156. case '4': forConvert = 16*4;
  157. break;
  158. case '5': forConvert = 16*5;
  159. break;
  160. case '6': forConvert = 16*6;
  161. break;
  162. case '7': forConvert = 16*7;
  163. break;
  164. case '8': forConvert = 16*8;
  165. break;
  166. case '9': forConvert = 16*9;
  167. break;
  168. case 'A':
  169. case 'a': forConvert = 16*10;
  170. break;
  171. case 'B':
  172. case 'b': forConvert = 16*11;
  173. break;
  174. case 'C':
  175. case 'c': forConvert = 16*12;
  176. break;
  177. case 'D':
  178. case 'd': forConvert = 16*13;
  179. break;
  180. case 'E':
  181. case 'e': forConvert = 16*14;
  182. break;
  183. case 'F':
  184. case 'f': forConvert = 16*15;
  185. break;
  186. default:
  187. printf("%c is not a proper hex number!\n", *pos);
  188. exit(1);
  189. }
  190. pos++;
  191. switch(*pos)
  192. {
  193. case '0': forConvert += 0;
  194. break;
  195. case '1': forConvert += 1;
  196. break;
  197. case '2': forConvert += 2;
  198. break;
  199. case '3': forConvert += 3;
  200. break;
  201. case '4': forConvert += 4;
  202. break;
  203. case '5': forConvert += 5;
  204. break;
  205. case '6': forConvert += 6;
  206. break;
  207. case '7': forConvert += 7;
  208. break;
  209. case '8': forConvert += 8;
  210. break;
  211. case '9': forConvert += 9;
  212. break;
  213. case 'A':
  214. case 'a': forConvert += 10;
  215. break;
  216. case 'B':
  217. case 'b': forConvert += 11;
  218. break;
  219. case 'C':
  220. case 'c': forConvert += 12;
  221. break;
  222. case 'D':
  223. case 'd': forConvert += 13;
  224. break;
  225. case 'E':
  226. case 'e': forConvert += 14;
  227. break;
  228. case 'F':
  229. case 'f': forConvert += 15;
  230. break;
  231. default:
  232. printf("%c is not a proper hex number!(2)\n", *pos);
  233. exit(1);
  234. }
  235. *numOfSpaces = *numOfSpaces + 2;
  236. a[i] = forConvert;
  237. }else if(*pos == '+'){
  238. a[i] = ' ';
  239. }else{
  240. a[i] = *pos;
  241. }
  242. pos++;
  243. }
  244. a[trueCount] = '\0';
  245. *numOfSpaces = *numOfSpaces + 1;
  246. return a;
  247. }
  248.  
  249. /***************************************************
  250. ****************************************************
  251. ****************************************************
  252. ***************************************************/
  253. void insert(link *list_head, oneField a){
  254. link p = *list_head, prev = NULL, new_node;
  255. while(p && strcmp(a.fieldName, p->data.fieldName) > 0)
  256. {
  257. prev = p;
  258. p = p->next;
  259. }
  260. new_node = malloc(sizeof(node));
  261. new_node -> data = a;
  262. new_node -> next = p;
  263. if(prev)
  264. prev -> next = new_node;
  265. else
  266. *list_head = new_node;
  267. }
  268. void toUpper(string a){
  269. int i;
  270.  
  271. for(i = 0; i < strlen(a); i++){
  272. a[i] = toupper(a[i]);
  273. }
  274. }
  275.  
  276.  
  277. /**************************
  278. * mainProgram.c *
  279. ***************************/
  280. #include <stdio.h>
  281. #include <stdlib.h>
  282. #include <string.h>
  283. #include "cgilib.h" /* It contains the function prototype of
  284. "exit". */
  285. string get_input(void)
  286. {
  287. string a;
  288. int c;
  289. int count = 0;
  290. char file_name[20];
  291. FILE *text;
  292. printf("\n\nEnter the file name --> ");
  293. gets(file_name);
  294. text = fopen(file_name, "r"); /* open the file for reading */
  295. if (!text)
  296. {
  297. printf("\n\nError: The file \"%s\" does not exist.\n",
  298. file_name);
  299. exit(1); /* terminate the program with an error */
  300. }
  301.  
  302. while ((c = getc(text)) != EOF)
  303. count++;
  304.  
  305. fclose(text);
  306. a = (char *)calloc(count+1, sizeof(char));
  307. text = fopen(file_name, "r");
  308. if(!text)
  309. {
  310. printf("file not found!!!!\n");
  311. exit(1);
  312. }
  313. fgets(a, count+1, text);
  314. return a;
  315. }
  316. void main()
  317. {
  318. link a;
  319. char inputField[31];
  320. string inputString;
  321. inputString = get_input();
  322. a = tokenize(inputString);
  323. print_table(a);
  324. while(1){
  325. printf("Enter a field name(Enter exit to Quit): ");
  326. gets(inputField);
  327. if(!strcmp(inputField, "exit")){
  328. break;
  329. }
  330. printf("Value for specified field: %s\n", cgi_val(a,inputField));
  331. }
  332. }
Sorry for the tabbing problem, my text editor didnt include the tabs correctly when I pasted.
I realize there is a memory leak in the cgiscript.c file, I didnt free the space allocated, but the program compiles and runs successfully. Oh, and just so you know, this is a cgiscript file which reads in a cgiscript from a txt file and parse's it into its correct components.
Last edited by TylerSBreton; Nov 14th, 2006 at 7:04 pm.
Reputation Points: 25
Solved Threads: 3
Junior Poster in Training
TylerSBreton is offline Offline
89 posts
since Oct 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Setting the Focus on a control
Next Thread in C Forum Timeline: Validating data





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC