int readFile(struct record ** ptrStart, char filename[ ]) { FILE * inFile = fope
ID: 3855652 • 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
1. You need to check end of file condition there too. Probably for that reason you are getting an error. Last lines of your code will not be processed since it will not enter the while loop itself after the feof.
2. Print the lines[strlen(lines) - 2] character and see if you are getting expected '!' or not. If you are using windows os and notepad, you probably will not get it
If you want me to upload a proper debug code, let me know. Will uload a proper debug code, that will help you debuf your logic
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.