0

I have no problem opening and reading a binary file when i am not passing it to a function. But, in this case i am passing it to a function and keep running into a problem.

void fun1 (int amount,struct inventory a[],FILE *fp);

int main()
{
    tag a[10];
    int amount;
    int i;
    FILE *fp;

    fp=fopen("e:\\invent.txt","wb");

    printf("How many items do you want to enter? ");
    scanf("%d",&amount);
    for(i=1;i<=amount;i++)
    {
        printf("Enter the name of the item: ");
        scanf("%s",a[i].name);
        printf("Enter the unit amount the item has: ");
        scanf("%d",&a[i].num);
        printf("Enter the unit price for the item: ");
        scanf("%f",&a[i].price);
        fwrite(&a[i],sizeof(a[i]),amount,fp);
    }
    fclose(fp);
    fun1(amount,a,fp);           
}

^^This is my input plus my function call^^.

My Input:

How many items do you want to enter? 2
Enter the name of the item:Hammer
Enter the unit amount the item has: 32
Enter the unit price for the item: 11
Enter the name of the item: Nails
Enter the unit amount the item has:43
Enter the unit price for the item: 12

void fun1 (int amount,struct inventory a[],FILE *fp)
{
    int i;
    fp=fopen("e:\\invent.txt","rb");
    while(fread(&a[amount],sizeof(tag),amount,fp) == amount)
    {
        printf("\nItem\tUnit #\tPrice\n");
        for(i=1;i<=amount;i++)
        {
            printf("\n%s\t%d\t%.2f",a[i].name,a[i].num,a[i].price);
        }
    }
    fclose(fp);
    getchar();

}

^^My Function^^

My Output:

Item Unit # Price

Hammer 32 11.00

Hammer 32 11.00

Item Unit # Price

Hammer 32 11.00

Nails 43 12.00

It should not be printing "Hammer" twice. Only the bold one should be printed. If you can give me a link or if you have advice it would be so greatly appreciated!

Shamrocck
  • 31
  • 2
  • Just out of curiosity, why do you pass `fp` to the function? It's closed in `main` and reopened in `fun1`, so it could as well be a local variable in `fun1`. – Some programmer dude Dec 02 '11 at 06:55
  • Another note, in `main` you define `a` as `tag a[10]`, but `fun1` wants a `struct inventory` array. I'm guessing you have `typedef struct inventory tag` somewhere. If it's only slightly confusing today, think about how it will be in a few months. For your own sake you should be consistent with the types of variables and parameters. – Some programmer dude Dec 02 '11 at 06:59
  • i'm passing fp to the function because the project i'm doing requires me to have a binary file and i have to display the output in a function. I'm new at the whole FILE I/O thing so i'm sure if i'm doing this right. – Shamrocck Dec 02 '11 at 07:07
  • The file-pointer doesn't have to be passed, just declare it again in the function. It will be ok since you open the file again inside the function. – Some programmer dude Dec 02 '11 at 07:11

2 Answers2

1

In your input you are 1-indexing the array. Arrays are zero-indexed, so the last index you're accessing is out of bounds and there's no telling what you'll get.

Change that loop to be

for (i = 0; i < amount; i++) 
Kevin
  • 53,822
  • 15
  • 101
  • 132
0

problem is in your fun1 method i thing you are trying to collect whole list but you are giving size of structure so it can read up to first structure

try this:

void fun1 (FILE *fp)
{

    _tag a;
    fp=fopen("e:\\invent.txt","rb");
    while(fread(&a,sizeof(_tag),1,fp) == 1)
    {
        printf("\nItem\tUnit #\tPrice\n");


        printf("\n%s\t%d\t%d",a.name,a.num,a.price);


    }
    fclose(fp);

}
Ashish
  • 1,527
  • 4
  • 17
  • 33