繁体   English   中英

C 中的 fscanf function 存在问题

[英]Having issues with the fscanf function in C

因此,受文件指针的启发,我想创建自己的“杂货店”。 在这里,我想扫描我的产品价格,但我不断收到分段错误

int main(void)
{
FILE* fuf; 
fuf = fopen("apranqner.csv", "r");

printf("Price: ");

int price[15];

/*  I only want to print the first row of the table  

    3800.0, Butter-, Dairy Farm, 55
    500.0, Milk-, Dairy Farm, 60
    600.0, Tan-, Dairy Farm, 11
    450.0, Matsoon-, Dairy Farm, 6
    550.0, SourCream-, Dairy Farm, 8
    2800.0, Cheese-, Dairy Farm, 7
    250.0, Bread-, Bakery Goods, 45
    400.0, Cake-, Bakery Goods, 5
    700.0, Pringles-, Bakery Goods, 10
    100.0, Apache-, Bakery Goods, 11
    400.0, Coca-Cola-, Beverage Island, 12
    400.0, Pepsi-, Beverage Island, 12
    400.0, Fanta-, Beverage Island, 1
    250.0, Jermuk-, Beverage Island, 4
    100.0, 
*/

while (fscanf(fuf, "Price: %f ", price) != EOF)
{
    printf("%f", *price);
}   

fclose(fuf);
}

如果你真的想在这里使用fscanf是一个可能的解决方案。 请注意,您必须使用特殊的字符串匹配语法才能扫描字符串,因为字符串具有非字母字符,例如-或空格:

#include <stdio.h>

int main(void)
{
  FILE* fuf; 
  float val1;
  char desc1[20];
  char desc2[20];
  int val2;
  int rc;

  fuf = fopen("apranqner.csv", "r");
  if (fuf == NULL)
  {
        perror("fopen");
        return 1;
  }

  rc = fscanf(fuf, "%f, %[A-Za-z-], %[A-Za-z ], %d ", &val1, desc1, desc2, &val2) ;
  if (rc == 4)
  {
    printf("First row\n");
    printf("field 1: %f \n", val1);
    printf("field 2: %s \n", desc1);
    printf("field 3: %s \n", desc2);
    printf("field 4: %d \n", val2);
  }  
  else
  {
    printf("Error: only %d tokens scanned\n", rc);
    return 1;
  } 

  fclose(fuf);
  return 0;
}

执行:

cat apranqner.csv 
3800.0, Butter-, Dairy Farm, 55
./fscanf
First row
field 1: 3800.000000 
field 2: Butter- 
field 3: Dairy Farm 
field 4: 55

这是使用存储在数组中的结构的另一个版本的代码:

#include <stdio.h>
#include <stdlib.h>

#define MAX     1
#define LENGTH  20

int main(void)
{

  typedef struct {
    float price;
    char* product;
    char* department;
    int stock;
  } pro;


  FILE* fuf;
  pro menu[MAX];
  int rc;

  fuf = fopen("apranqner.csv", "r");
  if (fuf == NULL)
  {
        perror("fopen");
        return 1;
  }

  menu[0].product = malloc(LENGTH * sizeof (char));
  if (menu[0].product == NULL)
  {
        perror("malloc");
        return 1;
  }
  menu[0].department = malloc(LENGTH * sizeof (char));
  if (menu[0].department == NULL)
  {
        perror("malloc");
        return 1;
  }

  rc = fscanf(fuf, "%f, %[A-Za-z-], %[A-Za-z ], %d ",
                   &menu[0].price,
                   menu[0].product,
                   menu[0].department,
                   &menu[0].stock);

  if (rc == 4)
  {
    printf("First row\n");
    printf("price: %f \n", menu[0].price);
    printf("product: %s \n", menu[0].product);
    printf("dept. : %s \n", menu[0].department);
    printf("stock: %d \n", menu[0].stock);
  }
  else
  {
    printf("Error: only %d tokens scanned\n", rc);
    return 1;
  }

  fclose(fuf);
     return 0;
}
                                                                                                         
                                                                                                      

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM