CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 2013
    Posts
    1

    Access violation when using pointers to read data

    I have the following method to read in a data file using a struct and a doubly linked list.

    The readFile method is as follows:

    Code:
    struct contact *readFile(char * FName,struct contact **ptrList)
    {
    
    struct contact *head, *newContact;
    FILE *fptr;
    char oneLine[60];
    char *sname, *fname, *phone,*company, *email;
    
    head = *ptrList;
    
    fptr = fopen(FName,"r");
    
    if(fptr == NULL)
    {
    	printf("\nCant open file!");
    	return(ptrList);
    
    }
    
    fgets(oneLine, 55, fptr);
    while(!feof(fptr))
    {
    	fgets(oneLine, 55, fptr);
    	if(oneLine[strlen(oneLine)-1] == '\n')
    	{
    		oneLine[strlen(oneLine)-1] = '\0';
    
    	}
    
    	sname = strtok(oneLine,",");
    	fname = strtok(NULL,",");
    	phone = strtok(NULL,",");
    	company = strtok(NULL,",");
    	email = strtok(NULL,",");
    
    	if(head == NULL)
    	{
    		head = (struct contact *)malloc(sizeof(struct contact));
    		*ptrList = head;
    		strcpy(head->sname,sname);
    		strcpy(head->fname,fname);
    		strcpy(head->phone,phone);
    		strcpy(head->company,company);
    		strcpy(head->email, email);
    
    		head->prev = NULL;
    		head->next = NULL;
    
    
    	}
    	else
    	{
    
    		head->next = (struct contact *)malloc(sizeof(struct contact));
    		head = head->next;
    		head->next = NULL;
    		//copy the data to the new one
    		strcpy(head->sname,sname);
    		strcpy(head->fname,fname);
    		strcpy(head->phone,phone);
    		strcpy(head->company,company);
    		strcpy(head->email,email);
    
    		
    
    	}
    
      }//end while
    
      fclose(fptr);
      return(head);
    }
    
    int writeListToFile(char * theFile, struct contact *theList)
    {
    	struct contact * curr;
    	FILE *fptr;
    
    	fptr = fopen(theFile,"w");
    	if (fptr == NULL)
    	{
    		printf("\noops");
    		return(0);
    
    	}
    
    	fprintf(fptr, "Surname, FirstName, Phone, Company, Email");
    	curr = theList;
    	while(curr != NULL)
    	{
    		fprintf(fptr,"\n%s,%s,%s,%s,%s",curr->sname,curr->fname,curr->phone,curr->company,curr->email);
    		curr = curr->next;
    	}
    	fclose(fptr);
    	return(1);
    
    
    }//end writeFileToList
    
    
    My struct definition is like this:
    
    
    Code:
    struct contact {
    		char sname[15];
    		char fname[15];
    		char phone[15];
    		char company[15];
    		char email[15];
    		struct contact *prev;
    		struct contact *next;
    };
    Anyone know where I'm going wrong here?

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Access violation when using pointers to read data

    Your debugger will tell you.

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Access violation when using pointers to read data

    Anyone know where I'm going wrong here?
    Let's ask you -- do you know where you're going wrong? If not, then why not? You wrote the program with a plan in mind, so use the debugger to determine where the program deviates from your plan.

    If this is a school assignment, asking professional and experienced programmers on a forum to debug your program is considered cheating. You're the one that's supposed to be debugging your own programs. Even if this is not an assignment, debugging your own programs is a requirement in learning how to write programs.

    When debugging your program, if there is something that doesn't go the way it should have, then come back and tell us the line, function, etc. that crapped out, what you expected, and what actually happened. Then we can look more closely and explain why that line or set of lines behaved the way it did.

    But I will look at your code:
    Code:
    sname = strtok(oneLine,",");
    fname = strtok(NULL,",");
    phone = strtok(NULL,",");
    company = strtok(NULL,",");
    email = strtok(NULL,",");
    You just assume that the return values from all of these calls to strtok() are not NULL. But what if one of these are NULL? Your whole program from then on is faulty and would more than likely lead to an access violation.

    Where is your check for NULL values from these calls to strtok()? Why did you check for NULL when you attempted to open the file with fopen() (which is good), but then failed to check for NULL returns from strtok()?

    Regards,

    Paul McKenzie

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured