Question

Whats wrong? I'm pretty sure my syntax is correct since it has no warnings. Plus it won't go past getc(document); I tried fgetc(document); same result. What am I not getting here? (I used printf("$"); to see where it crashes)

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;
Was it helpful?

Solution

OK I modified your code a little and added a couple of checks, maybe it will be enough.

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;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top