You have a number of problems:
- You cannot use any input function correctly unless you check the return;
- See: Why is while ( !feof (file) ) always wrong?;
- Including the
&
before istenen
in scanf("%s", istenen
is wrong
Why?
When taking any input, regardless whether it is from the user or a file, you must check the return to determine whether the input succeeded or failed before proceeding to use those values filled by the input in the rest of your code. Otherwise, you invite Undefined Behavior by blindly using the variables that will be left indeterminate if your input fails. At minimum, you need, e.g.
fputs ("Enter the Product Barcode Number to be Called: ", stdout);
if (scanf("%s", istenen) != 1) { // NO & - istened already a pointer
fputs ("error: user canceled input.\n", stderr);
exit (EXIT_FAILURE);
}
fputs ("How many: ", stdout); /* just use fputs() unless a conversion needed */
if (scanf("%d", &birim) != 1) {
fputs ("error: invalid integer input.\n", stderr);
exit (EXIT_FAILURE);
}
Which leads to why while (!feof(fp))
is always wrong. As detailed in the answer, the problem is after your last good read of values from your file, EOF
is NOT set, you check while (!feof(fp))
and it tests true and you loop again and you attempt fscanf(fp,"%s %s %s %f\n",barkod,urunadi,kategori,&birim_fiyat);
which fails returning EOF
, but since you do not check for EOF
before printf("Aranan Ürün Bulundu: %s %s\n", urunadi,kategori);
you invoke Undefined Behavior using urunadi
and kategori
while there values are indeterminate.
You fix that by using the result of the read as the test condition for you loop, e.g.
while(fscanf(fp,"%s %s %s %f\n", barkod, urunadi, kategori, &birim_fiyat) == 4) {
if (strcmp (istenen,barkod) == 0) {
printf ("Aranan Ürün Bulundu: %s %s\n", urunadi, kategori);
ucret += birim * birim_fiyat;
}
}
Finally as noted in the comment above, you do not place an &
before istenen
in scanf("%s", istenen)
. Why? istenen
is already a pointer by virtue of an array being converted to a pointer to the first element, C11 Standard - 6.3.2.1 Other Operands - Lvalues, arrays, and function designators(p3)
Putting it altogether (and commenting the setlocale
for testing on my box), you would have:
#include <stdio.h>
// #include <conio.h>
#include <stdlib.h>
#include <string.h>
// #include <locale.h>
int main (void) {
int birim; // unit
float birim_fiyat; // unit price
char barkod[10]; // barcode
char kategori[10], urunadi[10], istenen[10];
float ucret=0.00; //price
// setlocale(LC_ALL,"Turkish");
// FILE *fp=fopen("barkod.txt","r+");
FILE *fp=fopen("dat/barcode.txt","r");
if (fp == NULL) {
fputs("cannot open file\n", stderr);
exit (EXIT_FAILURE);
}
fputs ("Enter the Product Barcode Number to be Called: ", stdout);
if (scanf("%s", istenen) != 1) { // NO & - istened alread a pointer
fputs ("error: user canceled input.\n", stderr);
exit (EXIT_FAILURE);
}
fputs ("How many: ", stdout);
if (scanf("%d", &birim) != 1) {
fputs ("error: invalid integer input.\n", stderr);
exit (EXIT_FAILURE);
}
while(fscanf(fp,"%s %s %s %f\n", barkod, urunadi, kategori, &birim_fiyat) == 4) {
if (strcmp (istenen,barkod) == 0) {
printf ("Aranan Ürün Bulundu: %s %s\n", urunadi, kategori);
ucret += birim * birim_fiyat;
}
}
}
(note: you have nothing that requires conio.h
so do not include that old DOS header. It makes your code 100% non-portable)
Also, unless you are writing back to the file, just open the file with "r"
instead of "r+"
.
Example Use/Output
$ ./bin/barcode
Enter the Product Barcode Number to be Called: 902
How many: 10
Aranan Ürün Bulundu: Havu‡ Meyve
Look things over an let me know if you have further questions.