I have to create a small data base for a shop. One of the functions i am creating is taking a customers ID and scanning that through a text file and to print out the info about that customer. What i am having trouble with is where do i insert the string compare in my program? some of the code may be wrong so any help will be helpful. Thanks
//declaring array for input of customer ID
int customer_ID [20];
printf("Please enter the customer ID:");
gets( customer_ID ); //users input stored in the array
FILE *fp; //declaring a file variable
char ch;
int i;
fp = fopen ("c:\\customers.txt", "r"); //opening file for reading
if (fp == NULL)
{
printf("Cannot open file. \n");
}
while ((ch = fgetc ( fp )) != EOF) //while the character is not the enn of file
{
printf("%c", ch);
}
fclose(fp); //closing the file
Instead of your while loop, do something like this instead:
1 2 3 4 5 6 7 8 9 10 11 12
string fileString;
ifstream infile;
infile.open ("customers.txt");
while(!infile.eof)
{
getline(infile,fileString);
// now use fileString to parse and get your
// customer id from the line. are your lines
// tab, space, or comma delimited. You will
// have to take this into account
}
infile.close();