Thread: Operating File

  1. #1
    Registered User
    Join Date
    Dec 2013
    Posts
    4

    Post Operating File

    Code:
    #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;
    }
    How to search,delete and Edit
    Please Help me
    i'm newbie

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    It looks like you're writing records to your file in this format with these three format strings

    Code:
    "Name   : %s \n"
    "Addres : %s \n"
    "---------------------------\n"
    To do the opposite (reading) you can use fscanf to get those three lines. I recommend checking the return value in case the lines don't follow the expected format. Since your records are not in any particular order you'll probably have to use a basic linear search. Read each record and decide if it meets the search criteria.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Also posted here.

    Jim

  4. #4
    Registered User
    Join Date
    Dec 2013
    Posts
    4
    Quote Originally Posted by c99tutorial View Post
    It looks like you're writing records to your file in this format with these three format strings

    Code:
    "Name   : %s \n"
    "Addres : %s \n"
    "---------------------------\n"
    To do the opposite (reading) you can use fscanf to get those three lines. I recommend checking the return value in case the lines don't follow the expected format. Since your records are not in any particular order you'll probably have to use a basic linear search. Read each record and decide if it meets the search criteria.
    Quote Originally Posted by jimblumberg View Post
    Also posted here.

    Jim
    I do not understand what you mean I am newbie in c
    Is there no source code to delete, edit and search

  5. #5
    Registered User
    Join Date
    Dec 2013
    Posts
    4
    Quote Originally Posted by c99tutorial View Post
    It looks like you're writing records to your file in this format with these three format strings

    Code:
    "Name   : %s \n"
    "Addres : %s \n"
    "---------------------------\n"
    To do the opposite (reading) you can use fscanf to get those three lines. I recommend checking the return value in case the lines don't follow the expected format. Since your records are not in any particular order you'll probably have to use a basic linear search. Read each record and decide if it meets the search criteria.
    I do not understand your point?
    please help me I'm newbie in c

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by jursnamo View Post
    Is there no source code to delete, edit and search
    Not until you write it, no.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Operating Files using c...???
    By Rehman khan in forum C Programming
    Replies: 2
    Last Post: 02-29-2012, 07:42 AM
  2. What is your operating system?
    By undisputed007 in forum A Brief History of Cprogramming.com
    Replies: 41
    Last Post: 04-17-2004, 02:56 PM
  3. Operating environment
    By Unimatrix139 in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 09-08-2002, 03:43 AM
  4. Operating System
    By kas2002 in forum C++ Programming
    Replies: 18
    Last Post: 06-20-2002, 12:03 AM
  5. Operating System
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 115
    Last Post: 03-31-2002, 06:34 AM

Tags for this Thread