【问题标题】:writing to a struct with a struct用结构写入结构
【发布时间】:2014-12-12 08:13:03
【问题描述】:

我正在使用 fscanf 读取一个 txt 文件。

for (int i = 0; i < totalLines; ++i)
{
    fscanf(fp,"%s %s %f %f[^\n]", &cp[i].name, &cp[i].animal, &cp[i].coordinates.lat, &cp[i].coordinates.lng);
    printf("Name: %s\nAnimal: %s\nLat: %.6f\nLong: %.6f\n\n", cp[i].name, cp[i].animal, cp[i].coordinates.lat, cp[i].coordinates.lng);  
}

问题在于它没有设置/打印最里面的结构成员或变量,它们是 2 个双精度 lng 和 lat 没有被分配。完成这项工作的最佳方法是什么?我在正确的轨道上吗?我试图用 (double) 来投射它,但这是通过错误来实现的。有什么想法吗?

更新

我已将代码更新为应有的样子,但我仍然得到一个 0 值。任何想法为什么?

【问题讨论】:

  • 我已经更新了我的代码,但仍然没有运气。它看起来像双重不想在那里。
  • 显示结构声明。
  • 你能看出&amp;cp[i].name, &amp;cp[i].animal,cp[i].name, cp[i].animal,的区别吗

标签: c struct header-files scanf


【解决方案1】:

考虑到你的结构如下:

/* Structure Holding Coordinates */
typedef struct location                                                         
{                                                                               
    double lat;                                                             
    double longt;                                                           
}location_t;                                                                    


typedef struct geo                                                              
{                                                                               
    char name[64];                                                              
    char animal[64];                                                            
    location_t loc;                                                        
} geo_t;                                                                        

你的阅读应该是这样的:

geo_t arr[NO_ENTRIES];     /* NO_ENTRIES is total lines in file */                                                     
for(i = 0; i< NO_ENTRIES; i++)                                              
    fscanf(fp, " %s %s %lf %lf", arr[i].name, arr[i].animal, &arr[i].loc.lat, &arr[i].loc.longt);

printf("After Reading\n\n\n");                                              
for(i = 0; i< NO_ENTRIES; i++)                                              
    printf(" %s %s %f %f\n", arr[i].name, arr[i].animal, arr[i].loc.lat, arr[i].loc.longt);

在这里查看:http://ideone.com/99dmvL

【讨论】:

    【解决方案2】:

    对于 double,请使用 %lf,就像您在 printf 中所做的一样。所以把代码改成:

    fscanf(fp,"%s %s %lf %lf", cp[i].name, cp[i].animal, &cp[i].coordinates.lat, &cp[i].coordinates.lng);
    

    【讨论】:

      【解决方案3】:

      当你试图读取长双精度值时,你应该使用 %lf 而不是 %s 来扫描双精度值 示例:fscanf(fp,"%lf",&double_value);

      【讨论】:

        【解决方案4】:

        使用 %f 而不是 %s 表示双精度值。

        fscanf(fp,"%s %s %f %f", cp[i].name, cp[i].animal, &cp[i].coordinates.lat, &cp[i].coordinates.lng);
        

        你的 printf 也会出错。我建议你这样做:

        printf("Name: %s\nAnimal: %s\nLat: %f\nLong: %f\n\n", cp[i].name, cp[i].animal, cp[i].coordinates.lat, cp[i].coordinates.lng);
        
        • 不要使用"%lf"。你的变量是双精度的,所以你应该使用"%f"
        • 在您的 printf 调用中,如果需要,请使用如下格式说明符:"%.5f"。它告诉 printf 输出小数点后 5 位。

        您也可以参考fscanffprintf 文档。

        【讨论】:

        • 感谢您的帮助,但是当我打印它们时,值仍然是 0.0000。
        • 我希望您的结构声明似乎是问题所在。你能在这里发布结构声明吗?
        【解决方案5】:

        您必须使用浮点控制字符串来获取值。不是字符串控制字符串。

         fscanf(fp,"%s %s %lf %lf", cp[i].name, cp[i].animal, &cp[i].coordinates.lat, &cp[i].coordinates.lng);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-08-18
          • 2021-10-23
          • 2016-12-01
          • 2018-03-16
          相关资源
          最近更新 更多