-
Edit txt
I want to be able to edit file.. when i hit command 3 but i do not know the code.. current code is as follows:
Code:
#include <stdio.h>
int main()
{
int option;
char name[18];
char name2[25];
char addr[50];
char bdate[10];
char hvisit[50];
char premedprob[50];
char premeds[50];
char crtmeds[50];
char notes[200];
char phone[25];
char text[50];
char *line = { "\n---------------------------------------------- \n" };
char *nameinput = { "First Name: " };
char *nameinput2 = { "Last Name: " };
char *addrinput = { "Address: " };
char *birthdateinput = {"Birth Date: "};
char *hospitalvisitinput = {"Hospitals Visited: "};
char *premedprobinput = {"Previous Medical Problems: "};
char *premedicationinput = {"Previous Medications: "};
char *crtmedicationinput = {"Current Medications: "};
char *notesinput = {"Notes On Patient: "};
char *phoneinput = { "Phone Number: " };
char *newline = "\n";
FILE *file_ptr;
file_ptr = fopen("Database.txt", "a");
if(file_ptr != NULL)
{
system("cls");
printf(" \n");
printf("Options Currently Available: \n");
printf(" \n");
printf("1. Add a new Patient Profile \n");
printf("2. Search for Patient Profile \n");
printf("3. Update Patient Profile \n");
printf("4. Exit \n");
printf(newline);
printf("Please Choose a Option: ");
scanf("%d", &option);
fclose(file_ptr);
}
if(option == 1)
{
file_ptr = fopen("Database.txt", "a");
if(file_ptr != NULL)
{
printf("Input Started \n");
printf(newline);
fputs(line, file_ptr);
printf("Please Enter First Name: ");
fputs(nameinput, file_ptr);
fflush(stdin);
fgets(name, 18, stdin);
fputs(name, file_ptr);
printf("Please Enter Last Name: ");
fputs(nameinput2, file_ptr);
fflush(stdin);
fgets(name2, 25, stdin);
fputs(name2, file_ptr);
printf("Please Enter Address: ");
fputs(addrinput, file_ptr);
fflush(stdin);
fgets(addr, 50, stdin);
fputs(addr, file_ptr);
printf("Please Enter Birth Date: ");
fputs(birthdateinput, file_ptr);
fflush(stdin);
fgets(bdate, 10, stdin);
fputs(bdate, file_ptr);
printf("Please Enter Phone Number: ");
fputs(phoneinput, file_ptr);
fflush(stdin);
fgets(phone, 25, stdin);
fputs(phone, file_ptr);
printf("Please Enter Hospitals Visited: ");
fputs(hospitalvisitinput, file_ptr);
fflush(stdin);
fgets(hvisit, 50, stdin);
fputs(hvisit, file_ptr);
printf("Please Enter Previous Medical Problems: ");
fputs(premedprobinput, file_ptr);
fflush(stdin);
fgets(premedprob, 50, stdin);
fputs(premedprob, file_ptr);
printf("Please Enter Previous Medications: ");
fputs(premedicationinput, file_ptr);
fflush(stdin);
fgets(premeds, 50, stdin);
fputs(premeds, file_ptr);
printf("Please Enter Current Medications: ");
fputs(crtmedicationinput, file_ptr);
fflush(stdin);
fgets(crtmeds, 50, stdin);
fputs(crtmeds, file_ptr);
printf("Please Enter Any Additional Notes: ");
fputs(notesinput, file_ptr);
fflush(stdin);
fgets(notes, 200, stdin);
fputs(notes, file_ptr);
printf("Information Saved To Database, Thankyou! \n");
system("pause");
}
fclose(file_ptr);
}
else if(option == 2)
{
file_ptr = fopen("Database.txt", "r");
if(file_ptr != NULL)
{
system("cls");
printf("Current Entries: \n");
while(fgets(text, 50, file_ptr) != NULL)
{
printf("%s", text);
printf(newline);
}
printf("End of File \n");
system("Pause");
fclose(file_ptr);
}
}
else if(option == 3)
{
}
else if(option == 4)
{
exit(1);
}
else
{
printf("Incorrect Input!\n");
}
return 0;
}
-
If it's a text file you can't fseek accurately, so you're going to have to read the whole thing into memory, edit it in memory, and write the file back out again.
Quzah.
-
and how may i accomplish such?
-
Learn basic file IO. Once you've discovered the finer points of opening and reading files, try printing what you have. Then try changing something. Oh, and you might want to read up on writing files also.
Quzah.
-