file processing updating record error

This is a discussion on file processing updating record error within the C Programming forums, part of the General Programming Boards category; can any1 tell me wat i am doing wrong? when i search for a name which is in the record ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    3

    file processing updating record error

    can any1 tell me wat i am doing wrong?
    when i search for a name which is in the record for updating, it says name not found. the only name i can update is the first one
    ?????
    #include <conio.h>
    #include <ctype.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    #include "function.h"
    #include "structure.h"
    #include "screen.h"




    /*************************External Global variables - in Mainmenu.c ***/
    contactFileN[30];
    extern FILE *fpC;

    /************************************ For OPtion "Open Files" ***/
    void OpenFiles ( void)
    {
    char fileTitle[30];
    char reply;

    printf ("\n\nOpen Files\n");
    printf ("------------\n\n");
    printf ("Enter the file name (without file extension) : ");

    fflush (stdin);
    gets (fileTitle);



    strcpy ( contactFileN, fileTitle);
    strcat ( contactFileN, ".1");

    /********************************* Open Basic File ***/
    fpC = fopen (contactFileN, "r+b");
    if (fpC == NULL)
    {
    printf ("File open error on %s\n", contactFileN);
    printf("Do you want to create new file (y/n) ?\n");
    fflush(stdin);
    scanf("%c", &reply);
    reply=tolower(reply);

    if (reply=='y')
    {
    fpC = fopen(contactFileN, "w+b");
    printf("%s file creation successful\n", contactFileN);

    }
    else
    {
    printf ("Press <ENTER> to continue\n");
    fflush(stdin); getchar();
    return;
    }
    }
    else
    {
    printf("File %s open successfully\n", contactFileN);
    }


    }//OpenFiles




    /************************* Events for Add Records Option ***/
    void AddRecords( void )
    {
    /* ?????????????????????????????????????????? */

    struct contact y;

    printf ("\n\nKey in the following information :\n");

    //Infor for basic file

    y.status = 1; //record active

    printf ("Name : ");
    fflush (stdin);
    gets (y.name);

    printf ("Key in an unique id code (numeral only) :");
    scanf ("%ld", &y.id);

    printf ("Birth dates (dd mm yyyy) : ");
    scanf ("%d %d %d", &y.birth.dd, &y.birth.mm, &y.birth.yy);

    printf ("Sex Code (M-male, F-Female) : ");
    fflush (stdin);
    y.sex= getchar();

    //Infor for Contact file

    printf("Enter status (1 for active, -1 for inactive) : ");
    scanf("%d", &y.status);

    printf("Address : \n");
    printf("Enter Street : ");
    fflush(stdin);
    gets(y.addr1.street);

    printf("Enter Postal Code : ");
    scanf("%ld", &y.addr1.pCode);

    printf("Enter Country : ");
    fflush(stdin);
    gets(y.addr1.country);

    printf ("Key in Telephone no (Press -9 if none) : ");
    scanf("%ld", &y.tel1);

    printf("Key in Mobile Phone no (Press -9 if none) : ");
    scanf("%ld", &y.mp1);

    printf("Key in Pager no (Press -9 if none) : ");
    scanf("%ld", &y.pg);

    printf("Key in Email address : ");
    fflush(stdin);
    gets(y.email);

    //Write to Basic File
    fseek (fpC, 0L, SEEK_END);
    fwrite (&y, sizeof(y), 1, fpC);


    printf ("One records added to the following two files : %s\n",
    contactFileN);

    printf ("Press <ENTER> for next record\n");
    fflush (stdin);
    getchar();
    }//AddRecords


    void UpdateInfo(void)
    {

    struct contact y;
    long ntel1;
    long nmp1;
    long npg;
    char nemail[30];
    char nstreet[51];
    long npCode;
    char ncountry[20];

    int i=0;
    int found=0;
    char name[30];

    char fileTitle[30];

    printf ("Enter the basic file name (without file extension) : ");

    fflush (stdin);
    gets (fileTitle);


    strcpy ( contactFileN, fileTitle);
    strcat ( contactFileN, ".1");

    /********************************* Open Basic File ***/
    fpC = fopen (contactFileN, "r+b");
    if (fpC==NULL)
    {
    printf("Error Opening File..press ENTER to continue\n");
    fflush(stdin);
    getchar();
    return;
    }

    printf ("%s file open successful\n",contactFileN);



    printf("Please enter your name : ");
    fflush(stdin);
    gets(name);

    //rewind the pointer

    rewind(fpC);

    while (1)
    {
    //read the file

    if (fread(&y, sizeof(y), 1, fpC)==0)
    {
    break;
    }

    rewind(fpC);
    //search through the file
    if (strcmpi(name, y.name)==0)
    {
    found=1;
    break;
    }
    else
    {
    printf("Name not found. Press ENTER to continue.\n");
    fflush(stdin);
    getchar();
    return;
    }
    i++;
    }//while
    if (found=1)
    {
    printf("Enter New Address : \n");
    printf(" Enter new street : ");
    fflush(stdin);
    gets(nstreet);
    printf(" Enter new postal code : ");
    scanf("%ld", &npCode);
    printf("Enter new country : ");
    fflush(stdin);
    gets(ncountry);
    printf("Enter new Phone (-9 if none) : ");
    scanf("%ld", &ntel1);
    printf("Enter new Mobile (-9 if none) : ");
    scanf("%ld", &nmp1);
    printf("Enter new Pager : (-9 if none) : ");
    scanf("%ld", &npg);
    printf("Enter new Email : ");
    fflush(stdin);
    gets(nemail);


    //position the pointer to current position
    fseek(fpC, -(long)sizeof(y),SEEK_CUR);


    strcpy(y.addr1.street, nstreet);
    strcpy(y.email, nemail);
    strcpy(y.addr1.country, ncountry);
    y.mp1= nmp1;
    y.pg=npg;
    y.tel1=ntel1;
    y.addr1.pCode=npCode;

    //updates the file
    fwrite(&y, sizeof(y), 1, fpC);
    rewind(fpC);


    printf("Congratulations! Record updated successfully!\n");

    printf("\nPress ENTER to continue\n");
    fflush(stdin);
    getchar();
    }
    fclose(fpC);

    } // UpdateInfo

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    A few things:

    Number one: code tags . Please use them.

    >>fflush (stdin);
    Read this

    >>gets (fileTitle);
    Read this

    >>strcat ( contactFileN, ".1");
    Are you sure there's 2 bytes of room left in that array before you concat them on?

    >>rewind(fpC);
    >>if (strcmpi(name, y.name) == 0)
    Why are you rewinding the file every time through the loop? You don't need to do this, it will cause you to only ever read the first struct in. Also, your loop control and logic is incorrect in the same area. Here is a snippet of your code, follow the flow and see if you can work out your mistakes:
    Code:
      while (1)
      {
        //read the file
        if (fread(&y, sizeof(y), 1, fpC) == 0)
        {
          break;
        }
    
        rewind(fpC);
    
        //search through the file
        if (strcmpi(name, y.name) == 0)
        {
          found = 1;
          break;
        }
        else
        {
          printf("Name not found. Press ENTER to continue.\n");
          fflush(stdin);
          getchar();
          return;
        }
    
        i++;
      }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    3
    ok, thanks Hammer. I can spot my mistake now. My if/else statements were mixed up. Thanks for helping me spot my mistakes!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  3. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 12:10 AM
  4. UNICODE and GET_STATE
    By Registered in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2002, 03:23 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

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