Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/161.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String 使用C从字符串中获取某些部分_String_Search - Fatal编程技术网

String 使用C从字符串中获取某些部分

String 使用C从字符串中获取某些部分,string,search,String,Search,晚上好,希望各位大师能帮上忙。我试图找到这个问题的答案,我需要通过搜索标签从下面的字符串中读取数据。i、 然而,我使用的代码并不好,因为它只存储它的第一部分,例如UKPART=12999,并且遗漏了-0112。有没有更好的方法来搜索字符串 迄今为止的最新情况 #include <stdio.h> #include <string.h> #include <windows.h> int main () { // in my appl

晚上好,希望各位大师能帮上忙。我试图找到这个问题的答案,我需要通过搜索标签从下面的字符串中读取数据。i、 然而,我使用的代码并不好,因为它只存储它的第一部分,例如UKPART=12999,并且遗漏了-0112。有没有更好的方法来搜索字符串

迄今为止的最新情况

#include <stdio.h>  
#include <string.h>  
#include <windows.h>  

int main ()  
{  
    // in my application this comes from the handle and readfile 
    char buffer[255]="TEST999.UKPART=12999-0112...ISUE-125" ;     
    // 
    int i;  
    int codes[256];   
    char *pos = buffer;   
    size_t current = 0;   
    //   
       char buffer2[255];
   if ((pos=strstr(pos, "UKPART")) != NULL) {   
        strcpy (buffer2, pos); // buffer2 <= "UKPART=12999-0112...ISUE-125"
   }

        printf("%s\n", buffer2);  
    system("pause"); 
    return 0;  
}  
#包括
#包括
#包括
int main()
{  
//在我的应用程序中,这来自handle和readfile
字符缓冲区[255]=“TEST999.UKPART=12999-0112…ISUE-125”;
// 
int i;
整数码[256];
char*pos=缓冲区;
电流大小=0;
//   
字符缓冲区2[255];
如果((pos=strstr(pos,“UKPART”)!=NULL){
strcpy(buffer2,pos);//buffer2
  • strstr()绝对是搜索子字符串的正确方法。酷:)

  • 听起来您希望使用与“sscanf()”不同的内容来复制子字符串

  • 问:为什么不直接使用“strcpy()”

    EXAMPLE:
       char buffer[255]="IZTAG-12345...UKPART=12999-0112...ISUE-125" ;     
       char buffer2[255];
       if ((pos=strstr(pos, "UKPART")) != NULL) {   
            strcpy (buffer2, pos); // buffer2 <= "UKPART=12999-0112...ISUE-125"
    
    示例:
    字符缓冲区[255]=“IZTAG-12345…UKPART=12999-0112…ISUE-125”;
    字符缓冲区2[255];
    如果((pos=strstr(pos,“UKPART”)!=NULL){
    
    strcpy(缓冲区2,位置);//buffer2虽然没有完全定义您想要的内容,但使用a进行拆分更为灵活。@tiwo:非常好的建议。这里还有一个链接:如果没有代码,您的问题就没有意义,因此我还原了您对它的删除。我以前从未使用过此功能。很抱歉,我刚从php迁移过来。您的意思是用strcpy替换sscanf吗?抱歉这个愚蠢的问题。问:什么“不起作用”?如果可能的话,请发布一条错误消息。问:您记得在使用“buffer2”之前声明它吗?将数据读入“codes”是更好的变量吗?最重要的是:问:“UKPART=12999-0112…ISUE-125”你想读的结果是什么?很抱歉,我没有说清楚,我只需要返回UKPART=12999-0112,而不是字符串的结尾。因为这是另一个标记。现在输出UKPART=12999-0112…ISUE-125需要是UKPART=12999-0112谢谢paul。