从文件中分割文本

时间:2011-06-07 15:49:31

标签: c

struct recordNode {
   char district[50];
   int employees;
   int employers;
   int students;
   int retried;
   int others;
   struct recordNode* left;
   struct recordNode* right;
};

FILE *getFile (char filename[]) {
   struct recordNode* node;

   FILE* fpin;
   FILE* fpout;

   char line_buffer[lineSize]; /* BUFSIZ is defined if you include stdio.h */
   int counter = 0;
   filename = "testData.txt";

   //file validation
   fpin=fopen("testData.txt", "r");

   if (fpin == NULL ) exit(0);
        counter = 0;
    while (fgets(line_buffer, sizeof(line_buffer), fpin)) { /* same as while (feof(in) != 0) */
               counter++;
               if (counter != 0) {
                //Central & Western - Chung Wan,7576,1042,2156,1875,3154 (sample data)
               sscanf(line_buffer, "%s,%d,%d,%d,%d", node->district, node->employees, node->students, node->retried, node->others);
               printf("%s", node->district); **//error**
               }


               getchar();



    }
        getchar();

   return fpout;
}

编译时出错,我的代码出了什么问题?有一条错误消息 项目Project1.exe使用消息....................(borland C ++)

引发了异常类EAccessViolation

1 个答案:

答案 0 :(得分:3)

您的问题(或至少其中一个)在这里:

sscanf(line_buffer, "%s,%d,%d,%d,%d", node->district, node->employees, 
                      node->students, node->retried, node->others);

除了第一个参数之外的所有参数都应该是指针:

sscanf(line_buffer, "%s,%d,%d,%d,%d", node->district, & node->employees, 
                      & node->students, & node->retried, & node->others);

另外,这个:

filename = "testData.txt";

根本不应该存在 - 要读取的文件的名称作为参数传递给函数。

最后,这个:

 struct recordNode* node;

应该是:

 struct recordNode node;

您需要将node->district之类的内容更改为node.district。这是令人厌倦的工作 - 也许你可以在发布这里之前花更多的精力来追逐这些东西。你会从中学到更多。