Thread: How to read struct variable 'username' from random access file and print to .txt?

  1. #1
    Registered User
    Join Date
    Dec 2017
    Posts
    1

    How to read struct variable 'username' from random access file and print to .txt?

    In the code listed below my questions are about:

    the newRecord() function which adds new users to the program but doesn't accurately pass the information to the createFile() function. OR the createFile() function which isn't accurately reading the .dat file created to obtain the username from the newRecord() function. My result is squiggly weird characters for the line of code:

    This first line of code below is where I 'suspect' something is very wrong but Ive been at it for a day and have changed a lot of things with no success.
    Code:
    if(client.username != '\0'){
        fprintf(writePtr,"%s",client.username);
    }/*end if*/
    The entire program is supposed to mimic a transaction processing program done in the C How To Program 6th edition (Chap. 11 File Processing). Basically I want to adapt the program to handle usernames followed by password validation etc however I am stuck at the first step i.e. storing username and printing successfully to a text file from a random access binary file.

    I attach all of the code used in the program here:



    Code:
    /*
    C How To Program 6th Edition Fig. 11.16: fig11_16.c
    This program creates and then reads a random access file sequentially,it creates new data to be placed in the file.
    
    
    */
    
    
    #include <stdio.h>
    #include <stdlib.h>
    
    
    /*clientData structure definition */
    struct clientData{
        char *username;/*username*/
        int lessonNum;
    };/*end structure clientData*/
    
    
    int enterChoice(void);
    void newRecord(FILE *fPtr);
    void textFile(FILE *readPtr);
    void createFile(FILE *fPtr);
    
    
    int main(int argc, char *argv[])
    {
        FILE *cfPtr; /*username.dat file pointer*/
        int choice;/*user's choice*/
        
        
        /*fopen opens file; creates one if file cannot be opened*/
        if((cfPtr = fopen("username.dat","rb+")) == NULL){
            printf("Creating file by that name now \n");
            
            /*function call to create file username.dat */
            createFile(cfPtr);
        
        }/*end if*/
        else{
            /*enable user to specify action*/
            while((choice = enterChoice()) !=5){
                switch(choice){
                    case 1:
                    textFile(cfPtr);
                    break;
                    case 3:
                    newRecord(cfPtr);
                    break;
                    /*display message if user does not select a valid choice */
                    default:
                    printf("Incorrect choice\n");
                    break;
                }/*end switch*/
    
    
    
    
            }/*end while*/
        }/*end else*/
    
    
        fclose(cfPtr);/*fclose closes the file*/
    
    
    return 0;
    }
    
    
    
    
    /*function creates file if none previously done*/
    void createFile(FILE *fPtr){
        int i;/*counter used to count from 1-100*/
        
        /*create client Data with default information*/
        struct clientData client={"",0};
    
    
    
    
        /*fopen opens the file; exits if file cannot be opened*/
        if((fPtr = fopen("username.dat","wb")) ==NULL){
            printf("File could not be opened.\n");
        }/*end if */
    
    
        else{
        
            for(i=1;i<=100;i++){
                /*Do I need to add struct clientData as function parameter?*/
                fwrite(&client,sizeof(struct clientData),1,fPtr);
            }/*end for*/
            fclose(fPtr);/*fclose closes the file*/
        }/*end else*/
        printf("File has been created.\n");
    
    
    }/*end function createFile*/
    
    
    
    
    /* create formatted text file for printing */
    void textFile(FILE *readPtr)
    {
        FILE *writePtr;/*accounts.txt file pointer*/
    
    
        /*create clientData with default information*/
        struct clientData client = {"",0};
            
    
    
            /*fopen opens the file exits if file cannot be opened*/
            if((writePtr = fopen("usernames.txt","w")) == NULL){
                printf("File could not be opened.\n");
            }/*end if*/
        
            else{
                rewind(readPtr);/*sets pointer to beginning of file*/
                fprintf(writePtr, "%s","Username\n");
        
            /*copy all records from random-access file into text file */
                    while (!feof(readPtr)){
                        fread(&client,sizeof(struct clientData),1,readPtr);
        
                        /*write single record to text file*/
                        if(client.username != '\0'){
                            fprintf(writePtr,"%s",client.username);
                        }/*end if*/
                    }/*end while*/
            fclose(writePtr);/*fclose closes the file*/
            }/*end else*/
        
    }/*end function textFile*/
    
    
    /*enable user to input menu choice*/
    int enterChoice(void)
    {
        int menuChoice;/*variable to store user's choice*/
        
        /*display available options*/
        printf("\nEnter your choice\n"
            "1-Store a formatted text file of usernames\n"
            " \"usernames.txt\"for printing\n"
            "3-add a new account\n"
            "5-end program\n?");
            scanf("%d",&menuChoice);/*receive choice from user*/
            return menuChoice;
    }/*end function enterChoice */
    
    
    
    
    /* create and insert new username*/
    void newRecord(FILE *fPtr)
    {
        /*create clientData with default information*/
        struct clientData  client = {"",0};
        
        char newUser[15];/*account username*/
        int n;/*int used as substitute for (accountNum -1) in original code */
    
    
        /*obtain username of account to create*/
        printf("Enter your desired acctNum (3 digits:\n");
        scanf("%s",newUser);
        
        n=sizeof(newUser);
    
    
        /*move file pointer to correct record in file*/
        fseek(fPtr,(n-1) * sizeof(struct clientData),SEEK_SET);
    
    
        /*read record from file*/
        fread(&client,sizeof(struct clientData),1,fPtr);
    
    
        /*display error if username already exists*/
    
    
        if(client.username != '\0'){
            printf("Account number %s already exists\n",client.username);
        }/*end if*/
        
        else{
            client.username = newUser;
            n=sizeof(client.username);
            fseek(fPtr,(n-1) * sizeof(struct clientData),SEEK_SET);
    
    
            /*instert record in file*/
            fwrite(&client,sizeof(struct clientData),1,fPtr);
    
    
            /*Testing for values returned*/
            printf("we got %s \n\n",client.username); /*correct*/    
        }/*end else*/
            
    }/*end function newRecord*/

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Code:
    struct clientData{
        char *username;/*username*/
        int lessonNum;
    };/*end structure clientData*/
    You can NOT save a pointer to a memory location and restore it and expect it to point to valid data!

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Specif Record Read error in random access files.
    By infantheartlyje in forum C++ Programming
    Replies: 0
    Last Post: 10-31-2011, 01:41 AM
  2. Read from file and match the password and username
    By plodos in forum C Programming
    Replies: 3
    Last Post: 01-05-2009, 01:10 PM
  3. Replies: 12
    Last Post: 04-01-2008, 08:58 AM
  4. random file access
    By bob20 in forum C++ Programming
    Replies: 6
    Last Post: 11-27-2002, 10:28 PM
  5. Read struct-like file/print to stdout (again)
    By inakappeh in forum C Programming
    Replies: 3
    Last Post: 09-18-2001, 04:33 AM

Tags for this Thread