How to split string into a structure?

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

Join Date: Nov 2009
Posts: 1
Reputation: auracabarcas is an unknown quantity at this point 
Solved Threads: 0
auracabarcas auracabarcas is offline Offline
Newbie Poster

How to split string into a structure?

 
0
  #1
Nov 1st, 2009
hi, i have been unable to solve a problem related to C (i'am a newbie girl). I've googled around and maybe I have a flat learning curve :-(
the fact is that i need to write a program that reads from a .txt file and print the output to another .html file (table)
the input file has 6 space-separated fields per line (activity_code, activity, begining_date, ending_date, days and hour of the day -Morning/Afternoo/Night) and downwards is alike.
i.e. this 2 lines:
ZZZ Tancat 01/01/2009 31/12/2009 SMT MN

PER PerlMeeting 01/05/2009 31/05/2009 TWF N

How do I split this strings and assign them to a structure in order to can handle data? Here is the code I wrote, it's very simple and hope you can help me with some advice. Thanks in advance.

  1. #include <stdio.h>
  2. #include <ctype.h> /* to handle isspace()
  3. #define MAXFILES 200
  4. #define MAXLEN 50
  5.  
  6. typedef struct {
  7. char *CODE[MAXLEN];
  8. char *NAME[MAXLEN];
  9. char *DATE_IN[MAXLEN];
  10. char *DATE_OUT[MAXLEN];
  11. char *DoW[MAXLEN];
  12. char *HOUR[MAXLEN];
  13. } rec_s;
  14. int main()
  15. {
  16. rec_s LOG[MAXLEN];
  17. char c[120]; /* declare a char array */
  18. char actividad[MAXLEN];
  19. char d;
  20. char words[MAXLEN];
  21. char delims[] = " ";
  22.  
  23. int count=0;
  24. char *buffer;
  25. int i;
  26. int j;
  27.  
  28. FILE *file; /* declare a FILE pointer */
  29.  
  30. file = fopen("requests.txt", "r");
  31.  
  32.  
  33. /* open a text file for reading */
  34.  
  35. if(file==NULL) {
  36. printf("Error: can't open file.\n");
  37.  
  38. return 1;
  39. }
  40. else {
  41. printf("File opened!:\n\n");
  42.  
  43.  
  44.  
  45. while(!feof(file)) {
  46. d = fgetc(file);
  47. if ( ! isspace (d) ) /* how do i use this function to assign every line string (6) to its appropriate field? */
  48. {
  49. fgets(c, 120, file);
  50. puts(c); /* this shows in screen the input file */
  51. /* I don't know how to add every string to the structure */
  52. }
  53.  
  54.  
  55.  
  56. }
  57. printf("\n\nNow closing file...\n");
  58. fclose(file);
  59. return 0;
  60.  
  61. }
  62.  
  63. }
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 421
Reputation: gerard4143 is on a distinguished road 
Solved Threads: 48
gerard4143's Avatar
gerard4143 gerard4143 is offline Offline
Posting Pro in Training
 
0
  #2
Nov 1st, 2009
Whoops my bad...Please disregard the following
Last edited by gerard4143; Nov 1st, 2009 at 9:53 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,048
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 178
Aia's Avatar
Aia Aia is offline Offline
Postaholic
 
1
  #3
Nov 2nd, 2009
hope you can help me with some advice.
First, you must understand what constitutes a string in C. It is a sequence of chars terminated with a '\0'.
So if I have this:
  1. char fullname[] = "Jiminy Cricket";
and I want to split it into name and last name, I have to make it as:
'J', 'i', 'm', 'i', 'n', 'y', '\0'; and 'C', 'r', 'i', 'c', 'k', 'e', 't', '\0';
Secondly, there's not memory to hold any string in your structure rec_s, only pointers that will hold the address of something.
Reply With Quote Quick reply to this message  
Reply

Message:



Other Threads in the C Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC