File Process Update and Delete Problem

This is a discussion on File Process Update and Delete Problem within the C Programming forums, part of the General Programming Boards category; Hi i have written a student information storing program. it has add,delete,list,and update menu (in program i have written update ...

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    10

    File Process Update and Delete Problem

    Hi

    i have written a student information storing program.
    it has add,delete,list,and update menu
    (in program i have written update == uptade,i know : P )

    my question is i cant delete or uptade the information.
    where is my failure ?

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<conio.h>
    
    #define max 3
    
    struct og
    {
        char id[12];
        char tc[11];
        char name[30];
        char tel[11];
        char adres[50];
    }ogrenci[max],ogr[max],ex;
    
    FILE *kp;
    void menu(void);    // menu
    void add(void);     // add user
    void del(void);     // delete
    void list(void);    // list of users
    void uptade(void);  //uptade the information
    
    void main()
    {
        menu();
        system("pause");
    }
    
    
    void menu()
    {
        char secim;
        system("cls");
        menu:
        printf("\n\n\n\t########MAIN   MENU########\n\n");
        printf("\n\t[A]-ADD NEW STUDENT\n");
        printf("\t[D]-DELETE\n");
        printf("\t[U]-UPTADE\n");
        printf("\t[L]-LIST\n");
         printf("\t[Q]-QUIT\n");
        printf("\t\n\n\n[PLEASE SELECT]\n");
    
        scanf("%c",&secim);
        printf("\n");
        while( getchar() != '\n' ); // gets i etkilememesi išin kullandık
    
        switch(secim)
        {
            case 'a': add(); break;
            case 'A': add(); break;
            case 'd': del(); break;
            case 'D': del(); break;
            case 'u': uptade(); break;
            case 'U': uptade(); break;
            case 'l': list(); break;
            case 'L': list(); break;
            case 'q': exit(0); break;
            case 'Q': exit(0); break;
            default: system("cls"); goto menu; break;
        }
    }
    
    /*******************************************/
    /********KAYIT FORMU***********************/
    
    void add()
    {
        
        int ret;
        int i;
        int j;
        int k;
    
        char temp[50];
        system("cls");
        if((kp=fopen("kayit.txt","a+"))==NULL)
        {
            printf("hata\n\n");
        } //if
    
        
        else
        {
            i=0;
            
            do
            {
                
    
            printf("enter id>\n");
            gets(temp);
            strcpy(ogrenci[i].id,temp);
    
            for(j=0; j<max; j++)
            {
                
    
                if(i==j)
                {
                    continue;
                }
                
    
                else if(!strcmp(ogrenci[i].id,ogrenci[j].id))
                {
                    printf("\nBu ID kullanilmaktadir!\n");
                    printf("enter id>\n");
                    gets(temp);
                    strcpy(ogrenci[i].id,temp);
                    break;
                }
    
    
    
            }// for j
    
            printf("\n");
            printf("enter tc no>\n");
            gets(temp);
            strcpy(ogrenci[i].tc,temp);
    
            for(j=0; j<max; j++)
            {
                if(j==i)
                {
                    continue;
                } //if
                else if(!strcmp(ogrenci[i].tc,ogrenci[j].tc))
                {
                    printf("\nBu TC NO kullanilmaktadir!\n");
                    printf("enter tc no>\n");
                    gets(temp);
                    strcpy(ogrenci[i].tc,temp);
                    break;
    
                } // if
    
            }// for j
    
            printf("\nenter name>\n");
            gets(temp);
            strcpy(ogrenci[i].name,temp);
    
    
            printf("\nenter tel no>\n");
            gets(temp);
            strcpy(ogrenci[i].tel,temp);
    
    
            printf("\nenter addresses>\n");
            gets(temp);
            strcpy(ogrenci[i].adres,temp);
    
            printf("\n");
            i++;
            }while(i<max);
            for(i=0; i<max; i++)
            {
            ret=fwrite(&ogrenci[i],sizeof(ex),1,kp);
            }
        }//else
    
        fclose(kp);
        menu();
    
    
    }//add
    
    void del()
    {
        int i,j,ret,a;
        char id[12];
    
        if((kp=fopen("kayit.txt","r+"))==NULL)
        {
            printf("can not open\n");
            exit(0);
        }//if
        else
        {
            for(i=0; i<max; i++)
        {
            ret=fread(&ogrenci[i],sizeof(ex),1,kp);
        }//for
            printf("enter id for deleting>\n");
            gets(id);
            
            for(i=0; i<max; i++)
            {
            if(!strcmp(id,ogrenci[i].id))
            {
                a=i;
                break;
            }//if
            }//for
    
            ogrenci[a].adres[0]='\0';
            ogrenci[a].id[0]='\0';
            ogrenci[a].tc[0]='\0';
            ogrenci[a].tel[0]='\0';
            ogrenci[a].name[0]='\0';
    
            printf("deleted...\n");
            for(i=0; i<max; i++)
            {
            ret=fwrite(&ogrenci[i],sizeof(ex),1,kp);
            }
            fclose(kp);
    
        }//else
    
    }
    
    void uptade()
    {
        
        char ch;
        char id[12];
        char tc[11];
        char name[30];
        char tel[11];
        char adres[50];
        int i,k,a;
        int ret;
    
        system("cls");
        
        if((kp=fopen("kayit.txt","r+"))==NULL)
        {
            printf("can not open\n");
            exit(0);
        }//if
        for(i=0; i<max; i++)
        {
            ret=fread(&ogrenci[i],sizeof(ex),1,kp);
        }//for
        printf("enter ID for updating>\n");
        gets(id);
    
        for(i=0; i<max; i++)
        {
            if(!strcmp(id,ogrenci[i].id))
            {
                break;
            }//if
        }//for
        select:
        printf("what do you want to update\n\n");
        printf("[i]-id\n");
        printf("[t]-tc no\n");
        printf("[n]-name\n");
        printf("[o]-tel no\n");
        printf("[a]-addresses\n");
        scanf("%c",&ch);
        printf("\n");
        while( getchar() != '\n' );
        switch(ch)
        {
        case 'i': 
            printf("enter new id>\n");
            gets(id);
            k=strlen(id);
            ogrenci[i].id[0]='\0';
            for(a=0; a<k; a++)
            {
                ogrenci[i].id[a]=id[a];
            }
            break;
        case 'I':
            
            printf("enter new id>\n");
            gets(id);
            strcpy(ogrenci[i].id,id);
            break;
        case 't':
            printf("enter new tc no>\n");
            gets(tc);
            strcpy(ogrenci[i].tc,tc);
            break;
        case 'T':
            printf("enter new tc no>\n");
            gets(tc);
            strcpy(ogrenci[i].tc,tc);
            break;
        case 'n':
            printf("enter new name>\n");
            gets(name);
            strcpy(ogr[i].name,name);
            
            break;
        case 'N':
            printf("enter new name>\n");
            gets(name);
            strcpy(ogrenci[i].name,name);
            break;
        case 'o':
            printf("enter new tel no>\n");
            gets(tel);
            strcpy(ogrenci[i].tel,tel);
            break;
        case 'O':
            printf("enter new tel no>\n");
            gets(tel);
            strcpy(ogrenci[i].tel,tel);
            break;
        case 'a':
            printf("enter new addresses>\n");
            gets(adres);
            strcpy(ogrenci[i].adres,adres);
            break;
        case 'A':
            printf("enter new addresses>\n");
            gets(adres);
            strcpy(ogrenci[i].adres,adres);
            break;
        default: printf("error\n"); goto select; break;
        }//switch
        for(i=0; i<max; i++)
        {
        ret=fwrite(&ogrenci[i],sizeof(ex),1,kp);
        }
        fclose(kp);
        printf("kaydedildi....\n");
        system("pause");
        menu();
    }
    
    void list()
    {
        int i,j,ret;
        if((kp=fopen("kayit.txt","r"))==NULL)
        {
            printf("can not open\n\n");
        }//if
    
        for(i=0; i<max; i++)
        {
        ret=fread(&ogr[i],sizeof(ex),1,kp);
        }
        printf("\n\n-----LIST OF STUDENTS-----\n");
        for(i=0; i<max+3; i++)
        {
    
            printf("ID: %s\n",ogr[i].id);
            
            printf("TC NO: %s\n",ogr[i].tc);
            
            printf("NAME: %s\n",ogr[i].name);
            
            printf("TEL NO: %s\n",ogr[i].tel);
            
            printf("ADDRESSES: %s\n",ogr[i].adres);
    
            printf("\n");
        }//for
        fclose(kp);
    
        system("pause");
        menu();
        
    
        }

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,501
    void main should be int main, returning 0
    FAQ > main() / void main() / int main() / int main(void) / int main(int argc, char *argv[]) - Cprogramming.com

    You should not be using gets -> Ever
    FAQ > Why gets() is bad / Buffer Overflows - Cprogramming.com

    }//for
    This is not the correct way of making your program neat - You need to use proper indentation.
    Indent style - Wikipedia, the free encyclopedia

    The use of goto is as a loop is frowned upon in most cases. I think that a do/while loop would be more appropriate for your code.

    There is a little trick with
    Code:
    scanf("%c",&ch);
    
    /* 
      Add a space before the %c and white characters 
      on the buffer before your input are ignored 
    */
    scanf(" %c",&ch);
    When you say that you code "does not work", what is your current code doing? What do you want each section to do?
    Fact - Beethoven wrote his first symphony in C

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,847
    After doing this
    ret=fread(&ogrenci[i],sizeof(ex),1,kp);

    You need to do this
    rewind(kp);

    Before doing this
    ret=fwrite(&ogrenci[i],sizeof(ex),1,kp);


    > system("pause");
    > menu();
    If you put a proper while loop in menu(), then you could just return.
    As it stands, you have potentially endless recursion.

    Code:
        case 'a':
            printf("enter new addresses>\n");
            gets(adres);
            strcpy(ogrenci[i].adres,adres);
            break;
        case 'A':
            printf("enter new addresses>\n");
            gets(adres);
            strcpy(ogrenci[i].adres,adres);
            break;
    1. NEVER use gets -> SourceForge.net: Gets - cpwiki
    2. You can shorten the code to
    Code:
        case 'a':
        case 'A':
            printf("enter new addresses>\n");
            gets(adres);
            strcpy(ogrenci[i].adres,adres);
            break;

    Code:
    #define max 3
     
    struct og
    {
        char id[12];
        char tc[11];
        char name[30];
        char tel[11];
        char adres[50];
    }ogrenci[max],ogr[max],ex;
    1. By convention, #define names are written in uppercase, say #define MAX 3
    2. Create #defines for all the other sizes as well (#define ID_LEN 12) and so on.
    Then replace all the other magic numbers for your array sizes scattered through the code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 08-10-2012, 04:18 AM
  2. Replies: 0
    Last Post: 01-23-2011, 10:56 PM
  3. WPF Delete/Update/Insert Buttons
    By Tester84 in forum C# Programming
    Replies: 1
    Last Post: 12-06-2010, 11:02 PM
  4. Update Record & Delete Record in File.
    By unsafe_pilot1 in forum C Programming
    Replies: 13
    Last Post: 05-18-2008, 08:22 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21