First record returning null??

This is a discussion on First record returning null?? within the C Programming forums, part of the General Programming Boards category; Hi, I'm having some trouble with this file which ultimately needs linking to two other files, Stock, and Order. The ...

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    1

    First record returning null??

    Hi,

    I'm having some trouble with this file which ultimately needs linking to two other files, Stock, and Order.

    The problem is that after adding the first record, I debug the code posted below and it doesn’t seem to recognize the first record I added, which causes trouble when adding the second record. It adds the node correctly which causes the cycle to go through the correct number of times, but when it gets to the last cycle it is returned a NULL value, which is supposed to be the first record I added.

    Can anyone spot the flaw in the code?

    Code:
     
    #include <stdio.h>
    #include 
    #include <string.h>
    #include 
    
    FILE *fSupplier;
    void DisplaySupplierMenu();
    void SearchBySupplierCode();
    void AddSupplierRecord();
    void AmendSupplierRecord();
    bool SearchSupplier();
    void DisplaySearchedSupplierRecord();
    bool WriteNewSupplierRecord();
    
    void LoadSupplierRecord(struct SupplierRefer **q);
    void AddSupplierNode(struct SupplierRefer **q, int num);
    void AssignSupplierRefer(struct SupplierRefer **p);
    
    void OpenSupplierFile();
    void CloseSupplierFile();
    
    int CurrSupplierRecNum=0;
    int TotalSupplierNode=0;
    char SearchedSupplierCode[7];
    
    struct Supplier{
      char SupplierCode[7];
      char SupplierName[21];
      char SupplierAddress[21];
      char SupplierPhone[11];
    };
    
    struct Supplier Supplier;
    
    struct SupplierRefer{
      char SupplierCode[7];
      struct SupplierRefer *Supplierlink;
    };
    
    struct SupplierRefer *mySupplierrefer;
    
    int main() {
      mySupplierrefer=(struct SupplierRefer*)malloc(sizeof(struct 
                                  SupplierRefer));
      LoadSupplierRecord(&mySupplierrefer);
      bool isExit=false;
      while (!isExit) {
        DisplaySupplierMenu();
        char ch;
        gets(&ch);
        
        switch (ch) {
    
        case '1':
          SearchBySupplierCode();
          break;
        case '2':
          AddSupplierRecord();
          break;
        case '3':
          AmendSupplierRecord();
          break;
        case '0':
          isExit=true;
        }
      }
      return 0;
    }
    
    void DisplaySupplierMenu() {
      puts(" \n");
      puts(" * 1 : View Supplier Record *\n");
      puts(" * 2 : Add Supplier Record *\n");
      puts(" * 3 : Amend Supplier Record *\n");
      puts(" * 0 : Exit *\n");
      puts(" \n");
    }
    
    void OpenSupplierFile() {
      fSupplier=fopen("a:\\Supplier.txt", "r+");
      
      if (fSupplier==0) {
        fSupplier=fopen("a:\\Supplier.txt", "w");
        fclose(fSupplier);
        fSupplier=fopen("a:\\Supplier.txt", "r+");
      }
    }
    
    void CloseSupplierFile() {
      fclose(fSupplier);
    }
    
    void SearchBySupplierCode() {
      OpenSupplierFile();
      puts("Enter Supplier Code For The Record You Want To  
                Search\n");
      gets(SearchedSupplierCode);
    
      if (SearchSupplier()) {
        DisplaySearchedSupplierRecord();
        CloseSupplierFile();
        return;
      }
      else {
        printf("Supplier Code Wasn't Found");
        return;
      }
    }
    
    void DisplaySearchedSupplierRecord() {
      long offset;
      printf("Searched Record Details\n");
    
      offset=sizeof(Supplier)* CurrSupplierRecNum;
      fseek(fSupplier,offset,0);
      fread(&Supplier, sizeof(Supplier), 1, fSupplier);
      puts("\nSupplier Code");
      puts(Supplier.SupplierCode);
      puts("\nSupplier Name");
      puts(Supplier.SupplierName);
      puts("\nSupplier Address");
      puts(Supplier.SupplierAddress);
      puts("\nSupplier Phone");
      puts(Supplier.SupplierPhone);
      printf("\nSearched Successfully");
    }
    
    void AddSupplierRecord() {
      puts("\nEnter New Supplier Code\n");
      gets(SearchedSupplierCode);
      OpenSupplierFile();
      
      while(true)   {
        if (SearchSupplier()) {
          printf("\nEntered Supplier Code Already Exixts\n");
          puts("\nEnter New Supplier Code\n");
          gets(SearchedSupplierCode);
        }
        else {
          if (WriteNewSupplierRecord()) {
            AddSupplierNode(&mySupplierrefer, TotalSupplierNode);
            CloseSupplierFile();
            return;
          }
          else {
            CloseSupplierFile();
            return;
          }
        }
      }
      CloseSupplierFile();
    }
    
    bool WriteNewSupplierRecord() {
      long offset;
      char temp_name[21];
      char temp_address[21];
      char temp_phone[11];
      offset=sizeof(Supplier)* TotalSupplierNode;
      fseek(fSupplier, offset, 0);
    
      fflush(stdin);
    
      if(strlen(SearchedSupplierCode)== 6) {
        strcpy(Supplier.SupplierCode, SearchedSupplierCode);
        puts("Enter Value For Supplier Name, Should Have Atleast 
                  Single Character\n");
        gets(temp_name);
    
        if(strlen(temp_name)>0 & strlen(temp_name)<21) {
          strcpy(Supplier.SupplierName, temp_name);
          puts("Enter Value For Supplier Address\n");
          gets(temp_address);
    
           if(strlen(temp_address)>0 & strlen(temp_address)<21) {
             strcpy(Supplier.SupplierAddress, temp_address);
             puts("Enter Value For Supplier Phone Number, Should Be    
                       Between 1-10 Characters");
             gets(temp_phone);
    
             if(strlen(temp_phone)>0 & strlen(temp_phone)<11) {
               strcpy(Supplier.SupplierPhone, temp_phone);
               fwrite(&Supplier, sizeof(Supplier), 1, fSupplier);
               puts("New Record Added Successfully");
               return true;
             }
             else {
               puts("Enterd Supplier Phone Number Is Not Valid, Should 
                         Be Between 1-10 Characters");
               return false;
             }
           }
           else {
             puts("Enterd Supplier Address Is Not Valid, Should Be 
                       Between 1-20 Characters");
             return false;
           }
         }
         else {
           puts("Enterd Supplier Name Is Not Valid, Should Be Between 
                     1-20 Characters, Should Have Atleast Single 
                     Character");
           AddSupplierRecord();
         }
       }
       else {
         puts("Enterd Supplier Code Is Not Valid, Should Be 6 
                   Characters\n");
         AddSupplierRecord();
       }
    }
    
    void AmendSupplierRecord() {
      long offset;
      char temp_name[21];
      char temp_address[21];
      char temp_phone[11];
    
      fflush(stdin);
      puts("\nEnter Supplier Code You Want To Amend\n");
      gets(SearchedSupplierCode);
      OpenSupplierFile();
    
      if (SearchSupplier()) {
        offset=sizeof(Supplier)* CurrSupplierRecNum;
        fseek(fSupplier, offset, 0);
        fread(&Supplier, sizeof(Supplier), 1, fSupplier);
    
        puts(Supplier.SupplierName);
        puts("\nEnter New Value For Supplier Name\n");
        gets(temp_name);
    
        if(strlen(temp_name)>0 & strlen(temp_name)<21) 
          strcpy(Supplier.SupplierName, temp_name);
        else {
          printf("Entered Supplier Name Is Not Valid, Should Be 
                     Between 1-20 Characters");
          return;
        }
    
        puts(Supplier.SupplierAddress);
        puts("Enter New Value For Supplier Address\n");
        gets(temp_address);
    
        if(strlen(temp_address)>0 & strlen(temp_address)<21) 
          strcpy(Supplier.SupplierAddress, temp_address);
        else {
          printf("Entered Supplier Address Is Not Valid, Should Be 
                     Between 1-20 Characters");
          return;
        }
        puts(Supplier.SupplierPhone);
        puts("Enter New Value For Supplier Phone Number\n");
        gets(temp_phone);
    
        if(strlen(temp_phone)>0 & strlen(temp_phone)<11)
          strcpy(Supplier.SupplierPhone, temp_phone);
        else {
          printf("Entered Supplier Phone Number Is Not Valid, Should 
                     Be Between 1-10 Characters");
          return;
        }
        fseek(fSupplier, offset, 0);
        fwrite(&Supplier, sizeof(Supplier),1,fSupplier);
        fflush(stdin);
    
        puts("Record Successful Amended");
        CloseSupplierFile();
        LoadSupplierRecord(&mySupplierrefer);
      }
      else {
        printf("Searched Supplier Code Does Not Exist");
        fflush(stdin);
      }
    }
    
    bool SearchSupplier() {
      int index=0;
      struct SupplierRefer *temp;
      temp=mySupplierrefer->Supplierlink;
    
      if (TotalSupplierNode==0 ) {
        puts("File Is Empty.");
        return false;
      }
      while (index<=TotalSupplierNode-1) {
        if (strcmp(SearchedSupplierCode,temp->SupplierCode)==0) {
          CurrSupplierRecNum=index;
          return true;
        }
        else {
          index+=1;
          temp=temp->Supplierlink;
        }
      }
      return false;
    }
    
    bool FindSupplierName() {
      OpenSupplierFile();
      return false ;
    }
    
    void LoadSupplierRecord(struct SupplierRefer **q) {
      long offset;
      struct SupplierRefer *temp;
      temp=*q;
      TotalSupplierNode=0;
      OpenSupplierFile();
    
      while(true) {
        if(fread(&Supplier, sizeof(Supplier),1,fSupplier)==1) {
          if (temp==NULL) {
            temp=(struct SupplierRefer*)malloc(sizeof(struct 
                        SupplierRefer));
            AssignSupplierRefer(&temp);
          }
          else {
            temp->Supplierlink=(struct SupplierRefer*)malloc(sizeof
                                            (struct SupplierRefer) );
            temp=temp->Supplierlink;
            AssignSupplierRefer(&temp);
          }
        }
        else {
          CloseSupplierFile();
          break;
        }
      }
      return;
    }
    
    void AssignSupplierRefer(struct SupplierRefer **p) {
      struct SupplierRefer *temp;
      temp=*p;
    
      long offset=sizeof(Supplier)*TotalSupplierNode;
      fseek(fSupplier, offset, 0);
      fread(&Supplier, sizeof(Supplier), 1,fSupplier);
      strcpy(temp->SupplierCode,Supplier.SupplierCode);
      TotalSupplierNode+=1;
      return;
    }
    
    void AddSupplierNode(struct SupplierRefer **q, int num) {
      int index;
      struct SupplierRefer *temp;
    
      if (TotalSupplierNode==0 || *q==NULL) {
        *q=(struct SupplierRefer*)malloc(sizeof(struct SupplierRefer));
        temp=*q;
        AssignSupplierRefer(&temp);
        return;
      }
      else {
        while(index<=TotalSupplierNode) {
          temp=temp->Supplierlink;
        }
        temp=(struct SupplierRefer*)malloc(sizeof(struct 
                                                                 SupplierRefer) );
        AssignSupplierRefer(&temp);
    
      }
      return;
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Sorry, the code was too long for me to get past the fflush(stdin)'s, the gets', the seeking in a non-binary oriented file, the casting of malloc without including stdlib.h, and this little gem:
    Code:
    char ch;
    gets(&ch);
    I don't see that one too often, but it makes me smile every time.
    My best code is written with the delete key.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Don't forget the lack of a return true/false in a bool function.

    Is bool valid C? Or maybe it just isn't C89.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    _Bool is a valid type in C99.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Is bool valid C?
    If you include stdbool.h it is. Otherwise you're stuck with plain old _Bool for C99 and the appropriate hack for C89.
    My best code is written with the delete key.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    I though so.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well, you thought wrong. There is no bool type in C. It is a macro in C99. The only boolean type is _Bool.

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Well, you thought wrong. There is no bool type in C. It is a macro in C99. The only boolean type is _Bool.
    Who said anything about a type?
    Quote Originally Posted by dwks
    Is bool valid C? Or maybe it just isn't C89.
    Quote Originally Posted by me
    If you include stdbool.h it is. Otherwise you're stuck with plain old _Bool for C99 and the appropriate hack for C89.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked List Not Saving Value as Int
    By bar338 in forum C Programming
    Replies: 4
    Last Post: 05-04-2009, 07:53 PM
  2. Tweakable Radar...
    By DoraTehExploda in forum Game Programming
    Replies: 8
    Last Post: 06-07-2005, 10:49 AM
  3. Help with yacc/compiler design/seg fault
    By trippeer in forum C Programming
    Replies: 1
    Last Post: 04-08-2005, 03:43 AM
  4. Wierd Segmentation Faults on Global Variable
    By cbranje in forum C Programming
    Replies: 6
    Last Post: 02-19-2005, 11:25 AM
  5. opengl program as win API menu item
    By SAMSAM in forum Game Programming
    Replies: 1
    Last Post: 03-03-2003, 06:48 PM

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