help regarding file handling...

2»

Comments

  • hi there=] is this a turbo c program? what compiler are you using? =]



    [img]http://dl4.glitter-graphics.net/pub/493/493254uhpm9tl406.gif[/img]
  • Look at the variable location you have defined. And in cin you are using location1, a totally different variable or do I assume that was a typo?
  • i already corrected it, its still not displaying the first record...

    [code]#include
    #include
    #include
    #include
    #include
    #define maxstring 30


    struct emp_info
    {
    char firstname[10], lastname[20], address[30];
    int id, age;
    }employee;

    void menu();
    void add();
    void readfile();
    void delfile();
    void getrecord();
    char *stringcapitalize(char *);
    char buffer[maxstring + 1];



    void main()
    {
    clrscr();
    menu();
    getche();
    }



    void menu()
    {
    clrscr();
    int choice;
    cout<<"MENU"<<endl;
    cout<<"1. Add Employee"<<endl;
    cout<<"2. Read Complete File."<<endl;
    cout<<"3. Delete all."<<endl;
    cout<<"4. getrecord"<<endl;
    cout<<"5. Exit"<<endl;
    cout<<"Enter your choice... ";
    cin>>choice;
    switch(choice)
    {
    case 1:
    clrscr();
    add();
    break;

    case 2:
    clrscr();
    readfile();
    break;

    case 3:
    clrscr();
    delfile();
    break;

    case 4:
    clrscr();
    getrecord();
    break;

    case 5:
    exit(0);
    }
    }

    void add()
    {

    char choice;
    FILE *pf;
    pf=fopen("D:\record.txt","a");
    if((pf=fopen("D:\record.txt", "a"))==NULL)
    {
    cout<<"could not open file";
    exit(1);
    }

    cout<<"Enter first name.. ";
    gets(employee.firstname);
    cout<<endl;
    cout<<"Enter last name.. ";
    gets(employee.lastname);
    cout<<endl;
    cout<<"Enter address.. ";
    gets(employee.address);
    cout<<endl;
    cout<<"Enter employee id ... ";
    cin>>employee.id;
    cout<<endl;
    cout<<"Enter employee age";
    cin>>employee.age;
    cout<<endl<<endl;
    fwrite(&employee, sizeof(employee),1,pf);
    fclose(pf);
    cout<<"Do you wish to add another record? (y\n)";
    cin>>choice;
    switch(choice)
    {
    case 'y':
    case 'Y':
    clrscr();
    add();
    break;
    default:
    menu();
    }
    }

    void readfile()
    {
    FILE *pf;
    pf=fopen("D:\record.txt","r");
    if((pf=fopen("D:\record.txt", "r"))==NULL)
    {
    cout<<"could not open file";
    }
    while (fread(&employee, sizeof(employee),1,pf)==1)
    {
    printf("%s
    ", stringcapitalize(employee.firstname));
    printf("%s
    ", stringcapitalize(employee.lastname));
    cout<<employee.address<<endl;
    cout<<employee.id<<endl;
    cout<<employee.age<<endl<<endl<<endl;
    }
    fclose(pf);
    getche();
    menu();
    }


    void delfile()
    {
    clrscr();
    char choice;
    cout<<"Are you sure (y\n)??"<<endl;
    cin>>choice;
    switch(choice)
    {
    case 'y':
    case 'Y':

    remove("D:\record.txt");
    if(remove("D:\record.txt")==NULL)
    {
    cout<<"Oops, file not deleted";
    }
    else
    {
    cout<<"deleted";
    }
    getche();
    menu();
    break;
    default:
    menu();
    break;
    }
    }

    void getrecord()
    {
    clrscr();
    char choice;
    int location;
    FILE *pf;
    pf=fopen("D:\record.txt", "r");
    cout<<"Enter record number to be read... ";
    cin>>location;
    location=location-1;
    fseek(pf, location*sizeof(employee), SEEK_SET);
    fread(&employee, sizeof(employee),1,pf);

    if(fread(&employee, sizeof(employee),1,pf)==NULL)
    {
    cout<<"Oops, file not found";
    }
    else
    {
    cout<<employee.firstname<<endl;
    cout<<employee.lastname<<endl;
    cout<<employee.address<<endl;
    cout<<employee.id<<endl;
    cout<<employee.age<<endl;
    }
    getch();
    fclose(pf);
    cout<<"



    get record agen (y\n)??"<<endl;
    cin>>choice;
    switch(choice)
    {
    case 'y':
    case 'Y':

    getrecord();
    getche();
    break;
    default:
    menu();
    break;
    }
    }



    char *
    stringcapitalize(char *string)
    {
    char *cp;

    cp=string;
    *cp=toupper(*cp);
    ++cp;
    while (*cp != '')
    {
    *cp = tolower(*cp);
    ++cp;
    }

    return (string);
    }[/code]
  • hello...yes its turbo c...
  • ayt? okay2, il try running this one again..hmm.. looks like there are lots of errors huh. idunno, how to CHECk those.
    [img]http://dl4.glitter-graphics.net/pub/493/493254uhpm9tl406.gif[/img]
  • This post has been deleted.
  • AsmGuru62, toddlerasim, Lundin...

    thank you so much for all of your help...

    thanks for wasting your time on me...

    ^_^
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories