#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
  int menu,i;
  char ulang;
  FILE *pf;  
  char karakter;
  int data[5];
  char nama[30],alamat[40];
  char buffer[1024];

  do 
  { 
  printf("Menu Utama Data Kendaraan\n");
  printf("--------------------------\n");
  printf("1. Show Data \n");
  printf("2. Add Data \n");
  printf("3. Edit Data \n");
  printf("4. Remove Data \n");
  printf("3. Search Data \n");
  printf("--------------------------\n");

     printf("Insert your selection : "); scanf("%d",&menu);
     printf("\n----------------------------\n");

     switch(menu)
     {
              case 1:
                   {
                          pf = fopen("D:/tugas/file/tugas/data.txt","r");
                          if (pf != NULL)
                          {
                           while ((karakter = getc(pf)) != EOF)
                                 {
                                        printf("%c",karakter);
                                 }
                           }
                           else
                           {
                                 printf("Fatal erorr : File coba.txt cannot be opened");
                                 exit(EXIT_FAILURE);
                           }
                          break;
                   }
              case 2:
                   {
                          pf = fopen("D:/tugas/file/tugas/data.txt","a+");
                          printf("Insert name   : "); fflush(stdin); gets(nama);
                          printf("Insert Address : "); fflush(stdin); gets(alamat);

                          if (pf != NULL)
                          {

                                 fprintf(pf,"Name   : %s \n",nama);
                                 fprintf(pf,"Addres : %s \n",alamat);
                                 fprintf(pf,"---------------------------\n");
                          }
                          else
                          {
                                 printf("Fatal erorr : File coba.txt cannot be opened");
                                 exit(EXIT_FAILURE);
                          }
                          fclose(pf);
                          break;
                   }


     }
     printf("\nReload [y/t]: "); fflush(stdin); scanf("%c",&ulang);
     system("cls"); 
  }
  while (ulang != 't') ;


  //system("PAUSE");    
  return 0;
}

**i write a program Operating file
1.Read/show file in notepad
2. Add file in notepad
3.Delete a content in notepad
4.edit a content in notepad
5. Search a content in notepad

i have done read file and and Add a content file in the notepad
then how to delete,edit and search a content in notepad?
i'm newbie in C
this is Show and Add**
how to delete,edit and search a content in notepad?

Recommended Answers

All 3 Replies

Deleting and modifying a file's contents in-place can be tricky. The general recommended approach is to read the file, write to a temporary file with the appropriate changes, delete the original file, then rename the temporary. the benefit of this approach is that it's simple reading and conditional writing. For example, to delete all instances of "foo" from a file:

FILE *in = fopen("test.txt", "r");

if (in != NULL) {
    FILE *out = fopen("temp.txt", "w");

    while (fgets(line, sizeof line, in) != NULL) {
        remove_substring(line, "foo");
        fputs(line, out);
    }

    fclose(out);
    fclose(in);

    remove("test.txt");
    rename("temp.txt", "test.txt");
}

Writing remove_substring is an interesting exercise, so I'll refrain from doing that for you. :) If the substring you want to delete contains a newline then the logic can get a bit hairy, but I'm guessing that's not going to be a likely case here, so the above logic should suffice.

I have tried as you suggest
but not yet performed

case 3:
               {
                    FILE *in = fopen("D:/tugas/file/tugas/data.txt", "r");
                    if (in != NULL) {
                    FILE *out = fopen("D:/tugas/file/tugas/coba.txt", "w");

                    while (fgets(line, sizeof line, in) != NULL) {
                    remove_substring(line, "foo");
                    fputs(line, out);
                    }
                    fclose(out);
                    fclose(in);
                    remove("D:/tugas/file/tugas/data.txt");
                    rename("D:/tugas/file/tugas/data.txt", "D:/tugas/file/tugas/coba.txt");
                    }

}

                          //if (pf != NULL)
                         // {
                                 /*for (i=0; i<5; i++)
                                 {
                                     fprintf(pf,"Data ke-%d : %d \n",i+1, data[i]);
                                 }*/
                                // fprintf(pf,"Judul buku   : %s \n",judul);
                                // fprintf(pf,"Penerbit : %s \n",penerbit);
                               //  fprintf(pf,"pengarang : %s \n",pengarang);

                                // fprintf(pf,"---------------------------\n");






                                if there is something wrong in the writing of coding

Did you write the remove_substring function? My example won't compile without it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.