2011-06-07 77 views
0
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引发的异常类EAccessViolation与消息....................(borland C++)从文件中拆分文本

+0

编译时不会出现该错误,您在运行时会得到该错误。请学会准确。 – 2011-06-07 16:04:00

回答

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。这是令人厌烦的工作 - 也许你可以在发布之前花更多的精力去追逐这些东西。你将学到更多的方式。

+0

根据你的意见改变,但是错误信息仍然是 Project1.exe引发异常类EAccessViolation with Message ....................(borland C++) – hkvega01 2011-06-07 15:59:17

+0

+ 1好抓。我误解了“文件名”部分。它实际上是一个可以分配给它的指针。 – cnicutar 2011-06-07 15:59:21

+0

@ hkvega01这不是编译器错误。 – cnicutar 2011-06-07 15:59:51