int readFile(struct record ** ptrStart, char filename[ ]) { FILE * inFile = fope
ID: 3855651 • Letter: I
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;
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] = '';
uaddress[strlen(uaddress) - 2] = uaddress[strlen(uaddress) -1];
uaddress[strlen(uaddress) - 2] = '';
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;
};
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.
Explanation / Answer
The error Abort trap 6 comes when you are trying to access a memory location which is not actually yours. That means your program has not allocated that memory location to any of your variable.
Now the issue in your code is that you are trying to copy lines in uname and uaddress using strcpy.
Your program specifies the size of lines,uname and uaddress. The size allocated to uname and uaddress is 25 and 80, but the size allocated to lines is 256.
So by below statements: -
strcpy(uaddress, lines);
strcpy(uname, lines);
Here you are trying to copy an array of length 256 in an array of length 25 and 80. It copies it but in unauthorized memory location. So when you try to concatenate it , it tries to access that unauthorized location and gives you the error.
So one of the workaround for this can be that your uname and uaddress must be of size 256..i.e. it must be atleast equal to size of lines or more that that. I am sure the program will work if you do like below
Change these lines
char uname[25];
char uaddress[80];
To these lines
char uname[256];
char uaddress[256];
If you still get any problem, Please comment on the answer, I'll love to help you more ..
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.