Deleting / Changing a record in a text file
Alrighty
I believe this is my last issue with this assignment I will need the forums help with "Yay!"
I have two more function to write/complete for this program.
I have to take in 4 peices of data in my add record function, Course Code, Subject Description, mark 1 and mark 2. They have to appeare in the text file as follows
CourseCODE
Subject
Mark1
Mark2
I have my add function checking the file before it writes to make sure this not a duplicate course alredy there.
In my delete and change funcitons I use this procedure to check to make sure the coursecode is the file before I attempt to delete it.
my understanding of how the delete is to work, is that I create a temp file (no problem there) and copy the contents of my data file except the "deleted" records to the temp file, then delete the old data file and rename the temp one to that of the deleted file.
I am not sure how to read from file and write to another while simultaniously avoiding the four fields in the record once I find the record to be deleted or modified.
so say I select to delete COMP086 from the text file that looks like this
Code:
COMP059
Hardware Fun
89.99
70.10
COMP086
Programming in C
98.99
89.99
COMP098
Intro to Microprocessors
67.00
76.89
I can Identify the the field exisits but I am not sure where to go from there. here is my code for this function as it sits now
Code:
int delSubject()
{
//declare local variables
int j = 0;
int duplicate = 0;
char courseCODE[8];
char buff[BUFSIZ];
char *p;
FILE *fp;
FILE *tp;
// Get course code from user
fp = fopen(MarkFile, "r");
do
{
duplicate = 0;
system ("cls");
printf("\n\n Class Mark Subject Management \n\n");
printf(" ** Delete Subject ** \n\n");
printf(" Please enter the 7 digit course code \n");
printf("\n Please press <Enter> to continue ");
getchar();
fgets(buff, sizeof buff, stdin);
sscanf(buff, "%7s", courseCODE);
// Check course code for alpha chars and convert them to upper case
for(j=0; j<=7; j++)
{
if(isalpha(courseCODE[j]))
{
courseCODE[j]=toupper(courseCODE[j]);
}
}
//compare input course code to those already on record for matching record
//Loop through the data to check the fields
while (fgets(buff, sizeof(buff), fp))
{
if ((p = strchr(buff, '\n')) != NULL)
{
*p = '\0';
}
if (strcmp(courseCODE, buff) != 0)
{
duplicate = 1;
system ("cls");
printf("\n\n\n Matching course code not found please enter a valid code\n\n");
printf(" Press any key to continue.\n");
getchar();
break;
}
}
}while (duplicate != 0);
fclose(fp);
printf("\n\n Account successfully deleted press any key to continue ");
getchar();
getchar();
return 0;
}
int changeMark()
{
return 0;
}
I assume using
Code:
system("del marks.txt");
system("rename temp.txt marks.txt")
will work once I have copied the appropriate data over to the temp file.
TIA for any help
clearrtc
Help one last time please!
Hello to who ever might be on at this time,
I have successfully made the delete function work and I am so close to having the change function work.
when I run the program and edit the file it all appears to work until the very end when it writes the file... well perhaps after that becuase the function preforms the dos commands to copy the file etc.
here is the error message I am getting
Code:
Run-Time Check Failure #2 - Stack around the variable 'recordUPDATE' was corrupted.
This is my "completed" function
Code:
int changeMark()
{
//declare local variables
int i = 0;
int j = 0;
int duplicate = 0;
char courseCODE[8];
char buff[BUFSIZ];
char *p;
char recordUPDATE[4][BUFSIZ];
FILE *fp;
FILE *tp;
// Get course code from user
fp = fopen(MarkFile, "r");
do
{
duplicate = 0;
system ("cls");
printf("\n\n Class Mark Subject Management \n\n");
printf(" ** Modify Subject ** \n\n");
printf(" Please enter the 7 digit course code \n");
printf("\n Please press <Enter> to continue ");
getchar();
fgets(buff, sizeof buff, stdin);
sscanf(buff, "%7s", courseCODE);
// Check course code for alpha chars and convert them to upper case
for(j=0; j<=7; j++)
{
if(isalpha(courseCODE[j]))
{
courseCODE[j]=toupper(courseCODE[j]);
}
}
sscanf(courseCODE, "%7s", recordUPDATE[1]);
// Get input for subjectNAME from user
system ("cls");
printf("\n\n Class Mark Subject Management \n\n");
printf(" ** Modify Subject ** \n\n");
printf(" Please enter the subject name \n");
printf("\n Please press <Enter> to continue ");
fgets( buff, sizeof buff, stdin );
sscanf(buff, "%31s", recordUPDATE[2]);
// Get input for the firstMARK from user
system ("cls");
printf("\n\n Class Mark Subject Management \n\n");
printf(" ** Modify Subject ** \n\n");
printf(" Please enter the first mark \n");
printf("\n Please press <Enter> to continue ");
fgets(buff, sizeof buff, stdin);
sscanf(buff, "%5s", recordUPDATE[3]);
// Get input for the secondMARK from user
system ("cls");
printf("\n\n Class Mark Subject Management \n\n");
printf(" ** Modify Subject ** \n\n");
printf(" Please enter the second mark \n");
printf("\n Please press <Enter> to continue ");
fgets(buff, sizeof buff, stdin);
sscanf(buff, "%5s", recordUPDATE[4]);
//compare input course code to those already on record for duplicate or for quit condition
//Loop through the data to check the fields
tp = fopen (TempFile, "a");
while (fgets(buff, BUFSIZ, fp) != NULL)
{
i = 0;
if ((p = strchr(buff, '\n')) != NULL)
{
*p = '\0';
}
//Looking for Data to be deleted
if (strcmp(courseCODE, buff) == 0)
{
//Remove the data to be deleted from the buffer
for( i = 0; i < 4; i++ )
{
fprintf(tp, "%s\n",recordUPDATE[i]);
fgets(buff, BUFSIZ, fp);
}
}else{
fprintf(tp, "%s\n",buff);
}
}
}while (duplicate != 0);
fclose(fp);
fclose(tp);
fclose(fp);
fclose(tp);
system("del marks.txt");
system("rename temp.txt marks.txt");
printf("\n\n Course successfully modified press any key to continue ");
getchar();
return 0;
}
Thanks for any help
clearrtc