2011-12-10 63 views
0

我试图从具有逗号读取文件分隔值,像这样(第5场应该只有R/B/n和第6场Y/N:C语言编程的fscanf分割错误

531,A,10,10,b,n,96.00,100.00 
531,B,15,15,b,n,144.00,0.00 
531,C,20,20,b,n,192.00,0.00 
533,A,11,11,r,n,123.20,100.00 
533,B,22,22,r,n,246.40,0.00 

我有以下的fscanf码块解析由线和存储值的文件中,我改变/输出以后的阵列。

while(fscanf(data, "%4[0-9],%[A-Z],%3[0-9],%3[0-9],[prb],[yn],%d,%d\n", &house[i], room[i], &length[i], &width[i], paintcode[i], ceilingcode[i], &cost[i], &setupcost[i]) != EOF) { 
    printf("%d,%c,%d,%d,%c,%c,%.2lf,%.2lf\n", &house[i], room[i], &length[i], &width[i], paintcode[i], ceilingcode[i], &cost[i], &setupcost[i]); 
    i++; 
} 

不幸的是,我得到一个分段故障和它发生在这条线。我已经改变并测试了大约一个小时,我很肯定我没有错,除非我错过了一些兴。任何帮助将不胜感激,完整的主要功能粘贴在下面。

main() { 
    FILE *data; 
    int house[200]; 
    char room[200]; 
    int length[200]; 
    int width[200]; 
    char paintcode[200]; 
    char ceilingcode[200]; 
    double cost[200]; 
    double setupcost[200]; 
    char line; 
    int MAX_BUFF = 200; 
    char x[200]; 
    double price[100]; 
    int i = 0, num_records = 0; 

    data = fopen("quotes.data","r"); 
    if(data == NULL){ 
     printf("Error: file can't be open..\n"); 
    } else { 
     while(fscanf(data, "%4[0-9],%[A-Z],%3[0-9],%3[0-9],[prb],[yn],%d,%d\n", &house[i], room[i], &length[i], &width[i], paintcode[i], ceilingcode[i], &cost[i], &setupcost[i]) != EOF) { 
     printf("%d,%c,%d,%d,%c,%c,%.2lf,%.2lf\n", &house[i], room[i], &length[i], &width[i], paintcode[i], ceilingcode[i], &cost[i], &setupcost[i]); 
     i++; 
     } 
     fclose(data); 
+0

请至少写入C89,最好是C99--在上下文中,这意味着指定一个返回类型(main()为'int')和参数列表,并指向'int main(void)'。 –

+0

请注意,您的循环将空间的第一个字符串放置在字符串中的偏移量0处;第二个字符串从房间数组中的偏移量1开始。您的打印(或多或少)与此保持一致,因此它可以正常工作,但是您逐渐将自己留在越来越少的空间来存放琴弦。阅读200个参赛作品,你到处乱涂乱画。 –

回答

1

有相当这里的几个问题。

一开始,该scanf家庭只会返回EOF如果读失败没有得到任何输入值。如果您成功获得一些,但不是全部,您将获得成功扫描的数量(包括您获得全部数据的情况)。

您还需要通过要填充的变量的地址,这些变量尚未完成所有变量。

此外,格式说明像%4[0-9]将读取字符,不是一个整数,从而你有它需要的字符缓冲区,而不是一个整数的地址。如果你想要一个整数填充,你需要使用像%4d

%[A-Z]前面,这些实际上创建了一个以null结尾的字符串,因此您需要为此留出空间。

此外,请确保您可以读取行末尾的浮点数而不是整数,并且您缺少一些%格式字符。

最后,printf只需要地址字符串参数。

以下程序显示了这个在简化形式的行动(非数组)与输入文件:

#include <stdio.h> 

int main (void) { 
    FILE *data; 
    char room[2], paintcode[2], ceilingcode[2]; 
    int house, length, width; 
    double cost, setupcost; 

    data = fopen("quotes.data","r"); 
    if (data == NULL) { 
     printf("Error: file can't be opened.\n"); 
    } else { 
     while (fscanf (data, "%4d,%[A-Z],%3d,%3d,%[prb],%[yn],%lf,%lf\n", 
      &house, room, &length, &width, paintcode, ceilingcode, &cost, 
      &setupcost) == 8) 
     { 
      printf ("%d,%c,%d,%d,%c,%c,%.2lf,%.2lf\n", 
       house, *room, length, width, *paintcode, *ceilingcode, 
       cost, setupcost); 
     } 
    } 
    fclose(data); 
    return 0; 
} 
+0

感谢您的回复, – user1022659

+0

感谢您的帮助,您的措辞完美地解释了它,并且它的工作非常精美,现在只是解析阵列。 – user1022659

0

检查scanf函数的返回值正确

RETURN VALUE 
    These functions return the number of input items successfully matched and 
    assigned, which can be fewer than provided for, or even zero in the event 
    of an early matching failure. 
1

你需要采取的地址(如&house[i])只有当你将参数传递给fscanf。的fprintf整数和单字符参数是按值传递:

printf("%d,%c,%d,%d,%c,%c,%.2lf,%.2lf\n", house[i], room[i], length[i], width[i], paintcode[i], ceilingcode[i], cost[i], setupcost[i]); 

呼叫至fscanf,而另一方面,需要指针。由于您将单字符扫描为字符,而不是字符串,因此您应该传递房间地址,颜色代码等。

+0

+1:这是坠机的关键。 –

+0

谢谢,寻求帮助。它给了我一些错误,打印出fscanf的结果以及你解释它的方式使它点击。 – user1022659