C 如何打印与pin码相关的输出?
在这段代码中,我试图打印与pin码相关的数据。这意味着如果用户输入pin码,则应打印该pin码的数据。即使我在循环之外打印数据,从另一个文件读取pin码,它也会给我另一个文件C 如何打印与pin码相关的输出?,c,fgets,C,Fgets,在这段代码中,我试图打印与pin码相关的数据。这意味着如果用户输入pin码,则应打印该pin码的数据。即使我在循环之外打印数据,从另一个文件读取pin码,它也会给我另一个文件2000我尝试了很多,但失败了 文件中的数据 Name,ID,Pin,Amount,Phone Bilal Khan,1111111111111,1122,1000,1122334 Ali Ahmed,2222222222222,2233,2000,66778899 #include <stdio.h> #
2000
我尝试了很多,但失败了
文件中的数据
Name,ID,Pin,Amount,Phone
Bilal Khan,1111111111111,1122,1000,1122334
Ali Ahmed,2222222222222,2233,2000,66778899
#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);
}
}
给定输出
1000
2000
2000
Relevant to the Pin Code.
预期产出
1000
2000
2000
Relevant to the Pin Code.
代码
Name,ID,Pin,Amount,Phone
Bilal Khan,1111111111111,1122,1000,1122334
Ali Ahmed,2222222222222,2233,2000,66778899
#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);
}
}
#包括
#包括
#包括
#定义字符串\u LEN 200
int main(){
FILE*fp1=fopen(“FILE.csv”,“r”);
字符字符串[string_LEN];
char*line=NULL,*pinFound=NULL,*wordOne=NULL,*wordTwo=NULL,*wordThree=NULL,*wordFour=NULL,*wordFive=NULL;
而(fgets(字符串、字符串、fp1)){
line=strtok(字符串“\n”);
pinFound=strstr(第2233行);
wordOne=strtok(行“,”);
wordTwo=strtok(NULL,“”,“”);
wordThree=strtok(空,“,”);
wordFour=strtok(NULL,“”,“”);
wordFive=strtok(空,“,”);
如果(找到)
printf(“%s\n”,wordFour);
}
}
之所以有此输出,是因为strstrstr在两行中找到了2233<代码>比拉尔·汗,111111111 2210001122334包含2233个字符。对于本例,您可以替换pinFound=strstrstr(第2233行)代码>通过pinFound=strstrstr(第2233行)代码>用于测试,但正确的解决方案是使用类似regexp的东西。您有这个输出,因为strstrstr在两行中找到2233<代码>比拉尔·汗,111111111 2210001122334
包含2233个字符。对于本例,您可以替换pinFound=strstrstr(第2233行)代码>通过pinFound=strstrstr(第2233行)代码>用于测试目的,但正确的解决方案是使用类似regexp的东西