Question: int readFile(struct record ** ptrStart, char filename[ ]) { FILE * inFile = fopen(filename, r); int uaccountnum; char uname[25]; char uaddress[80]; char lines[256]; int lineNum;
int readFile(struct record ** ptrStart, char filename[ ]) { FILE * inFile = fopen(filename, "r");
int uaccountnum; char uname[25]; char uaddress[80];
char lines[256]; int lineNum; lineNum = 1;
if (DEBUG_MODE == 1) { printf(" ********************************************* "); printf("FUNCTION CALLED: readfile(); "); printf("PARAMETERS PASSED: "); printf("File name: %s ", filename); printf("********************************************* "); printf(" ---Contents of the input file being added--- ");
}
while(!feof(inFile)) { if(lineNum == 1) { fgets(lines, sizeof(lines), inFile); uaccountnum = atoi(lines); lineNum++; } else if (lineNum == 2) {
fgets(lines, sizeof(lines), inFile); strcpy(uname, lines); lineNum++; } else if(lineNum == 3) { fgets(lines, sizeof(lines), inFile); if(lines[strlen(lines) - 2] != '!') { strcpy(uaddress, lines);
while(lines[strlen(lines) - 2] != '!') { fgets(lines, sizeof(lines), inFile); strcat(uaddress, lines); //abort trap 6 error here
} } else { strcpy(uaddress, lines); lineNum++; } } else { uname[strlen(uname) - 1] = '\0'; uaddress[strlen(uaddress) - 2] = uaddress[strlen(uaddress) -1]; uaddress[strlen(uaddress) - 2] = '\0'; lineNum = 1;
addRecord(ptrStart,uaccountnum,uname,uaddress); }
}fclose(inFile); return 0; }
----------------------------- This is my structure
int DEBUG_MODE;
struct record
{
int accountno;
char name[25];
char address[80];
struct record* next;
};
-------------------------------
input.txt
2000 Tom cruise 1800 Somewhere street
I am trying to read information from a textfile called "input.txt". I have narrowed down the error to strcat(uaddress, lines); I am trying to cut the array lines at the end of the array uaddress but I am unsure what exactly the problem is and how to fix it The program is supposed to read from a txt file and create a record, the records are linked lists.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
