如何打印与密码相关的输出?
How to print the output that is relevant to the pin code?
在此代码中,我试图打印与个人识别码相关的数据。这意味着如果用户输入密码,则应打印该密码的数据。即使我在循环外打印数据,从另一个文件读取 pin 代码,它也会给我另一个文件 2000
我尝试了很多但失败了。
文件中的数据
Name,ID,Pin,Amount,Phone
Bilal Khan,1111111111111,1122,1000,1122334
Ali Ahmed,2222222222222,2233,2000,66778899
给定输出
1000
2000
预期输出
2000
Relevant to the Pin 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);
}
}
你有这个输出是因为 strstr 在两行中找到了 2233。 Bilal Khan,1111111111111,1122,1000,1122334
包含 2233。对于此示例,您可以将 pinFound = strstr(line, "2233");
替换为 pinFound = strstr(line, ",2233,");
以进行测试,但正确的解决方案是使用正则表达式之类的东西。
在此代码中,我试图打印与个人识别码相关的数据。这意味着如果用户输入密码,则应打印该密码的数据。即使我在循环外打印数据,从另一个文件读取 pin 代码,它也会给我另一个文件 2000
我尝试了很多但失败了。
文件中的数据
Name,ID,Pin,Amount,Phone
Bilal Khan,1111111111111,1122,1000,1122334
Ali Ahmed,2222222222222,2233,2000,66778899
给定输出
1000
2000
预期输出
2000
Relevant to the Pin 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);
}
}
你有这个输出是因为 strstr 在两行中找到了 2233。 Bilal Khan,1111111111111,1122,1000,1122334
包含 2233。对于此示例,您可以将 pinFound = strstr(line, "2233");
替换为 pinFound = strstr(line, ",2233,");
以进行测试,但正确的解决方案是使用正则表达式之类的东西。