在移动strchr指针后使用strtok
Using strtok after moving strchr pointer
我试图解析一个字符串的形式:
structure: something 2 3 4 5 6 7 10 242 12
我想要的是将整数值打印到文件中。现在,我可以使用strtok并根据空格分割字符串,然后对"structure:"answers"something"进行字符串分割,而不将它们打印到文件中。但是它们可以是任何单词,所以这只适用于这个特定的情况。但它的格式总是
<word1> <word2> 1 2 3 4 n
我尝试使用strchr将指针移动到2的前面,然后在随后的字符串上使用strtok,这将允许我只拆分整数。我做了如下操作:
char number[256];
char *pch;
// using strchr to navigate to second space in line,
// then use strtok to split numbers
pch = strchr(buf, ' ');
pch = strchr(pch + 1, ' ');
pch = strtok(buf, " ");
while (pch != NULL) {
fprintf(outputFile, "%sn", pch);
pch = strtok(NULL, " ");
}
显然这不起作用,只是打印该行中的所有内容。尝试增加strchr也可能是错误的,但我认为它会在它找到的第一个空格字符上增加,然后找到第二个空格(2之前的那个)。然后,我想从指针开始并删除之后的所有内容(尽管在这种情况下我只是重新分配指针)。
那么,是否有可能在"某事"之后得到一个字符串,然后运行strtok ?
用pch
代替buf
pch = strtok(buf, " ");
所以将这行更新为
pch = strtok(pch, " ");
相关文章:
- 1d 智能指针不适用于语法 (*)++
- 在C#中处理C++指针而不使用unsafe的最佳方法
- 为什么使用 "this" 指针调用派生成员函数?
- 函数向量_指针有不同的原型,我可以构建一个吗
- 使用指针从C++中的数组中获取最大值
- 助记符和指向成员语法的指针
- 嵌入方指针压缩已禁用
- 数组的指针从不分段故障
- C++ 指针的内存地址和指向数组的内存地址如何相同?
- 何时在引用或唯一指针上使用移动语义
- QMetaObject invokeMethod的基于函数指针的语法
- 如何从 std::atomic 中提取指针 T<T>?
- 如何在 C# 中映射双 C 结构指针?
- C++将浮点指针值舍入为小数位数
- 为什么++(*p)更改指针值
- 调整大小后指向元素值的指针unordered_map有效?
- strtok() - 为什么必须传递 NULL 指针才能获取字符串中的下一个标记
- 如何使"strtok function"一次使用多个令牌字符串? 函数指针会解决这个问题吗?
- 使用strtok函数打印命令行参数标记.如何使用此函数返回指向指针的指针
- 在移动strchr指针后使用strtok