0
int main()
{

    FILE* infile1;

    int stockCode[15];
    char stockName[100];
    int stockQuantity[15];
    int stockReorder[15];
    int unitPrice[15];
    int i;

    infile1  = fopen("NUSTOCK.TXT", "r");

    while(fscanf(infile1, "%d %s %d %d %f",
         &stockCode, stockName, &stockQuantity, &stockReorder, &unitPrice) != EOF)
    {
        printf("  %3d  %-18s  %3d  %3d  %6.2f \n",
          stockCode, stockName, stockQuantity, stockReorder, unitPrice);
    }

    fclose(infile1);

}

What I'm trying to do is to take information from a file and store it into 5 separate arrays. However, when printing out, it only prints out the name correctly.

1394854864 Prune-Basket 1394854688 1394854624 0.00
1394854864 Pear-Basket 1394854688 1394854624 0.00
1394854864 Peach-Basket 1394854688 1394854624 0.00
1394854864 Deluxe-Tower 1394854688 1394854624 0.00

The original file looks like this. So all the numbers aren't being scanned in and I can't figure out why...

101 Prune-Basket 065 060 25.00
105 Pear-Basket 048 060 30.00
107 Peach-Basket 071 060 32.00
202 Deluxe-Tower 054 040 45.00

0

2 Answers 2

2

I think what you want to do is designing a structure for saving many personal records. And each record contain:

  • code
  • name
  • quantity
  • reorder
  • unitPrice

You should know the meaning of each type in C language.

I suggest you rewrite your codes like these:

#include <stdio.h>
#include <stdlib.h>
struct OneRecord{
    int code;
    char name[100];
    int quantity;
    int recorder;
    float unitPrice;
};

int main(){


    struct OneRecord* records = (struct OneRecord*)calloc(15, sizeof(struct OneRecord));
    int i = 0;

    FILE* infile1  = fopen("NUSTOCK.TXT", "r");
    int max=0;

    //%99s is used for max string length, because of we can protect the out of string's memory length
    while((max<15)&&(fscanf(infile1, "%d %99s %d %d %f",
                &records[i].code, records[i].name, &records[i].quantity, &records[i].recorder, &records[i].unitPrice) == 5))
    {
        i++;
        max++;
    }
    for(i=0;i<max;i++){
        printf("  %3d  %-18s  %3d  %3d  %6.2f \n",
                records[i].code,records[i].name,records[i].quantity,records[i].recorder,records[i].unitPrice);
    }

    fclose(infile1);
    free(records);
}

And how to use the C Structure in functions or many other places? In C programming language, there are different types such as int, char, struct and so on. You can use struct like many other types.

void printRecords(const struct OneRecord* records, int max)
{
    int i;
    for(i=0;i<max;i++){
        printf("  %3d  %-18s  %3d  %3d  %6.2f \n",
                records[i].code,records[i].name,records[i].quantity,records[i].recorder,records[i].unitPrice);
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Minor: Suggest ""%d %99s %d %d %f"" and while (fscanf(...) == 5)
Thank you! I didn't think of using a structure and after researching to understand a bit more I think I got it. My next question is, how would I use that structure in a function I write?
@user3357457 Please see the example. You can use struct like many other types.
1

You are using those arrays incorrectly. Try this:

i = 0;
while(fscanf(infile1, "%d %s %d %d %f",
            stockCode+i, stockName, stockQuantity+i, stockReorder+i, unitPrice+i) != EOF)
{
    printf("  %3d  %-18s  %3d  %3d  %6.2f \n",
            stockCode[i], stockName, stockQuantity[i], stockReorder[i], unitPrice[i]);
    i++;
}

Also, the unitPrice should be a array of float, not of int.

1 Comment

@chux Yes, you are right. I forgot to mention that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.