1

怎么了?我很确定我的语法是正确的,因为它没有警告。另外它不会过去getc(document);我尝试了fgetc(document);相同的结果。我没有得到什么?(我曾经printf("$");看到它崩溃的地方)

char temp[51];
int cntr = 0,listcntr = 0,buffer;
FILE *document;
contact list[MAXCONTACTS];
document = fopen("addressbook.txt","r");
do
{
    for(cntr = 0;cntr < 51; cntr++)
        temp[cntr] = '\0';
    cntr = 0;
    do
    {

        buffer = getc(document);
         printf("$");
        if(buffer != '\t')
            temp[cntr] = buffer;
        ++cntr; 
    }while(buffer != '\t'&& buffer != EOF);
list[listcntr].name = temp;
4

1 回答 1

1

好的,我稍微修改了您的代码并添加了一些检查,也许就足够了。

char temp[51];
int listcntr = 0,buffer;
contact list[MAXCONTACTS];
FILE *document = fopen("addressbook.txt","r");

if (NULL == document){
    //exit here
}

for(int i = 0;i < 51; i++){
    temp[i] = '\0';
}

int cntr = 0;
for (int cntr=0; cntr < 51 && !feof(document); cntr++){
    buffer = getc(document);
    if (buffer == '\t'){
        break;
    }
    temp[cntr] = buffer;
}

if(listcntr < MAXCONTACTS){
    // you should probably copy temp here
    list[listcntr].name = temp;
}
于 2012-09-12T10:00:09.347 回答