New wrote:
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;
}
--
Al Bowers
Tampa, Fl USA
mailto:
xa******@myrapidsys.com (remove the x to send email)
http://www.geocities.com/abowers822/