I am thinking of using
fscans(??)
to get a line at a time but how do i "tokenize" that string ??
thanks for your time and understanding,
bill
I am thinking of using
fscans(??)
to get a line at a time but how do i "tokenize" that string ??
thanks for your time and understanding,
bill
i got fgets to work and it displays the .dat file perfectly but,
i need to have lines that begin with // to be skipped.
i also need to tokenize the buffer[] (is strtok() as bad as people on here say ??)
any assistance is greatly appreciated.
thanks,
bill
#include <stdio.h>
FILE *fp1;
void Load_events()
{
//char logon[20] ;
//int term;
//double time;
//int user;
char buffer[1000];
printf("before logon.dat opened\n");
fp1 = fopen("LOGON.DAT","r");
if (fp1 == NULL) // Invalid file
printf("Invalid file\n");
while(fgets (buffer,1000, fp1) != NULL)
{
printf ("%s", buffer);
}
fclose(fp1);
printf("\nafter logon.dat closed\n");
}
main(int argc, char *argv[])
{
Load_events();
}
fgets() works and the data prints out exactly like it is in the .dat file.
Now, i need to parse the buffered string.
i tried using some code i got from a search on this board,
(seps[], strtok(), all // ed below, but the first token from the buffered list that i get is garbage and i get compile errors on the code below that.
any assistance is greatly appreciated!!
thanks,
bill
Code:#include <stdio.h> #include <string.h> FILE *fp1; void Load_events() { // char logon[20]; //char *logptr ; //double time; //int user; // int term; char buffer[1000] ; //char seps[] = " \n,( )"; // logptr = logon; printf("before logon.dat opened\n"); fp1 = fopen("LOGON.DAT","r"); // for "filename", insert a file or var if if (fp1 == NULL) // Invalid file printf("Invalid file\n"); while(fgets (buffer,1000, fp1) != NULL) { if ( strncmp( buffer, "//", 2 ) == 0 ) continue; // skip comment else { // logptr = strtok( buffer, seps ); /* Find first token*/ // term = strtok( buffer, seps ); /* Find first token*/ // user = strtok( buffer, seps ); /* Find first tok // time = strtok( buffer, seps ); /* Find first token*/ // printf ("logon = %s\nterm = %d\nuser = %d\ntime = %f\n", logon,term,user,time); }// end of else } fclose(fp1); printf("\nafter logon.dat closed\n"); } main(int argc, char *argv[]) { Load_events(); }
Last edited by billh; 09-04-2002 at 06:47 PM.
this is what logon.dat looks like
Code:// LOGON.DAT // // terminal no. user id time (in seconds) LOGON 7 3 21 LOGON 3 1 4 LOGON 2 4 212 LOGON 4 2 5
sscanf is your friend.
sscanf( buf, "%s%d%d%d" ... )
strtok is horrible, IMO.
Quzah.
Hope is the first step on the road to disappointment.
>strtok is horrible, IMO.
Horrible for the unwary maybe, but the only real problems strtok has is that it modifies the original argument and is not re-entrant. Being standard and relatively easy to use usually out-weighs those problems. What are your arguments against it?
-Prelude
My best code is written with the delete key.
> What are your arguments against it?
The fact that it modifies the string passed it, and it ugly as sin. I mean, really, couldn't they have come up with something better than "all additionall calls take NULL as the argument"?
That's just ugly code. Yeah, I know that's how it works, but looking at the code and trying to figure out what it does just by looking at the function itself:
ptr1 = myfunction( string_to_modify );
ptr2 = myfunction( NULL );
ptr3 = myfunction( NULL );
Just looking at those three lines, you're left scracthing your head going "WTF?".
It's ugly code. That's my main complaint.
Quzah.
Hope is the first step on the road to disappointment.
>It's ugly code. That's my main complaint.
I agree with you on that, but you also have to consider that since it is relatively well-known there isn't generally a problem with not understanding the code, and even if it is ugly, the usefulness and standardization of the function make up for this IMHO.
If you had the chance, how would you redesign strtok? Here is an old(reworked to remove icky things like void main) implementation I wrote when I was in school...be gentle :
-PreludeCode:#include <stdio.h> #include <string.h> static char *make_tok ( char *dst, char *src, char *delim ) { char *from = src; char *to = dst; while( *from != '\0' ){ if ( strchr ( delim, *from ) != NULL ) break; *to++ = *from++; } *to = '\0'; return ( *dst != '\0' ) ? ++from : NULL; } int main ( void ) { char buf[100]; char *str = "my:string:tokenizing:function"; char *remainder = str; while ( ( remainder = make_tok ( buf, remainder, ":" ) ) != NULL) printf ( "%s\n", buf ); return 0; }
My best code is written with the delete key.