| | |
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
* adobe api append array arrays bash binarysearch char character cm copyanyfile copypdffile createcopyoffile createprocess() csyntax database directory dynamic execv feet fgets file floatingpointvalidation fork frequency function getlogicaldrivestrin givemetehcodez global gtkgcurlcompiling gtkwinlinux highest histogram homework i/o ide include infiniteloop initialization input interest intmain() iso keyboard kilometer lazy license linked linkedlist linux list lowest matrix meter microsoft mqqueue multi mysql oddnumber odf open openwebfoundation overwrite pause pdf pointer pointers posix power program programming pyramidusingturboccodes read recursion recv recvblocked repetition reversing scheduling segmentationfault send single socketprogramming spoonfeeding stack standard strchr string student suggestions system test testautomation unix urboc user whythiscodecausesegmentationfault win32api windows.h






