all operations are on the file (stream). I am reading in the file, checking for one value, if found, report back, (reset stream) keep programming running. if not found report back ( reset stream) keep programming running.
there is no criteria to have an option to quit.
I added ftell to tell me what it is doing, it is showing 0 (zero) and still I get file not found when the information is valid. it is sporadic for the most part.
so I am not sure why it is not working all of the time.
Code:
#include <stdio.h>
int your_choice(void);
int item_number(void);
int main (int argc, const char **argv)
{
if(argc < 2)
{
printf("no file\n");
return -1;
}
FILE *fp;
if ( (fp = fopen(argv[1], "r") ) == NULL )
{
printf("File < %s > no open, yo!\n", argv[1]);
return -1;
}
char buffer[1024];
int inputID = 0, flag = 0, ItemId = 0, quantity= 0, price = 0;
while (1)
{
your_choice();
inputID = item_number();
while (( fgets(buffer, sizeof buffer, fp)) != NULL)
{
//if found kick it back one
//more loop and start over.
if ( flag == 1)
{
fseek(fp,0,SEEK_SET);
printf("reset flag\ntell me %ld\n", ftell(fp) );
flag = 0;
break;
}
//run it through the entire file, or until a match
//is found.
//either way this loop is done same results are gotten
//while ( sscanf(buffer, "%d", &ItemId) == 1 )
while ( sscanf(buffer, "%d%d%d", &ItemId, &quantity, &price) == 3 )
{
//used with other commented out while above
// sscanf(buffer, "%d%d%d", &ItemId, &quantity, &price);
printf("running in inner loop\ntell me %ld\n", ftell(fp) );
if ( inputID == ItemId )
{
printf("\n\n\nItemID Quantity Price Per Unit(SAR)\n");
printf("%d %10d %10d\n", ItemId,quantity,price);
getchar();
printf("\n\n\nPress Enter key to continue ...\n");
//set stream back to start
fseek(fp,0,SEEK_SET);
printf("inner loop\ntell me %ld\n", ftell(fp) );
flag = 1;
getchar();
break;
}
else
{
//keeps it going until NULL, or EOF
break;
}
} //end inner loop
} //end outer loop
//when buffer reaches NULL, to
//and to prevent it from chaning whenever
// the it matchs, on break
if ( inputID != ItemId)
{
getchar();
//set stream back to start
fseek(fp,0,SEEK_SET);
printf("error not found\ntell me %ld\n", ftell(fp) );
printf("\n\nError: Invalid item ID\n\n\n"
"Press Enter key to continue ...\n");
flag =1;
getchar();
}
//set stream back to start
//fseek(fp,0,SEEK_SET);
}//end outer 3rd loop
return 0;
}
int your_choice(void)
{
printf("Please Select your choice: ");
int choice = 0;
scanf(" %d", &choice);
return choice;
}
int item_number(void)
{
printf("Enter itemID: ");
int item_number = 0;
scanf(" %d", &item_number);
return item_number;
}
test file
Code:
1000 3 10
1001 1 30
1002 6 23
1003 9 32
1004 42 55
some results
Code:
reset flag
tell me 0
Please Select your choise: 2
Enter itemID: 4222
running in inner loop
tell me 10
running in inner loop
tell me 20
running in inner loop
tell me 30
running in inner loop
tell me 40
running in inner loop
tell me 51
error not found
tell me 0
Error: Invalid item ID
Press Enter key to continue ...
Please Select your choise: 1
Enter itemID: 10004
reset flag
tell me 0
error not found
tell me 0
Error: Invalid item ID
Press Enter key to continue ...
Please Select your choise: 1
Enter itemID: 1004
reset flag
tell me 0