c - 在 C89/C90 中解析字符串
问题描述
解析字符串
char* "AS LSLDS SAAL SS"
解决方案
像这样:
int offset;
sscanf(line, "%s %s %d %n", location, direction, &length, &offset);
name = line + offset; // or strncpy or something, if you want a new string instead of just a pointer into the old one
%n
记录到目前为止已经消耗了多少字符。如果你向前跳转那么多字符,那么你会得到字符串的其余部分。
旁注:%s
在没有最大字段宽度的情况下使用(或让它通过m
修饰符自行分配)是危险的,除非你 100% 确定你永远不会得到一个过长的词。
推荐阅读
- amazon-cloudformation - CloudFormation - DHCPOptions - DomainNameServers 数组不保持顺序
- matlab - 如何使for循环倒退?
- linux - 从 linux 信号处理程序初始化 c++11 函数静态变量是否安全?
- linux - 启用 set -e 时防止 shell 脚本在某些命令上退出
- mysql - MySQL 将在什么时间执行每天安排的事件?
- node.js - 需要关于rabbitmq的推荐
- pipeline - GOCD api比较管道中两个触发器之间的变化
- php - Stream_get_contents() 不适用于 Cron 作业
- uwp-xaml - How do i change background color on a Popup element located in a ContentDialog?
- excel - Import XML file relating parent nodes number to their attributes Power query