if i have a list kinda like this
"name"
bobby
"endname"
"age"
34
"endage"
"name"
susie
"endname"
"age"
53
"endage"

how would i sort through it ignoring the stuff in "" and copying just the names and ages into arrays. ive been using strtok and if statements but that doesnt seem very efficient.
is there a simple way to do this?

If statements are needed because some comparisons involving the row of char's is needed.

There is a simple way, to do it though. (Loose code to emphasize logic)

char buffer[100]; //nice and big :)

fgets(buffer, sizeof(buffer), filePointer); //put the whole row into the buffer

if(first char in buffer is < '0' || the first char in buffer is > '9') 
   //It's a word or string of some kind
   //so handle that, here
else 
   sscanf(buffer, "%d", &number); //not a word, so get it into your number variable

That's one of the beauties about fgets(), btw. ;)

And welcome to the forum, Nadleeh! ;)

Edited 5 Years Ago by Adak: n/a

thanks!:) i forgot to mention its reading from a file. i have it in a buffer already.. i just dont get how do get it to ignore the stuff in ""

void readFile( char FILENAME[]);

int main(int argc, char * argv [])
{
   char FILENAME[20];
//  int n;
// for (n = 0; n < argc; n ++)
   strcpy(FILENAME, argv[1]);

   readFile(FILENAME);

return 0;   
}
void readFile ( char FILENAME[])
{
 FILE * fp;
 char buffer [81];
 char nameList[40][81];
 int i = 0;

   if ( ( fp = fopen ( FILENAME, "r"))== NULL)
   {
       fprintf ( stderr, " cant open %s \n", FILENAME );
       exit (1);
   } 

   fgets ( buffer, 81 , fp );   // priming read
   while ( ! feof (fp))
   {
      strcpy (nameList[i],buffer);
      fgets ( buffer, 81, fp);
      i++;
   } 

fclose ( fp);

int j;
for (  j=0; j<39; j++)
 printf ("%s",nameList[j]);

}

this is what i have atm... i want namelist to just hold names and not the tags

Edited 3 Years Ago by Nick Evan: Fixed formatting

Oh! Even easier, since all the tags begin with a double quote. Test for that double quote at buffer[0]. If you want the numbers as well, then use logic like my previous post has.

And don't worry about the rest of the char's that you don't want, being in the buffer. They will be over-written in the next call to fgets(), anyway.

Edited 5 Years Ago by Adak: n/a

im kinda new to this.. how exactly would i test for whats in the array?

This is the general idea, (from my first post):

char buffer[100]; //nice and big :)

fgets(buffer, sizeof(buffer), filePointer); //put the whole row into the buffer

if(first char in buffer is < '0' || the first char in buffer is > '9') 
   //It's a word or string of some kind
   //so handle that, here
else 
   sscanf(buffer, "%d", &number); //not a word, so get it into your number variable

Do you want JUST the names, or do you want the names and the ages, saved? One post says both, one post says just the names.

No, this is not like using strtok(), at all. It does get names (and ages if you want them), but it doesn't use the same logic as strtok().

ok i will! one more quick question. im trying to copy each person and their info into a struct. i know i need to have a nested loop but for some reason its not displaying anything

Can't help much with code, if I can't see it -- please post it, and please use [code]
tags around it.

oh sorry... ok this is what doesnt work. right now im just tryna test it out. displaying the contents of nameList works but when i try to cpy it into the struct it displays nothing.

int j;
struct userinfo user[i];
for ( j=0; j<i; j++)
{
 strcpy (user[j].firstName, nameList[j]);
 printf ("%s ",user.firstName[j]);
}

Edited 5 Years Ago by nadleeh: n/a

What is the size of i in this code?

Why aren't you using a macro for you size of the array?

For static arrays:

#define SIZE 20  //note: no semi-colon on the end of this line!

int main(void) {
  char stringArray[SIZE][40];  //your array name, of course, not stringArray
  
  //your other code in here
  return 0;
}

Something like this ^^^^

Also, please post the struct declaration, so I can see what firstname really is.

Edited 5 Years Ago by Adak: n/a

This article has been dead for over six months. Start a new discussion instead.