| | |
How to split string into a structure?
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Nov 2009
Posts: 1
Reputation:
Solved Threads: 0
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.
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.
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <ctype.h> /* to handle isspace() #define MAXFILES 200 #define MAXLEN 50 typedef struct { char *CODE[MAXLEN]; char *NAME[MAXLEN]; char *DATE_IN[MAXLEN]; char *DATE_OUT[MAXLEN]; char *DoW[MAXLEN]; char *HOUR[MAXLEN]; } rec_s; int main() { rec_s LOG[MAXLEN]; char c[120]; /* declare a char array */ char actividad[MAXLEN]; char d; char words[MAXLEN]; char delims[] = " "; int count=0; char *buffer; int i; int j; FILE *file; /* declare a FILE pointer */ file = fopen("requests.txt", "r"); /* open a text file for reading */ if(file==NULL) { printf("Error: can't open file.\n"); return 1; } else { printf("File opened!:\n\n"); while(!feof(file)) { d = fgetc(file); if ( ! isspace (d) ) /* how do i use this function to assign every line string (6) to its appropriate field? */ { fgets(c, 120, file); puts(c); /* this shows in screen the input file */ /* I don't know how to add every string to the structure */ } } printf("\n\nNow closing file...\n"); fclose(file); return 0; } }
1
#3 Nov 2nd, 2009
•
•
•
•
hope you can help me with some advice.
So if I have this:
C Syntax (Toggle Plain Text)
char fullname[] = "Jiminy Cricket";
'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.
![]() |
Other Threads in the C Forum
- Previous Thread: Remowing white spaces + separating sentences
- Next Thread: Print star pattern
| Thread Tools | Search this Thread |
Tag cloud for C
#include ansi array arrays asterisks binarysearch calculate centimeter changingto char command convert copyimagefile cprogramme creafecopyofanytypeoffileinc database directory dynamic fflush file fork forloop framework functions getlasterror givemetehcodez grade graphics gtkgcurlcompiling hacking hardware histogram homework inches include incrementoperators input iso kernel km lazy linked linkedlist linux linuxsegmentationfault list lists locate logical_drives looping loopinsideloop. lowest match matrix microsoft motherboard mysql number opendocumentformat opensource owf pattern pdf performance pointer posix problem probleminc process program programming radix recursion recv research reversing scanf scripting segmentationfault sequential shape socket socketprograming spoonfeeding standard string strings structures student systemcall testing threads turboc unix user variable voidmain() wab windows.h windowsapi






