Can printf change its parameters??

Posted by martani_net on Stack Overflow See other posts from Stack Overflow or by martani_net
Published on 2010-04-18T15:09:03Z Indexed on 2010/04/18 16:23 UTC
Read the original article Hit count: 375

Filed under:
|

EDIT: complete code with main is here http://codepad.org/79aLzj2H

and once again this is were the weird behavious is happening

for (i = 0; i<tab_size; i++)
{
  //CORRECT OUTPUT
  printf("%s\n", tableau[i].capitale);
  printf("%s", tableau[i].pays);
  printf("%s\n", tableau[i].commentaire);
  //WRONG OUTPUT
  //printf("%s --- %s --- %s |\n", tableau[i].capitale, tableau[i].pays, tableau[i].commentaire);
}


I have an array of the following strcuture

struct T_info
{
    char capitale[255];
    char pays[255];
    char commentaire[255];
};

struct T_info *tableau;

This is how the array is populated

int advance(FILE *f)
{
  char c;

  c = getc(f);
  if(c == '\n')
    return 0;

  while(c != EOF && (c == ' ' || c == '\t'))
  {
    c = getc(f);
  }

  return fseek(f, -1, SEEK_CUR);

}

int get_word(FILE *f, char * buffer)
{
  char c;
  int count = 0;
  int space = 0;

  while((c = getc(f)) != EOF)
    {
      if (c == '\n')
      {
    buffer[count] = '\0';
    return -2;
      }

      if ((c == ' ' || c == '\t') && space < 1)
      {
    buffer[count] = c;
    count ++;
    space++;
      }
      else
      {
    if (c != ' ' && c != '\t')
    {
      buffer[count] = c;
      count ++;
      space = 0;
    }       
    else /* more than one space*/
    {
     advance(f);
     break;
    }
      }
    }

   buffer[count] = '\0';
   if(c == EOF)
     return -1;

   return count;
}

void fill_table(FILE *f,struct T_info *tab)
{
    int line = 0, column = 0;

    fseek(f, 0, SEEK_SET);

    char buffer[MAX_LINE];
    char c;
    int res;

    int i = 0;
    while((res = get_word(f, buffer)) != -999)
    {
      switch(column)
      {
        case 0:
        strcpy(tab[line].capitale, buffer); 
        column++;
          break;
        case 1:
          strcpy(tab[line].pays, buffer);
          column++;
          break;
        default:
          strcpy(tab[line].commentaire, buffer);    
          column++;
          break;
      }
      /*if I printf each one alone here, everything works ok*/
          //last word in line
      if (res == -2)
      {
        if (column == 2)
        {
          strcpy(tab[line].commentaire, " ");       
        }
//wrong output here
        printf("%s -- %s -- %s\n", tab[line].capitale, tab[line].pays, tab[line].commentaire);

        column = 0;
          line++;
          continue;
      }
      column = column % 3;

      if (column == 0)
      {
        line++;
      }

      /*EOF reached*/
      if(res == -1)
        return;

    }
    return ;
}

Edit :

trying this

    printf("%s -- ", tab[line].capitale);
    printf("%s --", tab[line].pays);
    printf("%s --\n", tab[line].commentaire);

gives me as result

 --  --abi  -- Emirats arabes unis

I expect to get

Abu Dhabi -- Emirats arabes unis --

Am I missing something?

© Stack Overflow or respective owner

Related posts about c

    Related posts about printf