Thread: Inventory records

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    63
    I am now moving in a different direction with this, Ive created a menu, and finished the text file but have come up with an error and a few warnings saying:

    error C2065: 'hfPtr' : undeclared identifier
    warning C4047 '=' : 'int ' differs in levels of indirection from 'struct _iobuf *'
    warning C4047: '==' : 'int ' differs in levels of indirection from 'void *'
    warning C4133: 'function' : incompatible types - from 'char [29]' to 'struct _iobuf *'
    warning C4133: 'function' : incompatible types - from 'char [22]' to 'struct _iobuf *'

    does anyone see what I've done wrong and anycoments on this code would be appreciated, thanks
    Code:
    #include <stdio.h>
    
    /* hardwareData structure definition */
    struct hardwareData {
    	int recordNum;
    	char toolname[20];
        int quantity;
    	double cost;
    
    }; /* end structure hardwareData */
    
    /* prototypes */
    int enterChoice( void );
    void textFile( FILE *readPtr );
    void updateRecord( FILE *fPtr );
    void newRecord( FILE *fPtr );
    void deleteRecord( FILE *fPtr );
    
    int main()
    {
       FILE *hfPtr; /* hardware.dat file pointer */
       int choice; /* user choice */
    
        /* fopen opens file; exits if file cannot be opened */
    	if ((hfPtr = fopen( "hardware.dat", "rb+" )) == NULL ) {
    		printf( "File could not be opened.\n" );
    	} /* end if */
    	else {
    
    		/* enable user to specify action */
    		while (( choice = enterChoice()) != 5 ) {
    
    			switch ( choice ) {
    				/* create text file from record */
    			case 1: 
    				textFile( hfPtr );
    				break;
    
    				/* update record */
    			case 2: 
    				updateRecord( hfPtr );
    				break;
    
    				/* create record */
    			case 3:
    				newRecord ( hfPtr );
    				break;
    
    				/* delete existing record */
    			case 4:
    				deleteRecord ( hfPtr );
    				break;
    
    				/* display message if user does not enter valid choice */
    			default:
    				printf( "Incorrect choice\n" );
    				break;
    
    			} /* end switch */
    
    		} /* end while */
    
    		fclose( hfPtr ); /* fclose closes the file */
    
    	} /* end else */
    
    	return 0; /* indicate successful termination */
    
    } /* end main */
    
    /* create formated text for printing */
    void textFile( FILE *readPtr )
    {
    	FILE *writePtr; /* hardware.txt file pointer */
    
    	/* create hardwareData with default information */
        struct hardwareData hardware = { 0, "", 0,0.0 };
    
        /* fopen opens file; exits if file cannot be opened */
    	if ((hfPtr = fopen( "hardware.dat", "rb+" )) == NULL ) {
    		printf( "File could not be opened.\n" );
    	} /* end if */
    	else{ 
    		rewind( readPtr ); /* sets pointer to begining of file */
    		fprintf( "writePtr, %-6s%-10s%-8s%-8s\n",
    			"Record#", "Tool name", "Quantity", "Cost" );
    
    		/* copy all records from random-access file to text file */
    		while ( !feof( readPtr ) ) {
    			fread( &hardware, sizeof( struct hardwareData ),1, readPtr );
    
    			/* write single record to text file */
    			if ( hardware.recordNum != 0 ) {
    				fprintf( "%-6d%-16s%-11d%10.2f\n",
                    hardware.toolname, hardware.quantity, hardware.cost );
    			} /* end if */
    
    		} /* end while */
    
    
        fclose( writePtr ); /* fclose closes file */
    	} /* end else */
    
    } /* end function textrFile */

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Well, undeclared identifier is pretty easy:
    Code:
    /* create formated text for printing */
    void textFile( FILE *readPtr )
    {
    	FILE *writePtr; /* hardware.txt file pointer */
    
    	/* create hardwareData with default information */
        struct hardwareData hardware = { 0, "", 0,0.0 };
    
        /* fopen opens file; exits if file cannot be opened */
    	if ((hfPtr = fopen( "hardware.dat", "rb+" )) == NULL ) {
    		printf( "File could not be opened.\n" );
    	} /* end if */
    	else{
    		rewind( readPtr ); /* sets pointer to begining of file */
    		fprintf( "writePtr, %-6s%-10s%-8s%-8s\n",
    			"Record#", "Tool name", "Quantity", "Cost" );
    
    		/* copy all records from random-access file to text file */
    		while ( !feof( readPtr ) ) {
    			fread( &hardware, sizeof( struct hardwareData ),1, readPtr );
    
    			/* write single record to text file */
    			if ( hardware.recordNum != 0 ) {
    				fprintf( "%-6d%-16s%-11d%10.2f\n",
                    hardware.toolname, hardware.quantity, hardware.cost );
    			} /* end if */
    
    		} /* end while */
    
    
        fclose( writePtr ); /* fclose closes file */
    	} /* end else */
    
    } /* end function textrFile */
    The next one is telling you that the first parameter to fprintf is a FILE* not a string.
    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.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 10:43 AM
  2. reading a file into a block
    By mickey0 in forum C++ Programming
    Replies: 19
    Last Post: 05-03-2008, 05:53 AM
  3. Replies: 26
    Last Post: 06-15-2005, 02:38 PM
  4. Counting number from a random file
    By kamisama in forum C Programming
    Replies: 42
    Last Post: 02-22-2005, 05:16 PM
  5. display records held in a struct
    By colinuk in forum C Programming
    Replies: 3
    Last Post: 02-02-2005, 07:51 AM