Files....

This is a discussion on Files.... within the C Programming forums, part of the General Programming Boards category; hi ... i got a problem with file... i wrote this without the aid of a struct... i just added ...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    12

    Files....

    hi ...

    i got a problem with file...
    i wrote this without the aid of a struct...

    i just added functions...to add,displat,search,delete..
    this is meant to be a very easy way to use files...

    though i know how to use struct...i jus wanted to try this way..

    Code:
    add()
    {
    clrscr();
    char Name[40];
    int ID;
    char Gender;
    float Amt;
    FILE *sfile;
    
    if ((sfile=fopen("stud.dat","a+"))==NULL)
    ID=1;
    else
    {
    do{
    fscanf(sfile,"%i%[^/]%*c%c%f%*c",&ID,&Name,&Gender,&Amt);
    }while (!feof(sfile));
    ID+=1;
    }
    
    printf("Student ID:%04i",ID);
    printf("\nName:    ");
    fflush(stdin);
    gets(Name);
    printf("Gender:    ");
    scanf("%c",&Gender);
    printf("Amount:    ");
    scanf("%f",&Amt);
    printf("\nRecord saved!");
    fprintf(sfile,"%04i %s/ %c %.2f\n",ID,Name,Gender,Amt);
    fclose(sfile);
    main();
    }
    
    display()
    {
    clrscr();
    char Name[40];
    int ID;
    char Gender;
    float Amt;
    FILE *sfile;
    
    if ((sfile=fopen("stud.dat","a+"))==NULL)
    printf("No Records!");
    else
    {
    while(!feof(sfile))
    {
    fscanf(sfile,"%i%[^/]%*c%c%f%*c",&ID,&Name,&Gender,&Amt);
    if (feof(sfile))
    break;
    
    
    printf("%04i %s %c %.2f\n",ID,Name,Gender,Amt);
    
    }
    }printf("\n---END OF FILE---");
    fclose(sfile);
    getch();
    main();
    }
    
    search()
    {
    clrscr();
    char Name[40],Target[40];
    int ID,Found=0;
    char Gender;
    float Amt;
    FILE *sfile;
    
    if ((sfile=fopen("stud.dat","r"))==NULL)
    printf("No Records!");
    else
    {
    printf("Enter Name to search: ");
    fflush(stdin);
    gets(Target);
    while (!feof(sfile)&&Found ==0)
    {
    fscanf(sfile,"%i%[^/]%*c%c%f%*c",&ID,&Name,&Gender,&Amt);
    if (strcmp(Target,Name)==0)
    Found=1;
    }
    if (Found)
    {
    printf("ID:   %04i\n",ID);
    printf("Name:  %s\n",Name);
    if (toupper(Gender)=='F')
    printf("Gender: Felmale\n");
    else
    printf("Gender: Male\n");
    printf("Amount: %.2f\n",Amt);
    }
    }
    fclose(sfile);
    getch();
    main();
    }
    
    del()
    {
    clrscr();
    char Name[40],Target[40];
    int ID,Found=0;
    char Gender;
    float Amt;
    FILE *sfile, *temp;
    
    temp=fopen("tmp.dat","w");
    if ((sfile=fopen("stud.dat","r"))==NULL)
    printf("No Records!");
    else
    {
    printf("Enter Name to delete: ");
    fflush(stdin);
    gets(Target);
    while (!feof(sfile))
    {
    fscanf(sfile,"%i%[^/]%*c%c%f%*c",&ID,&Name,&Gender,&Amt);
    if (strcmp(Target,Name)==0)
    
    fprintf(temp,"%i %s/ %c %f\n",ID,Name,Gender,Amt);
    else
    {
    Found=1;
    printf("ID:   %04i\n",ID);
    printf("Name:  %s\n",Name);
    if (toupper(Gender)=='F')
    printf("Gender: Felmale\n");
    else
    printf("Gender: Male\n");
    printf("Amount: %.2f\n",Amt);
    }
    }
    if (!Found)
    printf("Record not found!\n");
    }
    printf("\nRecord deleted!");
    fclose(sfile);
    fclose(temp);
    remove("stud.dat");
    rename("temp.dat","stud.dat");
    getch();
    main();
    }
    i know at the moment that gets() is not good...but i have limited choices....

    the problem is i can make one entry but there is an error on display...
    school work..
    thx....
    Last edited by M_amaR; 10-29-2003 at 07:51 PM.

  2. #2
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161
    Damn, even when you force the use of code tags, they don't even format the code legibly! Who taught you that left-justified code is the way to program? Format it properly and repost it!!!!
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Code:
    clrscr();
    fflush(stdin);
    scanf("%c",&Gender);
    gets(Name);
    getch();
    while(!feof(sfile))
    I guess the only thing left is that we can hope that it's a void main() that's being called recursively!
    i know at the moment that gets() is not good...but i have limited choices....
    Okay, but this is not the way to show that you are serious about learning anything.
    school work..
    Frightening... "Happy Halloween!" to you too!
    Last edited by Dave_Sinkula; 10-29-2003 at 11:20 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Registered User
    Join Date
    Oct 2003
    Posts
    12
    ok .....Walt didn't mean to cause an alarm ...

    i wrote this code in a bit of a hurry.... didn't indent !!

    well as for this code...i decided I jus write it with a struct.....

    and if i have any problems i post it... and this time ...i'll write the code properly.....

    and Dave_Sinkula, happy holloween...


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ressources files
    By mikahell in forum Windows Programming
    Replies: 4
    Last Post: 06-19-2006, 06:50 AM
  2. add source files to embedded VC 4.0
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2006, 03:28 AM
  3. *.cpp and *.h files understanding
    By ElastoManiac in forum C++ Programming
    Replies: 4
    Last Post: 06-11-2006, 04:45 AM
  4. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 10:46 AM
  5. Multiple Cpp Files
    By w4ck0z in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 01:41 PM

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