Jump to content
xisto Community
Sign in to follow this  
kvarnerexpress

Dumping Fields Into Variables

Recommended Posts

have a file that we are supposed to use as input. It looks like this:Code:name:curriculum:gradyear:ssn:dobI'm going to read it in a buffer, but trying to find the logic to seperate the fields and then place in a variable. I was thinking something like:Code:if(ispunct(buf[x]) != ':') strcpy(s->name, buf);Any suggestions is greatly appreciated!

Share this post


Link to post
Share on other sites

have a file that we are supposed to use as input. It looks like this:

Code:

name:curriculum:gradyear:ssn:dob

I'm going to read it in a buffer, but trying to find the logic to seperate the fields and then place in a variable. I was thinking something like:

Code:

if(ispunct(buf[x]) != ':')

  strcpy(s->name, buf);

Any suggestions is greatly appreciated!

53011[/snapback]


You can use the strtok() function.

As a pseudocode example:

char *aux;

/* Buffer contains name:curriculum:gradyear:ssn:dob*/

aux = buf;

/* i suppose s is a structure with the fields name, curriculum ..... and the memory for this fields has been already alloced, if not use strdup instead of strcpy */

strcpy (s->name,strtok(aux,':'));

strcpy (s->curriculum,strtok(NULL,':'));

strcpy (s->gradyear,strtok(NULL,':'));

strcpy (s->ssn,strtok(NULL,':'));

strcpy (s->dob,strtok(NULL,':'));

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.