Thread: Edit txt

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    3

    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;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    3
    and how may i accomplish such?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    3
    ok cool...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. edit txt file help
    By pczafer in forum C++ Programming
    Replies: 4
    Last Post: 04-20-2009, 07:49 AM
  2. (Multiline) Edit Control Limit
    By P4R4N01D in forum Windows Programming
    Replies: 9
    Last Post: 05-17-2008, 11:56 AM
  3. line number on a rich edit control
    By rakan in forum Windows Programming
    Replies: 1
    Last Post: 02-18-2008, 07:58 AM
  4. using c++ to edit txt files
    By trescenzi in forum C++ Programming
    Replies: 2
    Last Post: 12-28-2006, 08:54 PM
  5. Replies: 3
    Last Post: 07-23-2005, 08:00 AM