Not writing to a file.

This is a discussion on Not writing to a file. within the C Programming forums, part of the General Programming Boards category; There is something wrong with my program and although its creating the file, its not displaying the results. Can someone ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    1

    Unhappy Not writing to a file.

    There is something wrong with my program and although its creating the file, its not displaying the results. Can someone help please? Here is the code:

    #include <stdio.h>
    #include <conio.h>
    #include <string.h>

    /* Define Constants */
    #define MAXMEMBERS 100
    #define FILENAME "C:\\FileHandling\\Member.bin"

    /* Declare Structure Templates */
    typedef struct Member_Details
    {
    char Memb_No[4];
    char Memb_Name[23];
    char Memb_Street[20];
    char Memb_Town[20];
    char Memb_County[20];
    char Memb_ContactNo[15];
    }MEMBER;

    /* Variable that counts total records in the file */
    int TotalRecords=0;

    /* Function Prototypes */
    void WriteRecordsToFile (MEMBER[MAXMEMBERS]);
    int ReadRecordsFromFile (MEMBER[MAXMEMBERS]);

    void AddRecord(MEMBER[MAXMEMBERS]);
    void DeleteRecord(MEMBER[MAXMEMBERS]);
    void ViewRecord(MEMBER[MAXMEMBERS]);
    void ListRecords(MEMBER[MAXMEMBERS]);
    void AmendRecord(MEMBER[MAXMEMBERS]);

    void Menu(MEMBER[MAXMEMBERS]);

    void main()
    {
    /* Declare array suitable for storing MAXMEMBERS Members */
    MEMBER Members[MAXMEMBERS];

    TotalRecords = ReadRecordsFromFile(Members);
    Menu(Members);
    }


    /************************************************** ***************************/
    /* Function Name : WriteRecordsToFile */
    /* Return Type : void - Returns Nothing */
    /* Parameters : None */
    /* Purpose : Writes all record from Member array into Member File. */
    /************************************************** ***************************/
    void WriteRecordsToFile(MEMBER Members[MAXMEMBERS])
    {
    FILE *FilePtr;
    int i;

    /* Open the File in binary mode for reading writing only using fopen */
    if( (FilePtr=fopen(FILENAME, "rb")) == NULL)
    printf("ERROR OPENING FILE");
    else /* File opened properly */
    {
    /* For every record in the file... */
    for(i=0;i<TotalRecords;i++)
    if(strcmp(Members[i].Memb_No,"\0") != 0)
    fwrite(&Members[i], sizeof(Members[i]), 1, FilePtr);
    }
    fclose(FilePtr);
    }

    /************************************************** ***************************/
    /* Function Name : ReadRecords */
    /* Return Type : int - Returns Number of Records */
    /* Parameters : None */
    /* Purpose : Reads Member Records from a file. */
    /************************************************** ***************************/
    int ReadRecordsFromFile(MEMBER Members[MAXMEMBERS])
    {
    FILE *FilePtr;
    int Filesize, Num_Records, Cur_Rec;

    if((FilePtr=fopen(FILENAME, "wb")) == NULL )
    printf("FILE NOT OPEN");
    else {
    fseek(FilePtr, 0L, SEEK_END);
    Filesize=ftell(FilePtr);
    Num_Records=Filesize;
    fseek(FilePtr, 0L, SEEK_SET);

    for(Cur_Rec=0; Cur_Rec<Num_Records; Cur_Rec++)
    fread(&Members[Cur_Rec], sizeof (Members[Cur_Rec]), 1, FilePtr);
    }
    fclose(FilePtr);
    return Num_Records;
    }

    /************************************************** ***************************/
    /* Function Name : AddRecord */
    /* Return Type : void - Returns Nothing */
    /* Parameters : None */
    /* Purpose : Adds a member to the Member array. */
    /************************************************** ***************************/
    void AddRecord (MEMBER Members[MAXMEMBERS])
    {
    clrscr();

    /* Read details into next array element */
    printf("\nEnter Member Details:\n");
    printf("\t\tMember Number : ");
    gets(Members[TotalRecords].Memb_No);
    printf("\t\tMember Name : ");
    gets(Members[TotalRecords].Memb_Name);
    printf("\t\tMember Street : ");
    gets(Members[TotalRecords].Memb_Street);
    printf("\t\tMember Town : ");
    gets(Members[TotalRecords].Memb_Town);
    printf("\t\tMember County : ");
    gets(Members[TotalRecords].Memb_County);
    printf("\t\tMember Contact No. : ");
    scanf("%c",Members[TotalRecords].Memb_ContactNo);
    fflush(stdin);

    /* and Increment No. Records*/
    TotalRecords++;

    WriteRecordsToFile(Members);
    }

    /************************************************** ***************************/
    /* Function Name : DeleteRecord */
    /* Return Type : void - Returns Nothing */
    /* Parameters : MEMBER [MAXMEMBERS] - Memberss Array */
    /* Purpose : Deletes a member from the Member array. */
    /************************************************** ***************************/
    void DeleteRecord(MEMBER Members[MAXMEMBERS])
    {
    char Memb_NoToDelete[20], Answer;
    int Found=0, CurrentMember=0;

    clrscr();

    printf("Enter Number of Member to be Deleted : ");gets(Memb_NoToDelete);

    while( (CurrentMember < TotalRecords) && !Found)
    {
    if(strcmp(Members[CurrentMember].Memb_No,Memb_NoToDelete)==0)
    {
    Found=1;

    printf("\nMember Details:\n");
    printf("\t\tMember No: ",Members[CurrentMember].Memb_No);
    printf("\n\t\tMember Name: ",Members[CurrentMember].Memb_Name);
    printf("\n\t\tMember Street: ",Members[CurrentMember].Memb_Street);
    printf("\n\t\tMember Town: ",Members[CurrentMember].Memb_Town);
    printf("\n\t\tMember County: ",Members[CurrentMember].Memb_County);
    printf("\n\t\tMember Contact No. : ",Members[CurrentMember].Memb_ContactNo);

    /* Confirm that record is to be deleted */
    printf("\n\nAre you sure you wish to Delete this Member [Y/N] : ");Answer=getchar();

    if(Answer=='Y' || Answer=='y')
    { /* Delete Record by setting its elements back to zero */
    strcpy(Members[CurrentMember].Memb_No,"\0");
    strcpy(Members[CurrentMember].Memb_Name,"\0");
    strcpy(Members[CurrentMember].Memb_Street,"\0");
    strcpy(Members[CurrentMember].Memb_Town,"\0");
    strcpy(Members[CurrentMember].Memb_County,"\0");
    strcpy(Members[CurrentMember].Memb_ContactNo,"\0");
    }
    else
    printf("Record not deleted...");
    }
    else
    /* Move on to next Member in array */
    CurrentMember++;
    }

    if(!Found) /* If the Record was not found */
    printf("RECORD not found in FILE...");
    getch();
    }

    /************************************************** ***************************/
    /* Function Name : ViewRecord */
    /* Return Type : void - Returns Nothing */
    /* Parameters : MEMBER[MAXMEMBERS] - MEMBERS Array */
    /* Purpose : Displays a member in the Member array. */
    /************************************************** ***************************/
    void ViewRecord(MEMBER Members[MAXMEMBERS])
    {
    char Memb_NoToView[20];
    int Found=0, CurrentMember=0;

    clrscr();

    printf("Enter Number of Member to be Viewed : ");gets(Memb_NoToView);

    while( (CurrentMember < TotalRecords) && !Found)
    {
    if(strcmp(Members[CurrentMember].Memb_No,Memb_NoToView)==0)
    {
    Found=1;

    printf("\nMember Details:\n");
    printf("\t\tMember No: ",Members[CurrentMember].Memb_No);
    printf("\n\t\tMember Name: ",Members[CurrentMember].Memb_Name);
    printf("\n\t\tMember Street: ",Members[CurrentMember].Memb_Street);
    printf("\n\t\tMember Town: ",Members[CurrentMember].Memb_Town);
    printf("\n\t\tMember County: ",Members[CurrentMember].Memb_County);
    printf("\n\t\tMember Contact No. : ",Members[CurrentMember].Memb_ContactNo);
    }
    else
    CurrentMember++;
    }

    if(!Found) /* If the Record was not found */
    printf("RECORD not found in FILE...");
    getch();
    }

    /************************************************** ***************************/
    /* Function Name : ListRecords */
    /* Return Type : void - Returns Nothing */
    /* Parameters : MEMBER [MAXMMEMBERS] - Member Array */
    /* Purpose : Lists all members in the Member array. */
    /************************************************** ***************************/
    void ListRecords(MEMBER Members[MAXMEMBERS])
    {
    int CurrentMember, Found=0;

    clrscr();

    if(TotalRecords > 0)
    for(CurrentMember=0;CurrentMember < TotalRecords;CurrentMember++)
    {
    if(strcmp(Members[CurrentMember].Memb_No,"\0")!=0)
    {
    /* Display details to screen */
    printf("\nMember Details:\n");
    printf("\t\tMember No: ",Members[CurrentMember].Memb_No);
    printf("\n\t\tMember Name: ",Members[CurrentMember].Memb_Name);
    printf("\n\t\tMember Street: ",Members[CurrentMember].Memb_Street);
    printf("\n\t\tMember Town: ",Members[CurrentMember].Memb_Town);
    printf("\n\t\tMember County: ",Members[CurrentMember].Memb_County);
    printf("\n\t\tMember Contact No. : ",Members[CurrentMember].Memb_ContactNo);

    Found=1;
    }
    }

    if(!Found)
    printf("No RECORDS found in FILE...");
    getch();
    }

    /************************************************** ***************************/
    /* Function Name : AmendRecord */
    /* Return Type : void - Returns Nothing */
    /* Parameters : MEMBER[MAXMEMBERS] - Members Array */
    /* Purpose : Amends a members details in the Member array. */
    /************************************************** ***************************/
    void AmendRecord(MEMBER Members[MAXMEMBERS])
    {
    char Memb_NoToAmend[20], Answer;
    int Found=0, CurrentMember=0;

    clrscr();

    printf("Enter Number of Member to be Amended : ");gets(Memb_NoToAmend);

    while( (CurrentMember < TotalRecords) && !Found)
    {
    if(strcmp(Members[CurrentMember].Memb_No,Memb_NoToAmend)==0)
    {
    Found=1;

    printf("\nMember Details:\n");
    printf("\t\tMember No: ",Members[CurrentMember].Memb_No);
    printf("\n\t\tMember Name: ",Members[CurrentMember].Memb_Name);
    printf("\n\t\tMember Street: ",Members[CurrentMember].Memb_Street);
    printf("\n\t\tMember Town: ",Members[CurrentMember].Memb_Town);
    printf("\n\t\tMember County: ",Members[CurrentMember].Memb_County);
    printf("\n\t\tMember Contact No. : ",Members[CurrentMember].Memb_ContactNo);

    printf("\n\nAre you sure you wish to Amend this Member [Y/N] : ");Answer=getchar();

    if(Answer=='Y' || Answer=='y')
    { /* Read new details into array */
    printf("\nEnter Member Details:\n");
    fflush(stdin);
    printf("\t\tMember No: ");
    gets(Members[TotalRecords].Memb_No);
    printf("\n\t\tMember Name: ");
    gets(Members[TotalRecords].Memb_Name);
    printf("\n\t\tMember Street: ");
    gets(Members[TotalRecords].Memb_Street);
    printf("\n\t\tMember Town: ");
    gets(Members[TotalRecords].Memb_Town);
    printf("\n\t\tMember County: ");
    gets(Members[TotalRecords].Memb_County);
    printf("\n\t\tMember Contact No. : ");
    scanf("%c",Members[TotalRecords].Memb_ContactNo);
    fflush(stdin);
    }
    else
    printf("Record not amended...");
    }
    else
    CurrentMember++;
    }

    if(!Found) /* If the Record was not found */
    printf("RECORD not found in FILE...");
    getch();
    }

    /************************************************** ***************************/
    /* Function Name : Menu */
    /* Return Type : void - Returns Nothing */
    /* Parameters : MEBMER [MAXMEMBERS] - Books Array */
    /* Purpose : Displays menu, reads option and calls relevant function */
    /* based on option entered. */
    /************************************************** ***************************/
    void Menu(MEMBER Members[MAXMEMBERS])
    {
    int Option;

    do{
    /* Clear the screen and display the menu */
    clrscr();
    printf("\t\tMEMBER MAIN MENU\n\n");
    printf("\t\t1. Add Member\n");
    printf("\t\t2. Delete Member\n");
    printf("\t\t3. View Member\n");
    printf("\t\t4. List Members\n");
    printf("\t\t5. Amend Member\n");
    printf("\t\t6. Quit\n");
    printf("\n\t\tEnter Option : ");scanf("%d",&Option);
    fflush(stdin);

    /* Determine what option was entered and call relevant function */
    switch(Option)
    {
    case 1 : AddRecord(Members);break;
    case 2 : DeleteRecord(Members);break;
    case 3 : ViewRecord(Members);break;
    case 4 : ListRecords(Members);break;
    case 5 : AmendRecord(Members);break;
    case 6 : break;
    default : printf("INVALID OPTION - Re-enter-->");
    }
    }while(Option != 6);
    }

  2. #2
    5|-|1+|-|34|) ober's Avatar
    Join Date
    Aug 2001
    Posts
    4,429
    code tags would help... still looking at the code... gimme a few minutes..

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Tell printf to print something.
    Code:
    printf("\t\tMember No: %s",Members[CurrentMember].Memb_No);
    /* etc. */
    Things to avoid:Good ideas:

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. File Writing Problem
    By polskash in forum C Programming
    Replies: 3
    Last Post: 02-13-2009, 09:47 AM
  3. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  4. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM

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