0

We were asked to store values from one file to another. (Reading it from a source txt file and copying it into another txt file) I got all the syntax correct but somehow, my program's got a logical error in printing in the second file (Infinite loop so that's why it won't proceed in the next while loop.) The product of this program is getting the last set of information entered only and the loop never ends.

Here's my program:

char ans;
FILE *fp,*fp2;
fp = fopen("INVENTORY.txt","w");
do{
    printf("Date (mm/dd/yy): ");
    scanf("%d %d %d", &inv.dt.m, &inv.dt.d, &inv.dt.yr);
    printf("Part No.: ");
    scanf("%d", &inv.pno);
    printf("Price: ");
    scanf("%f", &inv.price);
    printf("Quantity On Hand: ");
    scanf("%d", &inv.qty);
    printf("Reorder Parts: ");
    scanf("%d", &inv.rp);
    printf("Monthly Order: ");
    scanf("%d", &inv.mo);
    if(inv.qty < inv.rp)
        inv.oa = (inv.rp + inv.mo) - inv.qty;
    else
        inv.oa = 0;
    fprintf(fp,"%d/%d/%d\t%d\t%.2f\t%d\t%d\t%d\t%.2f\n", inv.dt.m, inv.dt.d, inv.dt.yr, inv.pno, inv.price, inv.qty, inv.rp, inv.mo, inv.oa);
    printf("Add another (Y/N)? ");
    scanf(" %c",&ans);
    printf("\n");
}while(ans == 'Y' || ans == 'y');
fclose(fp);
fp = fopen("INVENTORY.txt", "r");
fp2 = fopen("REPORT.txt", "w");

while(fscanf(fp,"%d/%d/%d\t%d\t%.2f\t%d\t%d\t%d\t%.2f\n", &inv.dt.m, &inv.dt.d, &inv.dt.yr, &inv.pno, &inv.price, &inv.qty, &inv.rp, &inv.mo, &inv.oa)!=EOF){
    fprintf(fp2,"%d\t%.2f\t%d\t%d\t%d\t%.2f\n", inv.pno, inv.price, inv.qty, inv.rp, inv.mo, inv.oa);
}

fclose(fp);
fclose(fp2);
fp2 = fopen("REPORT.txt","r");
printf("Part No.\tPrice\tQuantity On Hand\tReorder Parts\tMonthly Order\tOrder Amount");

while(fscanf(fp2,"%d\t%.2f\t%d\t%d\t%d\t%.2f\n", &inv.pno, &inv.price, &inv.qty, &inv.rp, &inv.mo, &inv.oa)!=EOF){
    printf("%d\t%.2f\t%d\t%d\t%d\t%.2f",inv.pno, inv.price, inv.qty, inv.rp, inv.mo, inv.oa);
}
fclose(fp2);
return 0;

I hope someone can help me with this, thank you.

peng
  • 21
  • 2
  • You never test whether any of your `scanf()` calls in the initial loop is successful. You should test every call to `scanf()` to ensure you get the result you expect. Your final loop uses `while (fscanf(fp, …) != EOF)` — this too is wrong, as you might get back a value such as 0 (or 1…5) indicating a failure, or 6 indicating success (as well as perhaps EOF). If you get a short read, it is probably because there is a non-numeric character in the file, and you will be stuck forever. – Jonathan Leffler Mar 16 '19 at 05:58
  • Also note that your prompt `printf("Date (mm/dd/yy): ");` is misleading because the input format `scanf("%d %d %d", &inv.dt.m, &inv.dt.d, &inv.dt.yr);` won't work with slashes in the data. You'd need to use `"%d/%d/%d"` to accept the slashes. If you do enter any slashes, your remaining input is not going to work until you read the single character into `ans`. That probably won't be a `'y'` or `'Y'`. – Jonathan Leffler Mar 16 '19 at 06:00
  • Your copying loop is reading and writing 9 values per line; your reporting loop is reading only 6 values per line. Someone, somewhere, is confused. – Jonathan Leffler Mar 16 '19 at 06:15

1 Answers1

0

Joven,

I think your problem lies with scanf(" %c",&ans); try removing the space infront of "%c"

The best thing for you to do would to be put some output at the end of your loops for debugging, you have a lot of loops, at the end of each loop do something like printf("\nwhile loop id: 1, seq: %d\n", loop_count) you also have some things going on with your fclose and fopens, Accesing files takes a small bit of time, it's quicker for you to open the file when you need it, and then close it when your done...If you need to read and write to a file at the same time use fopen(filename, "rw").

TheStart101
  • 76
  • 1
  • 10
  • No; that space is necessary to skip over the newline left behind by the previous `scanf()` operation. See [`scanf()` leaves the newline in the input buffer](https://stackoverflow.com/questions/5240789/scanf-leaves-the-new-line-char-in-buffer) for more information. – Jonathan Leffler Mar 16 '19 at 06:02
  • Alright, I'd suggest creating a counter and trying to find the loop that you are stuck in, that should help you either refine your question so you can get better help, or should give you you're solution. – TheStart101 Mar 16 '19 at 06:02