代码之家  ›  专栏  ›  技术社区  ›  0xFF

printf能改变它的参数吗?

  •  2
  • 0xFF  · 技术社区  · 14 年前

    编辑: 这里有完整的main代码 http://codepad.org/79aLzj2H

    for (i = 0; i<tab_size; i++)
    {
      //CORRECT OUTPUT
      printf("%s\n", tableau[i].capitale);
      printf("%s\n", 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);
    }
    


    我有一系列的

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

    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 ;
    }
    

    编辑:

    试试这个

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

    结果给了我

     --  --abi  -- Emirats arabes unis
    

    Abu Dhabi -- Emirats arabes unis --
    

    我遗漏了什么吗?

    4 回复  |  直到 14 年前
        1
  •  5
  •   sepp2k    14 年前

    printf有副作用吗?

    好吧,它印在屏幕上。那是副作用。除此之外:没有。

    printf是否正在更改其参数

    如果错误的结果意味着输出没有在它应该出现的时候出现,那么这可能只是一个行缓冲问题(第二个版本没有打印换行符,这可能会导致输出没有被刷新)。

        2
  •  1
  •   Mike McNertney    14 年前

    printf不太可能是你的问题。更可能的是,你破坏了内存,而printf的奇怪结果只是一个症状。

    我在您的代码中看到了几个地方,它们可能导致读取或写入超过数组的末尾。很难说在没有看到你的意见的情况下,他们中的哪一个可能会给你带来问题,但我注意到以下几点:

    1. get_lines_count 如果最后一行不以换行结尾,则不计算最后一行,但您的其他方法将处理该行
    2. advance 如果换行符前面有空格,将跳过换行符,这将导致基于列的处理停止,并可能导致某些字符串未初始化
    3. get_word 不做任何边界检查 buffer

    可能还有其他人,就是那些突然向我扑来的人。

        3
  •  0
  •   Juliano    14 年前

    也许您发布的代码仍然不完整(fill_table()从get_word()中查找-999个幻数,但是get_word()永远不会返回该值),您的主函数丢失,因此我们不知道您是否正确分配了内存,等等。

    fseek 在文本文件中。你可能想用 ungetc 相反,在这种情况下。如果在读取文本流时确实要移动文件指针,则应使用 fgetpos fsetpos .

    你寻求帮助的方法是错误的。您甚至在不理解代码的情况下就假设printf有副作用。问题显然不在printf中,但您不必要地保存了信息。你的代码不完整。您应该创建一个简化的测试用例,它可以编译并清楚地显示您的问题,并将其完整地包含在您的问题中。如果你不理解程序的真正错误,不要责怪随机库函数。问题可能在任何地方。

        4
  •  0
  •   GManNickG    14 年前

      printf("%s\n", tableau[i].capitale);
      printf("%s", tableau[i].pays);
      printf("%s\n", tableau[i].commentaire);
    

    然后一切正常。。。

    Line no. 173 在里面 http://codepad.org/79aLzj2H )

      printf("%s\n %s %s /n", tableau[i].capitale, tableau[i].pays, tableau[i].commentaire);