Reputation:
In this code, I am trying to print the data that is relevant to the pin code. It means if a user enters the pin code, the data of that pin code should be printed. Even if I print the data outside of the loop, read pin code from another file, it gives me the another file 2000
I tried a lot but failed.
Data in the file
Name,ID,Pin,Amount,Phone
Bilal Khan,1111111111111,1122,1000,1122334
Ali Ahmed,2222222222222,2233,2000,66778899
Given output
1000
2000
Expected output
2000
Relevant to the Pin Code.
Code
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define STRING_LEN 200
int main(){
FILE *fp1 = fopen("file.csv", "r");
char string[STRING_LEN];
char * line = NULL, *pinFound = NULL, *wordOne = NULL, *wordTwo = NULL, *wordThree = NULL, *wordFour = NULL, *wordFive = NULL;
while(fgets(string, STRING_LEN, fp1)){
line = strtok(string, "\n");
pinFound = strstr(line, "2233");
wordOne = strtok(line, ",");
wordTwo = strtok(NULL, ",");
wordThree = strtok(NULL, ",");
wordFour = strtok(NULL, ",");
wordFive = strtok(NULL, ",");
if(pinFound)
printf("%s\n", wordFour);
}
}
Upvotes: 0
Views: 81
Reputation: 1746
You have this output because strstr find 2233 in the two lines. Bilal Khan,1111111111111,1122,1000,1122334
contains 2233. For this example you can replace pinFound = strstr(line, "2233");
by pinFound = strstr(line, ",2233,");
for test purpose, but the correct solution would be to use something like regexp.
Upvotes: 1