I'll put it in simpler terms:
remove the line
in your main() function.Code:primary=fopen("parts.txt","ab");
QuantumPete
Printable View
No, you should use "sizeof(inventory)" only, as you want to read ONE inventory post, not "verify-1" number of inventory entries - and in particular, when verify == 1, then verify-1 becomes zero, so you will read zero bytes (so not fill in the slot variable at all - it will have whatever it had before the call to read in it). There certainly are elements from 0 onwards (assuming the file is not empty, of course) - but you need to tell fread how much to read. Where to read is what use fseek for.
--
Mats
awesome the program seems to be operating well, ill add the next function and hopefully ill be able to do it without problems.
thanks
could you please explain exactly how? because if i remove primary what argument could i put when i call the function?Quote:
As well as remove primary var at all, and in the fill and edit functions change the parameter to local var
If you are opening the file inside the function, then you don't need to pass a FILE * variable.
My personal suggestion would be that you open and close the file only once, and pass the already opened file to your functions - but then you should NOT open the file again inside the functions. Opening and closing files do take time, and it serves no purpose to do so. If you want to make sure that the data is in the file, you can use fflush() to make sure the data has been actually written to the file.
--
Mats
well heres the final propgram and i have another problem (when do they end??)
so it builds successfully but as soon as i chose option 3 it gives me an assert eror and tells me to abort, any ideas why?Code:#include <stdio.h>
typedef struct inventory{
char partname[40];
int partnumber;
float price;
int stock;
}Inventory;
void fill(FILE *fill_program);
void get_info(Inventory *slot);
void edit(FILE*edit_program);
void edit_info(Inventory *slot);
void sort_print(FILE *sort_program);
int main (void)
{
int option=0;
FILE *primary;
primary=fopen("parts.txt","ab");
while(option!=5)
{
printf("PLEASE select from the following options\n");
printf("1. Enter new part\n");
printf("2. Edit existing part\n");
printf("3. Sort part list by number and write output file\n");
printf("5 Quit\n\n");
scanf("%d",&option);
switch(option)
{
case 1:
fill(primary);
break;
case 2:
edit(primary);
break;
case 3:
sort_print(primary);
break;
}/*close switch*/
}/*close while*/
fclose(main);
return 0;
}/*close main*/
void sort_print(FILE *sort_program)
{
Inventory hold = {"",0,0,0};
Inventory stack[30] ={"",0,0,0};
Inventory slot = {"",0,0,0};
int part1;
int par2;
int counter;
int pass;
FILE *output;
sort_program=fopen("parts.tx","rb");
for(counter=1;counter<=30;counter++)
{
fseek(sort_program,(counter-1)*sizeof(Inventory),SEEK_SET);
fread(&slot,sizeof(Inventory),1,sort_program);
stack[counter]=slot;
}/*close for*/
fclose(sort_program);
for(pass=1;pass<30;pass++)
{
for(counter=0;counter<=28;counter++)
{
if(stack[counter].partnumber > stack[counter+1].partnumber)
{
hold=stack[counter];
stack[counter]=stack[counter+1];
stack[counter+1]=hold;
}/*end if*/
}/*end for*/
}/*end for*/
output=fopen("Output.txt","w");
fprintf("%40s%50s%60s%70s\n", "Part name", "Part.NO", "Price", "stock");
for(counter=1;counter<=30;counter++)
{
fprintf("%40s%50d%60.2f%70d\n", stack[counter].partname,stack[counter].partnumber,stack[counter].price,stack[counter].stock);
}/*close for*/
fclose(output);
}/*close function*/
void edit(FILE*edit_program)
{
Inventory slot = {"",0,0,0};
int partnumber;
int verify=1;
int controller=1;
edit_program=fopen("parts.txt","rb+");
printf("Please enter part number\n");
scanf("%d", &partnumber);
while(controller==1)
{
fseek(edit_program,(verify-1)*sizeof(Inventory),SEEK_SET);
fread(&slot,sizeof(Inventory),1,edit_program);
if(partnumber==slot.partnumber)
{
printf("Current details are:\n");
printf("Part name : %s\n",slot.partname);
printf("Part price : %f\n",slot.price);
printf("Stock : %d\n\n",slot.stock);
printf("************************************************\n");
edit_info(&slot);
fseek(edit_program,(verify-1)*sizeof(Inventory),SEEK_SET);
fwrite(&slot,sizeof(Inventory),1,edit_program);
controller=0;
}/*close if*/
if(verify<=30)
{
verify++;
}/*close if*/
if(verify>=30)
{
printf("Part number not found, re enter number\n");
scanf("%d",&partnumber);
verify=1;
}/*close if*/
}/*close while*/
fclose(edit_program);
printf("\n");
}/*close function*/
void fill(FILE *fill_program)
{
Inventory slot = {"",0,0,0};
fill_program=fopen("parts.txt","ab");
fread(&slot,sizeof(Inventory),1,fill_program);
get_info(&slot);
fwrite(&slot,sizeof(Inventory),1,fill_program);
fclose(fill_program);
}/*close function*/
void get_info(Inventory *slot)
{
gets_s(slot->partname,40);
printf("Please enter partname\n");
gets_s(slot->partname,40);
printf("Please enter partnumber\n");
scanf("%d", &slot->partnumber);
printf("Please enter price\n");
scanf("%f", &slot->price);
printf("Please enter stock\n");
scanf("%d", &slot->stock);
printf("\n");
}/*close*/
void edit_info(Inventory *slot)
{
gets_s(slot->partname,40);
printf("Please enter partname\n");
gets_s(slot->partname,40);
printf("Please enter price\n");
scanf("%f", &slot->price);
printf("Please enter stock\n");
scanf("%d", &slot->stock);
printf("\n");
}/*close */
thanks
I don't know what the exact problem is, but this one is suspect:
This will write to stack[30], when the index should be 0..29. Remember that arrays are declared with the NUMBER OF ELEMENTS, but the elements range from zero to one less than that number.Code:Inventory stack[30] ={"",0,0,0};
...
for(counter=1;counter<=30;counter++)
...
stack[counter]=slot;
In the same loop:
You don't actually need to use fseek every time in the loop - once before the loop would be sufficient, since you are reading the blocks in succession.Code:fseek(sort_program,(counter-1)*sizeof(Inventory),SEEK_SET);
fread(&slot,sizeof(Inventory),1,sort_program);
The other thought is that you are repeating that particular sequence of fseek() and fread() many times - perhaps creating a function to read (and write?) the records would be a better choice.
Stylistically:
Mixing < and <= in loops that essentially deal with the same count is a bad idea.Code:for(pass=1;pass<30;pass++)
{
for(counter=0;counter<=28;counter++)
Also, the 30 that appears several places in your code should really be a constant defined at the beginning of the program, so that if you later on need to change this to 10000, you only need to change it once [although, to be very pedantic, that would potentially lead to other problems because large arrays on the stack isn't a good idea - but I think you won't need to worry about that right now].
And you are still passing a FILE * to each function, and then opening that file inside the function. You need to CHOOSE between:
A) opening the file and closing the file in ONE function(e.g. main), and passing the FILE * around
B) opening and closing the file in each function, and NOT passing a FILE * between functions.
If you are opening the file multiple times, the filename should be a constant value, not a literal string, so that if you decide to change the name of the file, you only have to change it in one place.
--
Mats
made the changes,
still with the problem though
new program
this is the errorCode:#include <stdio.h>
#define SIZE 30
typedef struct inventory{
char partname[40];
int partnumber;
float price;
int stock;
}Inventory;
void fill();
void get_info(Inventory *slot);
void edit();
void edit_info(Inventory *slot);
void sort_print();
void find(FILE *position, int i);
int main (void)
{
int option=0;
FILE *primary;
primary=fopen("parts.txt","ab");
while(option!=5)
{
printf("PLEASE select from the following options\n");
printf("1. Enter new part\n");
printf("2. Edit existing part\n");
printf("3. Sort part list by number and write output file\n");
printf("5 Quit\n\n");
scanf("%d",&option);
switch(option)
{
case 1:
fill();
break;
case 2:
edit();
break;
case 3:
sort_print();
break;
}/*close switch*/
}/*close while*/
fclose(main);
return 0;
}/*close main*/
void sort_print()
{
Inventory hold = {"",0,0,0};
Inventory stack[SIZE] ={"",0,0,0};
Inventory slot = {"",0,0,0};
int part1;
int par2;
int counter;
int pass;
FILE *sort_program;
FILE *output;
sort_program=fopen("parts.tx","rb");
find(sort_program,counter);
for(counter=1;counter<=SIZE;counter++)
{
fread(&slot,sizeof(Inventory),1,sort_program);
stack[counter]=slot;
}/*close for*/
fclose(sort_program);
for(pass=1;pass < SIZE ;pass++)
{
for(counter=0;counter < SIZE-1;counter++)
{
if(stack[counter].partnumber > stack[counter+1].partnumber)
{
hold=stack[counter];
stack[counter]=stack[counter+1];
stack[counter+1]=hold;
}/*end if*/
}/*end for*/
}/*end for*/
output=fopen("Output.txt","w");
fprintf("%40s%50s%60s%70s\n", "Part name", "Part.NO", "Price", "stock");
for(counter=0;counter < SIZE;counter++)
{
fprintf("%40s%50d%60.2f%70d\n", stack[counter].partname,stack[counter].partnumber,stack[counter].price,stack[counter].stock);
}/*close for*/
fclose(output);
}/*close function*/
void edit()
{
Inventory slot = {"",0,0,0};
int partnumber;
int verify=1;
int controller=1;
FILE*edit_program;
edit_program=fopen("parts.txt","rb+");
printf("Please enter part number\n");
scanf("%d", &partnumber);
while(controller==1)
{
find(edit_program,verify);
fread(&slot,sizeof(Inventory),1,edit_program);
if(partnumber==slot.partnumber)
{
printf("Current details are:\n");
printf("Part name : %s\n",slot.partname);
printf("Part price : %f\n",slot.price);
printf("Stock : %d\n\n",slot.stock);
printf("************************************************\n");
edit_info(&slot);
find(edit_program,verify);
fwrite(&slot,sizeof(Inventory),1,edit_program);
controller=0;
}/*close if*/
if(verify<=30)
{
verify++;
}/*close if*/
if(verify>=30)
{
printf("Part number not found, re enter number\n");
scanf("%d",&partnumber);
verify=1;
}/*close if*/
}/*close while*/
fclose(edit_program);
printf("\n");
}/*close function*/
void fill()
{
Inventory slot = {"",0,0,0};
FILE *fill_program;
fill_program=fopen("parts.txt","ab");
fread(&slot,sizeof(Inventory),1,fill_program);
get_info(&slot);
fwrite(&slot,sizeof(Inventory),1,fill_program);
fclose(fill_program);
}/*close function*/
void get_info(Inventory *slot)
{
gets_s(slot->partname,40);
printf("Please enter partname\n");
gets_s(slot->partname,40);
printf("Please enter partnumber\n");
scanf("%d", &slot->partnumber);
printf("Please enter price\n");
scanf("%f", &slot->price);
printf("Please enter stock\n");
scanf("%d", &slot->stock);
printf("\n");
}/*close*/
void edit_info(Inventory *slot)
{
gets_s(slot->partname,40);
printf("Please enter partname\n");
gets_s(slot->partname,40);
printf("Please enter price\n");
scanf("%f", &slot->price);
printf("Please enter stock\n");
scanf("%d", &slot->stock);
printf("\n");
}/*close */
void find(FILE *position,int i)
{
fseek(position,(i-1)*sizeof(Inventory),SEEK_SET);
}
http://i22.photobucket.com/albums/b3...Untitled-1.jpg
cheers
You still need to remove the following lines in your main function:
QuantumPeteCode:FILE *primary;
primary=fopen("parts.txt","ab");
and
fclose(main);
I think the error message is pretty clear - you are using counter without initializing it.
And I'm VERY surprised this compiles:
It should at the very least give you a warning for incompatible pointer types. Warnings may not stop you from getting an executable file, but warnings are there to tell you that the code isn't quite right - you should understand FULLY why you are getting the warnings if you choose to ignore them.Code:fclose(main);
[Yes, I know, you don't even get far enough to hit the problem, because your code exits due to the uninitialized variable before it gets to the fclose I pointed out, but it's still wrong, and once you have fixed the uninitialized variable, it will ].
I don't know where you get your ideas for variable names, but I would suggest that "position" should be used for the place you want to seek to, and "datafile" or some such for the FILE * variable.Code:void find(FILE *position,int i)
{
fseek(position,(i-1)*sizeof(Inventory),SEEK_SET);
}
--
Mats
ok i made the changes, however it just changed the error to
http://i22.photobucket.com/albums/b3...Untitled-2.jpg
Come on, can you seriously not understand what the error says?