Friday, March 9, 2018

Interactive C program : To create and update records

Here is an interactive C program written by me to create and update records in a txt file

/* program to create a text file   and update records  */

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include <dirent.h>
int main(void)
{

    FILE *fp;   /* file pointer*/
    char fName[30];
    char name[30];
    int age=0, SrNo=0,SrNo2=0,count_lines=0;
    char  qualification[20];
    char action,en,ch,c;

    A: printf("Type 'N' to create a new file and enter records\n");
    printf("Type 'U' to read/update/modify records in an existing file\n");
    scanf("%s",&action);
    if(action=='N')
    { //start of option if
        printf("Enter file name with .txt extension to create :");
        scanf("%s",fName);

        /*creating (open) a file, in “w”: write mode*/
        fp=fopen(fName,"w");
        /*check file created or not*/
        if(fp==NULL)
        {
        printf("File does not created!!!");
        exit(0); /*exit from program*/
        }

        //printf("\nFile created successfully.\n");
        printf("Start entering students records\n");
    // printf("Finally type e to exit or any other character to continue adding more records\n");
        while(en !='e')
        {

            printf("\nEnter (space separated) student's name, age and qualification \n");

            scanf("%s", name);
            SrNo++;
            fprintf(fp, "Roll No.%d  Name  :%s\t",SrNo, name);

            //printf("Enter student's age\n");

            scanf("%d", &age);

            fprintf(fp, "Age  :%d\t", age);

            // printf("Enter students qualification\n");

            scanf("%s", &qualification);

            fprintf(fp, "Qualification  :%s\n", qualification);

            printf("Type e to exit or any other char to add more records  ");
            //en = getchar();
            scanf("%s",&en);
            if(en== 'e')
            {   printf("\nFile created and records saved successfully.\n");
            break;
            }
        }
        fclose(fp);
    }  //end of option if
     else if(action=='U')
  {   //start of option if
          DIR *p;
            struct dirent *pp;
            p = opendir(".");

            if (p != NULL)
            {
                while ((pp = readdir (p))!=NULL)
                {
                    int length = strlen(pp->d_name);
                    if (strncmp(pp->d_name + length - 4, ".txt", 4) == 0)
                     {
                        puts (pp->d_name);
                     }
                }

                (void) closedir (p);
            }

            printf("\n1] Enter filename to read, from the above list of files\n");
            printf("2] Enter filename to add new records, from the above list of files\n");
            printf("3] Enter filename to delete a record, from the above list of files\n");
            printf("4] Enter filename to replace a record, from the above list of files\n");
            printf("5] Exit\n");

 ////////////////////////////////////////////////////////////////////////////////////////////////////////////

            while(1)
    {  //start of main while
                printf("\nEnter your choice : ");

                scanf("%d", &ch);

                switch (ch)
            {   //start of switch
                case 1:
                     printf("Enter the filename to be opened \n");

                     scanf("%s", fName);

                    /*  open the file for reading */

                    fp = fopen(fName, "r");

                    if (fp == NULL)

                    {

                        printf("Sorry this file does not exist !!, cannot open file \n");

                        exit(0);

                    } //end of if

                    c = fgetc(fp);

                    while (c != EOF)

                    { // start of 2nd while

                    printf ("%c", c);

                    c = fgetc(fp);

                    } //end of 2nd while

                    fclose(fp);
                    break;    // eo case 1

                case 2:
                    printf("Enter the filename to be opened and new record to be added\n");

                     scanf("%s", fName);

                    /*  open the file for reading */

                    fp = fopen(fName, "r");

                    if (fp == NULL)

                    {

                        printf("Cannot open file \n");

                        exit(0);

                    } //end of if
                    c=getc(fp);
                        while (c != EOF)

                    { //start of while

                            //Count whenever new line is encountered

                            if (c == '\n')

                            {

                                count_lines = count_lines + 1;

                            }

                            //take next character from file.

                            c = getc(fp);

                    } // end of while

                        fclose(fp); //close file.
                    SrNo2= count_lines;

                  fp=fopen(fName,"a");
                    /*check file created or not*/
                    if(fp==NULL)
                    {
                        printf("File not created!!!");
                        exit(0); /*exit from program*/
                    }

                    //printf("\nFile created successfully.\n");
                    printf("Start entering students records\n");
                    // printf("Finally type e to exit or any other character to continue adding more records\n");

                    while(en !='e')
                {

                    printf("\nEnter (space separated) student's name, age and qualification \n");

                    scanf("%s", name);
                    SrNo2++;
                    fprintf(fp, "Roll No.%d  Name  :%s\t",SrNo2, name);

                    //printf("Enter student's age\n");

                    scanf("%d", &age);

                    fprintf(fp, "Age  :%d\t", age);

                    // printf("Enter students qualification\n");

                    scanf("%s", &qualification);

                    fprintf(fp, "Qualification  :%s\n", qualification);

                    printf("Type e to exit or any other char to add more records  ");
                    //en = getchar();
                    scanf("%s",&en);
                    if(en== 'e')
                    {   printf("\nFile created and records updated successfully.\n");
                        //break;
                    }
            }
        fclose(fp);


                case 3:




                case 4:



                case 5:
                    exit(0);
                default:
                    printf("Wrong choice entered !! please try again\n");


            } // end of switch

    } //eo while
 ////////////////////////////////////////////////////////////////////////////////////////////////////////////
     } //eo option if
     else
     {
         printf("Incorrect option entered, please try again\n");
         goto A;

     }
       
   return 0;

}

No comments:

Post a Comment

Derivatives stock list at NSE

Complete FNO stock list at NSE. ABB India Ltd ACC Ltd APL Apollo Tubes Ltd AU Small Finance Bank Ltd Aarti Industries Ltd Abbott India Ltd A...