What I'm trying to do is to replace a specific line within a txt file using C but for some reason after saying what line i want to change it just ended the exe.
void UpdateCustomerPortfolio()
{
const char path[500] = "C:\\Users\\jason\\Desktop\\College stuff\\Final assessment\\Fundimentals of programming\\Accounts\\";
int Ic[12],line;
char txt[5] =".txt",num[100];
FILE * fPtr;
FILE * fTemp;
char buffer[BUFFER_SIZE];
char newline[BUFFER_SIZE];
int count;
fflush(stdin);
printf("Input IC number: ");
gets(Ic);
strcat (path, Ic);
strcat (path, txt);
system("cls");
printf("Enter the Information that requires an update:");
printf("\n[01]First Name");
printf("\n[02]Last Name");
printf("\n[04]Home Address");
printf("\n[05]Contact Number");
printf("\n[06]Email Address\n");
scanf("%d ",line);
This is the part for some reason it keeps ending after the user enters the line they want to change. I did try using fflush(stdin); here again but it still didn't work.
printf("Replace '%d' line with: ", line);
fgets(newline, BUFFER_SIZE, stdin);
/* Open all required files */
fPtr = fopen(path, "r");
fTemp = fopen("replace.tmp", "w");
/* fopen() return NULL if unable to open file in given mode. */
if (fPtr == NULL || fTemp == NULL)
{
/* Unable to open file hence exit */
printf("\nUnable to open file.\n");
printf("Please check whether file exists and you have read/write privilege.\n");
exit(EXIT_SUCCESS);
}
/*
* Read line from source file and write to destination
* file after replacing given line.
*/
count = 0;
while ((fgets(buffer, BUFFER_SIZE, fPtr)) != NULL)
{
count++;
/* If current line is line to replace */
if (count == line)
fputs(newline, fTemp);
else
fputs(buffer, fTemp);
}
/* Close all files to release resource */
fclose(fPtr);
fclose(fTemp);
/* Delete original source file */
remove(path);
/* Rename temporary file as original file */
rename("replace.tmp", path);
printf("\nSuccessfully replaced '%d' line with '%s'.", line, newline);
}