Using strcpy and structs

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Oct 2006
Posts: 89
Reputation: TylerSBreton is an unknown quantity at this point 
Solved Threads: 3
TylerSBreton's Avatar
TylerSBreton TylerSBreton is offline Offline
Junior Poster in Training

Using strcpy and structs

 
0
  #1
Nov 12th, 2006
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.

  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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: Using strcpy and structs

 
1
  #2
Nov 12th, 2006
Why are you using the strcpy function? These are C++ strings. Use the = operator.

thisRecord.fieldName =next
バルサミコ酢やっぱいらへんで
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 89
Reputation: TylerSBreton is an unknown quantity at this point 
Solved Threads: 3
TylerSBreton's Avatar
TylerSBreton TylerSBreton is offline Offline
Junior Poster in Training

Re: Using strcpy and structs

 
0
  #3
Nov 14th, 2006
No, this is a C program. string is defined as a char* up top.

PS. I figured out the problem. Thanks
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: Using strcpy and structs

 
0
  #4
Nov 14th, 2006
Good. Mind posting the solution so that someone else can learn from it?
Thank you.
バルサミコ酢やっぱいらへんで
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 89
Reputation: TylerSBreton is an unknown quantity at this point 
Solved Threads: 3
TylerSBreton's Avatar
TylerSBreton TylerSBreton is offline Offline
Junior Poster in Training

Re: Using strcpy and structs

 
1
  #5
Nov 14th, 2006
Originally Posted by WolfPack View Post
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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 2530 | Replies: 4
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC