Thread: need help on record data (add,delete) in text file using file IO, structure, function

  1. #1
    Registered User
    Join Date
    Aug 2013
    Posts
    2

    Red face need help on record data (add,delete) in text file using file IO, structure, function

    I'm still learning intro to C language. Currently I'm having problem with my code. The task is to ask user to input information about staff and write them into a text file. The problem occur when:


    1) output in text/.exe file display random characters.

    2) Data obtained from New_Staff unable to pass into Export_Profile.

    3) a new output will overwrite the existing information in the text/.exe file

    4) the delete function unable to delete correctly after the first time i delete a staff information in text file.


    I would be so happy if I can solve any of these 3 problem


    The code is as follows:
    Code:
     
        #include <stdio.h>
        #include <stdlib.h>
     
        void Menu();
        void New_Staff();
        void Delete_Staff();
        void Export_Profile();
    
    
        struct element{
            char id[20];
            char name[20];
            char gender[20];
            char phone[20];
            char email[20];
        }profile;
    
    
        int main(void){      //The program will continue running until option 4 selected                                   
            int a;    
        
            for(a=0;;a++){
               Menu();
            }
            return 0;
        }
    
    
        void Menu()         //Main Menu to select option
        {
            int n;
            printf("\n**********************************************");
            printf("\nMAIN MENU-STAFF INFORMATION SYSTEM");
            printf("\n**********************************************");
            printf("\n1. Add profile Staff Profile");
            printf("\n2. Delete Staff Profile");
            printf("\n3. Export All Profiles to 'output.txt'");
            printf("\n4. Exit");
            printf("\n**********************************************");
            printf("\nPlease enter your option< 1 / 2 / 3 / 4 >: ");
            scanf("%d", &n);
        
            switch(n){
            case 1:
                New_Staff();
                break;
            case 2:
                Delete_Staff();
                break;
            case 3:
                Export_Profile();
                break;
            case 4:
                printf("\n*** Thanks for using the program! Goodbye. ***");
                exit(0);
                break;
            default:
                printf("\nError! Wrong Number is Entered\nPlease Try Again");
                break;
            }
        }
    
    
        void New_Staff()                        //Add New Staff Profile    
        {    
            int x;
            struct element profile;
                            
            printf("\n===Add New Staff Profile===");
            printf("\n\nPlease enter the following staff information.");
        
            printf("\n\nStaff ID: ");
            scanf("%s", &profile.id);
        
            printf("Name\t: ");
            fflush(stdin);
            fgets(profile.name,20,stdin);
        
            for(x=0 ; x<20 ; x++)
            {
              if(profile.name[x] == '\n')
                profile.name[x] = '\0';
            }
         
            printf("Gender\t: ");
            scanf("%s", &profile.gender);
        
            printf("Phone\t: ");
            scanf("%s", &profile.phone);
        
            printf("Email\t: ");
            scanf("%s", &profile.email);
        
            printf("\nSYSTEM: New Staff Profile is Added Successfully.");
        
        }
    
    
        void Delete_Staff()            //Delete Staff Profile
        {                    
            FILE *fRead, *fWrite;
            char *TextFile;
            char c;
            int Delete_Id, temp = 1;
        
            TextFile="output.txt";
    
    
            fRead = fopen(TextFile, "r");
            c = getc(fRead);
    
    
            while (c != EOF){
            printf("%c", c);
            c = getc(fRead); 
            }
      
            rewind(fRead);
    
    
            printf("\nDelete Staff with ID: ");
            scanf("%d", &Delete_Id);
        
            Delete_Id=Delete_Id+1;
    
    
            fWrite = fopen("copy.c", "w");
            c = getc(fRead);
            while (c != EOF) {
            c = getc(fRead);
            if (c == '\n')
            temp++;
            if (temp != Delete_Id){
            putc(c, fWrite); 
               } 
            }
    
    
            fclose(fRead);
            fclose(fWrite);
    
    
            remove(TextFile);
    
    
            rename("copy.c", TextFile);
            printf("\nThe contents of file after being modified are as follows:\n");
        
            fRead = fopen(TextFile, "r");
            c = getc(fRead);
            while (c != EOF) {
            printf("%c", c);
            c = getc(fRead);
            }
          
            fclose(fRead);
        }
    
    
        void Export_Profile()            //Export Staff Profile to Text
        {        
            struct element profile;
        
            FILE *fPtr;
            fPtr=fopen("output.txt","w");
            FILE *fPtr1;
            fPtr1=fopen("output.txt","a+");
        
            if (fPtr == NULL)
            printf("Error in opening file\n");
            if (fPtr1 == NULL)
            printf("Error in opening file\n");
        
            fprintf(fPtr,"%10s %10s %10s %10s %10s","Staff","ID",
                     "Name","Gender","Phone","Email");
    
    
            fprintf(fPtr1,"\n%10s %10s %10s %10s %10s", profile.id, profile.name,   
                     profile.gender, profile.phone, profile.email);
    
    
            printf("\n%10s %10s %10s %10s %10s", "Staff", "ID", "Name",
                     "Gender", "Phone",  "Email");
            printf("\n%10s %10s %10s %10s %10s", profile.id, 
                     profile.name, profile.gender, profile.phone, profile.email);
        
            printf("\nSYSTEM: All staff profile have been exported to output.txt file");
    
    
            fclose(fPtr);
            fclose(fPtr1);
        }

    I'm sorry for the long code. I not really sure where problem lies.


    The output give this:



    **********************************************
    MAIN MENU-STAFF INFORMATION SYSTEM
    **********************************************
    1. Add profile Staff Profile
    2. Delete Staff Profile
    3. Export All Profiles to 'output.txt'
    4. Exit
    **********************************************
    Please enter your option< 1 / 2 / 3 / 4 >: 1


    ===Add New Staff Profile===


    Please enter the following staff information.


    Staff ID: 1
    Name : Carmen Gray
    Gender : Female
    Phone : 123-4567890
    Email : [email protected]


    SYSTEM: New Staff Profile is Added Successfully.
    **********************************************
    MAIN MENU-STAFF INFORMATION SYSTEM
    **********************************************
    1. Add profile Staff Profile
    2. Delete Staff Profile
    3. Export All Profiles to 'output.txt'
    4. Exit
    **********************************************
    Please enter your option< 1 / 2 / 3 / 4 >: 3


    Staff ID Name Gender Phone
    °²# @Γg╕∙ ∙
    SYSTEM: All staff profile have been exported to output.txt file
    **********************************************
    MAIN MENU-STAFF INFORMATION SYSTEM
    **********************************************
    1. Add profile Staff Profile
    2. Delete Staff Profile
    3. Export All Profiles to 'output.txt'
    4. Exit
    **********************************************
    Please enter your option< 1 / 2 / 3 / 4 >: 2


    Staff ID Name Gender Phone
    °²# @Γg╕∙ .

    Delete Staff with ID: 1


    The contents of file after being modified are as follows:
    Staff ID Name Gender Phone

    and the text file display this:

    Staff ID Name Gender Phone
    øý# @âg¸ù ù

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Are you trying to write to the same file opening it simultaneously for writing and appending?

    What do you expect will happen in this case?

    To compare c to EOF you need to declare it int not char

    Did you set your warning level to the maximum?

    You should not ignore most of the following warnings
    Code:
    gcc -c -o obj/test.o src/test.c -Wall -pedantic -march=core2 -Iinclude
    src/test.c: In function ‘main’:
    src/test.c:19:22: warning: C++ style comments are not allowed in ISO C90 [enabled by default]
    src/test.c:19:22: warning: (this will be reported only once per input file) [enabled by default]
    src/test.c: In function ‘New_Staff’:
    src/test.c:73:5: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[20]’ [-Wformat]
    src/test.c:86:5: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[20]’ [-Wformat]
    src/test.c:89:5: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[20]’ [-Wformat]
    src/test.c:92:5: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[20]’ [-Wformat]
    src/test.c: In function ‘Export_Profile’:
    src/test.c:166:5: warning: ISO C90 forbids mixed declarations and code [-pedantic]
    src/test.c:175:14: warning: too many arguments for format [-Wformat-extra-args]
    src/test.c:183:14: warning: too many arguments for format [-Wformat-extra-args]
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Feb 2013
    Location
    Buea Cameroon
    Posts
    64
    Quote Originally Posted by Nabil Fikri View Post
    2) Data obtained from New_Staff unable to pass into Export_Profile.
    I dont see where the function
    Code:
        void New_Staff()                        //Add New Staff Profile    
        {    
            int x;
            struct element profile;
                            
            printf("\n===Add New Staff Profile===");
            printf("\n\nPlease enter the following staff information.");
        
            printf("\n\nStaff ID: ");
            scanf("%s", &profile.id);
        
            printf("Name\t: ");
            fflush(stdin);
            fgets(profile.name,20,stdin);
        
            for(x=0 ; x<20 ; x++)
            {
              if(profile.name[x] == '\n')
                profile.name[x] = '\0';
            }
         
            printf("Gender\t: ");
            scanf("%s", &profile.gender);
        
            printf("Phone\t: ");
            scanf("%s", &profile.phone);
        
            printf("Email\t: ");
            scanf("%s", &profile.email);
        
            printf("\nSYSTEM: New Staff Profile is Added Successfully.");
        
        }
    Stores the values enterd by the use in the file output.txt that is used by the preceding functions. This is the reason for the strange characters

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    Aug 2013
    Posts
    2
    i'm sorry i dont understand what you're trying to say. i'm beginner and i'm not used to c terms

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary file - delete record issue
    By ulti-killer in forum C Programming
    Replies: 1
    Last Post: 12-03-2012, 11:29 AM
  2. Replies: 1
    Last Post: 02-29-2012, 09:52 AM
  3. Update Record & Delete Record in File.
    By unsafe_pilot1 in forum C Programming
    Replies: 13
    Last Post: 05-18-2008, 07:22 AM
  4. The best way to delete a record from a file?
    By salvadoravi in forum C Programming
    Replies: 25
    Last Post: 01-28-2008, 01:12 PM
  5. how to delete or modify a record in a file?
    By danielhf in forum C++ Programming
    Replies: 2
    Last Post: 09-22-2003, 09:18 AM

Tags for this Thread