reading from file

N

New

I am having trouble reading from a file I open the file in read mode I have
data formated like name~address~age but when I read it in and print it out I
get name coming out as name~address~age and address and age are both empty

How can I read the data in to split it into the three different strings?
Is it possible to do it in a while?

Thank you
 
A

Allan Bruce

New said:
I am having trouble reading from a file I open the file in read mode I have
data formated like name~address~age but when I read it in and print it out I
get name coming out as name~address~age and address and age are both empty

How can I read the data in to split it into the three different strings?
Is it possible to do it in a while?

Thank you

have a look at sscanf options, particluarly [^~] to read in a string
terminated by a '~'
Allan
 
A

Al Bowers

New said:
I am having trouble reading from a file I open the file in read mode I have
data formated like name~address~age but when I read it in and print it out I
get name coming out as name~address~age and address and age are both empty

How can I read the data in to split it into the three different strings?
Is it possible to do it in a while?

It appears you are not parsing correctly. You would need to provide
your code to detect the problem.

There are several ways you can do the parsing. Yes, you can use a loop
using, for example, function strtok.

I would prefer the use of a *scanf function using the scanset [^-].
Example:

#include <stdio.h>

#define MAXSTR 64

int main(void)
{
char *test = "George Washington-122 Pennsylvania Ave\n"
"Washington DC-46";
char name[MAXSTR+1],addr[MAXSTR+1],age[MAXSTR+1];

if( 3 == sscanf(test,"%64[^-]-%64[^-]-%"
"64[^-\n]",name,addr,age))
printf("NAME\n%s\n\nADDRESS\n%s\n\nAGE\n%s\n",name,addr,age);
else puts("Parsing failure");
return 0;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,141
Messages
2,570,818
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top