c++ - 基于转义字符的字符串解析

标签 c++ c parsing

我正在尝试解析以下字符串:

"USBSTOR\DISK&VEN_JETFLASH&PROD_TRANSCEND_8GB&REV_1100\00H8096XQ9UW1BQ5&0:JetFlash Transcend 8GB USB Device"

基于'\'(字符)

问题 1:但是这个字符被认为是转义字符

问题 2:字符串中间的 \0 被认为是字符串的结尾。

我尝试了很多方法。

(i) 我尝试将 '\' 替换为另一个字符,例如 '$' 并尝试使用 sscanf() 进行解析,但是它没有用。

你们能推荐点什么吗?

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

int main()
{
    char str[80] = "This is \www.tutorialspoint.com \website";
    const char s[2] = "\\";
    char *token;

    /* get the first token */
    token = strtok(str, s);

    /* walk through other tokens */
    while( token != NULL )
    {
      printf( " %s\n", token );

      token = strtok(NULL, s);
    }

    return(0);
}

最佳答案

做这个修改 char str[80] = "这是\\www.tutorialspoint.com\\website";

这样,您的输出是:

This is 
www.tutorialspoint.com 
website

记住:您在代码中使用的任何字符串文字都需要反斜杠的转义序列。

关于c++ - 基于转义字符的字符串解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19373940/

相关文章:

c++ - 如何在归并排序中找到低点和高点

c++ - 如何从现有的 git cmake 项目创建 Xcode 项目?

c - c中使用malloc为线程分配栈,使用SP在栈上存储上下文

c - 如何在 ctrl+C 上关闭管道?

c - MKL CBlas 错误

validation - 读取大 csv 文件,使用 uniVocity 解析器验证并写出

java - 使用 Joda 的 DateTimeFormatter 解析时如何忽略时区?

html - IMG 和 TD 的 CSS 宽度/高度

c++ - boost::program_options::positional_options_description 终止

c++ - 运行时检查失败 #0。不知道如何使用 CDECL