Dpry12 Dpry12 - 25 days ago 6
C Question

C Linked List: Access violation reading location 0x00000001

I am very new to linked list and need a little help. Currently I am receiving this error"


Unhandled exception at 0x0FD940C1 (msvcr120d.dll) in
ConsoleApplication1.exe: 0xC0000005: Access violation reading location
0x00000001.


What the program is trying to do is take a file and iterating through it and creating a linked list, each node consisting of the string value and a node pointer to the next node. I have followed multiple tutorials and I am not sure what the issue is. If anybody could help, that would be fantastic.

Below is my code:

#ifdef _WIN32
#define _CRT_SECURE_NO_DEPRECATE
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define BUFF_SIZE 300

typedef struct node {
char stringDat[BUFF_SIZE];
struct node* next;
} NODE;

int main(int argc, char** argv) {
FILE* fp;
FILE* fpLogFile;

char* fileLoc = "C:\\Users\\David\\Desktop\\data.dat";
char* logFile = "C:\\Users\\David\\Desktop\\log.txt";

char buff[BUFF_SIZE];
int i, nodeCnt;

if ((fp = fopen(fileLoc, "rb+")) == NULL) {
printf("FILE OPEN ERROR ON EXISTING FILE\n");
exit(1);
}

if ((fpLogFile = fopen(logFile, "w")) == NULL) {
printf("Error opening log file\n");
exit(1);
}

// root node
NODE* root = NULL;

// provide memory for root node
root = malloc(sizeof(NODE));
if (root == NULL){
return 1;
}
fprintf(fpLogFile, "%s\n", "ROOT MALLOC");
nodeCnt = 1;

// assign values to root node
root->next = NULL;
fprintf(fpLogFile, "%s\n", "ROOT Next = 0");

strcpy(root->stringDat, fread(buff, 20, 1, fp));
fprintf(fpLogFile, "%s\n", "ROOT buff");

// set curr to root node
NODE* curr = root;
fprintf(fpLogFile, "%s\n", "Curr = ROOT");

while (fread(buff, 20, 1, fp) != NULL) {

// navigate to last node in list
while (curr->next != NULL) {
curr = curr->next;
}

fprintf(fpLogFile, "%s\n", "Curr = last node");

// Create node at end of list
curr->next = malloc(sizeof(NODE));
fprintf(fpLogFile, "%s\n", "Malloc new Node");
nodeCnt++;
// move current node to last node
curr = curr->next;

if (curr == 0) {
fprintf(fpLogFile, "%s\n", "Out of memory");
return 0;
}
// assign val to current stringDat
strcpy(curr->stringDat, buff);

fprintf(fpLogFile, "%s %d %p %d\n", curr->stringDat, strlen(curr->stringDat), curr->next, nodeCnt);

for (i = 0; i < BUFF_SIZE; i++) {
buff[i] = 0;
}


}

while ((curr = root) != NULL) { // set curr to head, stop if list empty.
root = root->next; // advance head to next element.
free(curr); // delete saved pointer.
}

fclose(fp);
fclose(fpLogFile);

return 0;

}

Answer

This:

strcpy(root->stringDat, fread(buff, 20, 1, fp));

makes no sense, and should have triggered a compiler warning.

The second argument to strcpy() is a string pointer, but fread() returns size_t (the number of "elements" it succeeded in reading). When it succeeds here, it will return 1 which will trigger the error you're getting.

You meant:

if(fread(buff, 20, 1, fp) == 1)
  strcpy(root->stringDat, buff);