-
Inventory records
been working on this assignment creating an inventory program that will create 100 empty records, let me input data concerning each tool, enable me to list all tools, delete records for discontinued tools and update any information in the file. I think I'm off to a decent start but for some reason cant get this to compile, can anyone spot my dumb mistake.....thanks.
Code:
#include <stdio.h>
/* hardwareData structure definition */
struct hardwareData {
int recordNum;
char toolname[20];
int quantity;
double cost;
}; /* end structure hardwareData */
int main()
{
FILE *hfPtr; /* hardware.dat 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{
/* require user to specift record number */
printf( "Enter record number"
"( 1 to 100, 0 to end input)\n" );
scanf( "%d", &hardware.recordNum );
/* user enters information, which is copied into file */
while ( hardware.recordNum != 0 ) {
/* user enters tool name, quantity and cost */
printf ( "Enter tool name, quantity, cost\n? " );
/* set record tool name, quantity and cost */
fscanf( stdin, "%s%d%f", hardware.toolname, hardware.quantity, &hardware.cost );
/* seek position in file to user-specified record */
fseek( hfPtr, ( hardware.recordNum - 1 ) *
sizeof( struct hardwareData ), SEEK_SET );
/* write user-specified information in file */
fwrite( &hardware, sizeof( struct hardwareData ), 1, hfPtr );
/* enable user to input another record number */
printf( " Enter record number\n?" );
scanf( "%d", &hardware.recordNum );
} /* end while */
fclose( hfPtr ); /* fclose closes file */
} /* end else */
return 0; /* indicates successful termination */
} /* end main */
-
nevermind I found the eror, does this satisfy the requirements as listed above?
-
No, it doesn't satisfy the requirements.
Seems like a silly question. Do you really need someone to answer that for you?
can you list all the tools?
can you delete a tool?
can you update a tool?
My guess is you need a menu of some sort, where you give choices to, Add, Update, Delete, or Show All tools.
-
cant I just re-enter that record as balnk and delete or update the tool?
-
I'm not sure I'd want to use the following code:
Code:
/* seek position in file to user-specified record */
fseek( hfPtr, ( hardware.recordNum - 1 ) *
sizeof( struct hardwareData ), SEEK_SET );
/* write user-specified information in file */
fwrite( &hardware, sizeof( struct hardwareData ), 1, hfPtr );
A scenario... User enters 1 for the record number of the first record BUT enters 98 for the record number of the second input record. Now you have unused space between records 1 and 98. Normally, this space is set to NULL. But if some of this space is NOT set to NULL, when you go to display all records, you may be displaying gibberish from this unused area. Also, this is a waste of storage space.
You may want to consider reading the records into a linked list or struct array, processsing the records and then writing the records back out to the file from the LL or struct array.
-
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 */
-
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.
-
I dont understand the problem with the undeclared identifier, are they not all declared in the prototypes?
-
Where is hfPtr declared in a prototype? Or more to the point, since the name of a parameter in the prototype doesn't matter, where is it declared in the function? (The compiler is asking you the same question.)
[edit]While we're at it:
FAQ > Explanations of... > Why it's bad to use feof() to control a loop
-
I fixed the hfPtr, it was the others that I thought were declared in the prototype. Its now giving me an error that says writePr is being used without having been initialized but I thought that this had initialized it.
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" );
p.s. feof is what is used in my book so thats why I used it.
-
Still having that error with the rest of the code completed, anyone have and ideas? It compiles as is but when I try to enter information it must have a continous loop or something?
Code:
/* hardwareware.dat */
#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 );
FILE *hfPtr; /* hardware.dat file pointer */
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.txt", "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 */
/* update record */
void updateRecord ( FILE *fPtr )
{
int record; /* record number */
/* create record data with no information */
struct hardwareData hardware = { 0, "", 0,0.0 };
/* obtain number of account to update */
printf( "Enter record to update (0-100): ");
scanf( "%d", &record );
/* move file pointer to correct record */
fseek( fPtr, ( record - 1 ) * sizeof( struct hardwareData ),
SEEK_SET );
/* read record from file */
fread( &record, sizeof( struct hardwareData ), 1, fPtr );
/* display error if record does not exist */
if ( hardware.recordNum == 0) {
printf( "Record #%d has no information.\n", record );
} /* end if */
else { /* update record */
printf( "Enter new tool name, quantity,cost \n?" );
scanf( "%s%d%f", hardware.toolname, hardware.quantity, hardware.cost );
fseek( fPtr, ( record - 1 ) * sizeof( struct hardwareData ),
SEEK_SET );
/* write updated record over old record */
fwrite( &hardware, sizeof( struct hardwareData ), 1, fPtr );
} /* end else */
} /* end function update record */
/* delete an existing record */
void deleteRecord( FILE *fPtr )
{
struct hardwareData hardware; /* stores record read form file */
struct hardwareData blankRecord = { 0, "", 0,0.0 }; /* blank record */
int recordNum;
/* obtain number of record to delete */
printf( "Enter record number to delete (0-100): " );
scanf( "%d", &recordNum );
/* move file pointer to correct file */
fseek( fPtr, ( recordNum - 1 ) * sizeof( struct hardwareData ),
SEEK_SET );
/* read record from file */
fread( &hardware, sizeof( struct hardwareData ), 1, fPtr );
/* display error if record does not exist */
if ( hardware.recordNum==0 ) {
printf ( "Record %d does not exist.\n", recordNum );
} /* end if */
else { /* delete record */
/* move file pointer to correct record */
fseek( fPtr, ( recordNum - 1 ) * sizeof( struct hardwareData ),
SEEK_SET );
/* replace existing record with blank record */
fwrite( &blankRecord,
sizeof( struct hardwareData ), 1, fPtr );
} /* end else */
} /* end function delete record */
/* create and insert record */
void newRecord( FILE *fPtr )
{
/* create recordData with default information */
struct hardwareData hardware = { 0, "", 0,0.0 };
int recordNum;
/* obtain record number to create */
printf( "Enter record number to create (1-100) : " );
scanf( "%d", &recordNum );
/* move file pointer to correct record */
fseek( fPtr, ( recordNum - 1 ) * sizeof( struct hardwareData ),
SEEK_SET );
/* read record */
fread( &hardware, sizeof( struct hardwareData ), 1, fPtr );
/* display error if record already exists */
if ( hardware.recordNum != 0 ) {
printf( "Record already exists" );
} /* end if */
else { /* create record */
/* user enters toolname, quantity and cost */
printf( "Enter tool name, quantity, cost\n" );
scanf( "%s%d%f", &hardware.toolname, &hardware.quantity, &hardware.cost );
/* move file pointer to correct record */
fseek( fPtr, ( recordNum - 1 ) * sizeof( struct hardwareData ),
SEEK_SET );
/* insert record into file */
fwrite( &hardware,
sizeof( struct hardwareData ), 1, fPtr );
} /* end else */
} /* end function newRecord */
/* enable user to input menu choice */
int enterChoice( void )
{
int menuChoice; /* variable to stoer users choice */
/* display available options */
printf( "\nEnter your choice\n"
"1 - store a formatted text file of records called\n"
" \"hardware.txt\"for printing\n"
"2 - update a record\n"
"3 - add a new record\n"
"4 - delete a record\n"
"5 - end program\n? " );
scanf( "%d", & menuChoice ); /* recieve choice from user */
return menuChoice;
} /* end function enterChoice */
-
It brings up the menu, lets me enter a choice, since there is currently nothing in the file, I choose 3
"enter new record number"
which works fine by bring up the line
"enter tool name, quantity, cost"
however when i enter the information and hit enter, the program continously reprints the menu over and over until I close it.
-
Your problem is likely that you are using scanf for input. When you enter a number the newline is left in the input buffer. There are better ways to get input, both for numbers and for strings. These are described in the FAQ, as well as clearing the input buffer of such things as a leftover newline.
-
Is it not possible to use scanf to get input for this? I'm just asking because thats how they have been doing it in my book. Would it not be easieer to fix this rather than redo all of the input functions?
-
Just zoomin' through that FAQ, huh?
FAQ > How do I... (Level 2) > Flush the input buffer
-
I read about that and I cant really understand how else to do it without doing it all over, as far as I can tell it says that scanf is not how a "senior programmer" would collect information from the user. I didn't know what a compiler was 9 weeks ago, this is my first and last programming class as I am an engineering major, I am doing things how they are dfone in the book as I am taking this class online so I have no other sorces, and its a struggle doing this as is without doing something that isn't in my text, I just want to find out why this program is not functioning properly, I know that it can work, I just need to find the problems and now I reworked the create file function a bit and now am down to one warning:
(98) : warning C4133: 'function' : incompatible types - from 'char [22]' to 'struct _iobuf *'
It will execute but crashes and gives a error window that says debug assertation failed? I have no idea what this means any help?, heres the updated code
Code:
/* hardwareware.dat */
#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 );
FILE *hfPtr; /* hardware.dat file pointer */
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 = NULL; /* 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.txt", "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 */
/* update record */
void updateRecord ( FILE *fPtr )
{
int record; /* record number */
/* create record data with no information */
struct hardwareData hardware = { 0, "", 0,0.0 };
/* obtain number of account to update */
printf( "Enter record to update (0-100): ");
scanf( "%d", &record );
/* move file pointer to correct record */
fseek( fPtr, ( record - 1 ) * sizeof( struct hardwareData ),
SEEK_SET );
/* read record from file */
fread( &record, sizeof( struct hardwareData ), 1, fPtr );
/* display error if record does not exist */
if ( hardware.recordNum == 0) {
printf( "Record #%d has no information.\n", record );
} /* end if */
else { /* update record */
printf( "Enter new tool name, quantity,cost \n?" );
scanf( "%s%d%f", hardware.toolname, hardware.quantity, hardware.cost );
fseek( fPtr, ( record - 1 ) * sizeof( struct hardwareData ),
SEEK_SET );
/* write updated record over old record */
fwrite( &hardware, sizeof( struct hardwareData ), 1, fPtr );
} /* end else */
} /* end function update record */
/* delete an existing record */
void deleteRecord( FILE *fPtr )
{
struct hardwareData hardware; /* stores record read form file */
struct hardwareData blankRecord = { 0, "", 0,0.0 }; /* blank record */
int recordNum;
/* obtain number of record to delete */
printf( "Enter record number to delete (0-100): " );
scanf( "%d", &recordNum );
/* move file pointer to correct file */
fseek( fPtr, ( recordNum - 1 ) * sizeof( struct hardwareData ),
SEEK_SET );
/* read record from file */
fread( &hardware, sizeof( struct hardwareData ), 1, fPtr );
/* display error if record does not exist */
if ( hardware.recordNum==0 ) {
printf ( "Record %d does not exist.\n", recordNum );
} /* end if */
else { /* delete record */
/* move file pointer to correct record */
fseek( fPtr, ( recordNum - 1 ) * sizeof( struct hardwareData ),
SEEK_SET );
/* replace existing record with blank record */
fwrite( &blankRecord,
sizeof( struct hardwareData ), 1, fPtr );
} /* end else */
} /* end function delete record */
/* create and insert record */
void newRecord( FILE *fPtr )
{
/* create recordData with default information */
struct hardwareData hardware = { 0, "", 0,0.0 };
int piece;
/* obtain record number to create */
printf( "Enter record number to create (1-100) : " );
scanf( "%d", &piece);
/* move file pointer to correct record */
fseek( fPtr, piece * sizeof( struct hardwareData ),
SEEK_SET );
/* read record */
fread( &hardware, sizeof( struct hardwareData ), 1, fPtr );
/* display error if record already exists */
if ( !hardware.recordNum ) { /* create record */
hardware.recordNum = piece;
printf( "Enter tool name, quantity, cost\n?" );
scanf(" %[^0-9] %d%f", hardware.toolname, &hardware.quantity,
&hardware.cost);
/* move file pointer to correct record */
fseek(hfPtr, hardware.recordNum * sizeof( struct hardwareData), SEEK_SET);
/* insert record into file */
fwrite( &hardware, sizeof( struct hardwareData), 1, hfPtr);
} /* end if */
else {
/* user enters toolname, quantity and cost */
printf( "Record already exists.\n" );
} /* end else */
} /* end function newRecord */
/* enable user to input menu choice */
int enterChoice( void )
{
int menuChoice; /* variable to stoer users choice */
/* display available options */
printf( "\nEnter your choice\n"
"1 - store a formatted text file of records called\n"
" \"hardware.txt\"for printing\n"
"2 - update a record\n"
"3 - add a new record\n"
"4 - delete a record\n"
"5 - end program\n? " );
scanf( "%d", & menuChoice ); /* recieve choice from user */
return menuChoice;
} /* end function enterChoice */
-
Remove this global variable, and you'll see you are often using the wrong file pointer. That's a big part of your errors. You should not need this global at all (above main)
Code:
FILE *hfPtr; /* hardware.dat file pointer */
in textFile you're missing writePtr as the first paramter to your second fprintf
in updateRecord you pass &record to fread, you should be passing &hardware
another large bug is you are inconsitent when subtracting 1 from record when doing your fseek calls. either always do it or never do it.
make your scanf's consistent when reading data (add and update). You'll notice your are missing some &'s in your update routine. for simplicity why not just
Code:
scanf( "%s %d %f", hardware.toolname, &hardware.quantity, &hardware.cost );
Ideally you'd handle this better but it's a start
I don't understand what you are trying to do with textFile.
do you realize it's a different filename .txt vs .dat?
Edit. Ah i see... just creating a human readable version of the data
Actually seems like you're pretty close.
How about an option to show all the tools. Just write a loop that checks all 100 possible spots and if recordnum is not zero print the contents
-
Thanks for the tips, Ive made some changes, however the underlying problem still remains. It will compile with one warning which I can't seem to figure out:
(101) : warning C4133: 'function' : incompatible types - from 'char [10]' to 'struct _iobuf *'
It brings up the menu, lets me choose 3 to add new tool, but when I input the information and pres enter, It prints the menu and says incorrect choice, over and over and over until I manually close the program, any Ideas about the warning or the crash? Thanks for all of your help.
I thought that I had figured out that warning by adding record.Num to that fprintf command but then recieved the following errors:
(101) : warning C4133: 'function' : incompatible types - from 'char [9]' to 'struct _iobuf *'
(102) : warning C4047: 'function' : 'const char *' differs in levels of indirection from 'int '
(102) : warning C4024: 'fprintf' : different types for formal and actual parameter 2
Code:
/* 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( "%d%s%d%f",
hardware.recordNum, hardware.toolname,
hardware.quantity, hardware.cost );
} /* end if */
-
Code:
fprintf( writePtr, "%d%s%d%f",
hardware.recordNum, hardware.toolname,
hardware.quantity, hardware.cost );
-
Thanks, it now compiles with no errors or warnings but still will not allow me to enter information?
-
Sorry, I'm not following too closely (sticking to your book that has left you confused an unable to write this for yourself rather than implementing the suggestions here that make up for the shortcomings in your book and all); I don't suppose you could post your latest code?
-
Code:
/* hardwareware.dat */
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
/* hardwareData structure definition */
struct hardwareData {
int recordNum;
char toolname[30];
int quantity;
float 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 = NULL; /* 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 ((writePtr = fopen( "hardware.txt", "r+" )) == 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( writePtr, "%d%s%d%f",
hardware.recordNum, hardware.toolname,
hardware.quantity, hardware.cost );
} /* end if */
} /* end while */
fclose( writePtr ); /* fclose closes file */
} /* end else */
} /* end function textrFile */
/* update record */
void updateRecord ( FILE *fPtr )
{
int record; /* record number */
/* create record data with no information */
struct hardwareData hardware = { 0, "", 0,0.0 };
/* obtain number of account to update */
printf( "Enter record to update (0-100): ");
scanf( "%d", &record );
/* move file pointer to correct record */
fseek( fPtr, ( record - 1 ) * sizeof( struct hardwareData ),
SEEK_SET );
/* read record from file */
fread( &hardware, sizeof( struct hardwareData ), 1, fPtr );
/* display error if record does not exist */
if ( hardware.recordNum == 0) {
printf( "Record #%d has no information.\n", record );
} /* end if */
else { /* update record */
printf( "Enter new tool name, quantity,cost \n?" );
scanf( "%s %d %f", hardware.toolname, &hardware.quantity, &hardware.cost );
fseek( fPtr, ( record - 1 ) * sizeof( struct hardwareData ),
SEEK_SET );
/* write updated record over old record */
fwrite( &hardware, sizeof( struct hardwareData ), 1, fPtr );
} /* end else */
} /* end function update record */
/* delete an existing record */
void deleteRecord( FILE *fPtr )
{
struct hardwareData hardware; /* stores record read form file */
struct hardwareData blankRecord = { 0, "", 0,0.0 }; /* blank record */
int recordNum;
/* obtain number of record to delete */
printf( "Enter record number to delete (0-100): " );
scanf( "%d", &recordNum );
/* move file pointer to correct file */
fseek( fPtr, ( recordNum - 1 ) * sizeof( struct hardwareData ),
SEEK_SET );
/* read record from file */
fread( &hardware, sizeof( struct hardwareData ), 1, fPtr );
/* display error if record does not exist */
if ( hardware.recordNum==0 ) {
printf ( "Record %d does not exist.\n", recordNum );
} /* end if */
else { /* delete record */
/* move file pointer to correct record */
fseek( fPtr, ( recordNum - 1 ) * sizeof( struct hardwareData ),
SEEK_SET );
/* replace existing record with blank record */
fwrite( &blankRecord,
sizeof( struct hardwareData ), 1, fPtr );
} /* end else */
} /* end function delete record */
/* create and insert record */
void newRecord( FILE *fPtr )
{
/* create recordData with default information */
struct hardwareData hardware = { 0, "", 0,0.0 };
int piece;
/* obtain record number to create */
printf( "Enter record number to create (1-100) : " );
scanf( "%d", &piece);
/* move file pointer to correct record */
fseek( fPtr, piece * sizeof( struct hardwareData ),
SEEK_SET );
/* read record */
fread( &hardware, sizeof( struct hardwareData ), 1, fPtr );
/* display error if record already exists */
if ( !hardware.recordNum ) { /* create record */
hardware.recordNum = piece;
printf( "Enter tool name, quantity, cost\n?" );
scanf( "%s %d %f", hardware.toolname, &hardware.quantity, &hardware.cost );
/* move file pointer to correct record */
fseek(fPtr, hardware.recordNum * sizeof( struct hardwareData), SEEK_SET);
/* insert record into file */
fwrite( &hardware, sizeof( struct hardwareData), 1, fPtr);
} /* end if */
else {
/* user enters toolname, quantity and cost */
printf( "Record already exists.\n" );
} /* end else */
} /* end function newRecord */
/* enable user to input menu choice */
int enterChoice( void )
{
int menuChoice; /* variable to stoer users choice */
/* display available options */
printf( "\nEnter your choice\n"
"1 - store a formatted text file of records called\n"
" \"hardware.txt\"for printing\n"
"2 - update a record\n"
"3 - add a new record\n"
"4 - delete a record\n"
"5 - end program\n? " );
scanf( "%d", & menuChoice ); /* recieve choice from user */
return menuChoice;
} /* end function enterChoice */
-
What makes you think records aren't being added. did you look at the .dat file after you tried adding some?
in both update and delete you do
Code:
( record - 1 ) * sizeof( struct hardwareData )
but in new you do
Code:
hardware.recordNum * sizeof( struct hardwareData) // missing -1 ?
so if you add at record 1 it will not appear to be there with update or delete because you subtract 1 (so it will look at location zero)... You need to be consistent
(IMO all 6 of your fseek calls should look exactly the same. Why have a different variable name for your offset in each function? You call it record, recordNum and piece.)
you shouldn't use feof to control your loop in textFile. I believe your last line will be printed twice if you do it that way. In the board FAQ there is an entry explaining why and alternatives
-
so..is there any complete code or new successful code for this problem?..