#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], pinFind[STRING_LEN];
char * pinFound = NULL;
printf("Enter the pin: ");
scanf("%s", pinFind);
while(fgets(string, STRING_LEN, fp1)){
pinFound = strstr(string, pinFind);
if(pinFound){
char * word1 = strtok(string, ",");
char * word2 = strtok(NULL, ",");
char * word3 = strtok(NULL, ",");
char * word4 = strtok(NULL, ",");
char * word5 = strtok(NULL, ",");
char * word6 = strtok(NULL, ",");
printf("%s", word6);
}
}
return 0;
}
What I have tried:
In this code, I am trying to take the last word from a file but don't know how to do it? The result is like this.
18-10-2020
19-10-2020
20-10-2020
If I use
strrchr() to take the last occurence then the last the occurence is newline and it won't return and any result. But I use
strchr then the first occurence is also newline, so it won't take the last occurence. Instead the second last occurence will be taken but I want to print the last one.
Here is the file data
Name,ID,Phone number,Pin code,Deposit,Date
Ali Ahmed1,2222222222222,77777,6677,500,17-10-2020
Bilal Khan,2222222222222,77777,1122,500,18-10-2020
Ali Ahmed1,2222222222222,77777,6677,500,17-10-2020
Talha,2222222222222,77777,1122,500,19-10-2020
Ali Ahmed1,2222222222222,77777,1122,500,20-10-2020
Bilal Khan,2222222222222,77777,6677,500,17-10-2020