- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试使用以下命令从文件中读取字符串
while(fscanf(fd, "%s ", word) != EOF) {}
其中 fd 是文件,word 是我存储字符串的地方。但是,这有效地使用了空格作为分隔符。目前,如果我有一个读取“this% is, the4 str%ng”的文件,它会产生字符串“this%”、“is”、“the4”和“str%ng”。我需要它是“这个”"is"“那个”“str”“ng”。是否可以使用 fscanf 执行此操作,或者我还需要使用其他东西吗?
最佳答案
这些答案显示了 "%[]
格式说明符的使用。举个例子,假设您使用它从控制台获取两个字符串:
#include <stdio.h>
int main(void){
char s1[100] = "", s2[100] = "";
int res;
res = scanf("%99[^%]%%%99[^%]%%", s1, s2);
printf("%d %s %s\n", res, s1, s2);
}
第一个 %
开始每个格式规范,^%
告诉 scanf
在 %
处停止,下一个“转义”双 %
告诉 scanf 读取停止扫描的 %
。然后对第二个字符串重复,因此一个字符串的格式规范是 %99[^%]%%
。
为了让格式看起来更简单,假设分隔符不是%
而是#
,那么代码就是:
#include <stdio.h>
int main(void){
char s1[100] = "", s2[100] = "";
int res;
res = scanf("%99[^#]#%99[^#]#", s1, s2);
printf("%d %s %s\n", res, s1, s2);
}
函数fscanf
类似。
这个答案不处理“未知”分隔符,所以我修改了代码。
#include <stdio.h>
int main(void){
char s1[100] = "";
while(scanf("%99[^!£$%&*()_-+={};:'@#~,.<>/?0123456789]", s1) == 1) {
getchar(); // remove the delimiter
printf("%s\n", s1);
}
}
请注意,我没有包含字符 ^
或 "
或 [
或 ]
作为分隔符。
关于使用 fscanf 时,我可以将非字母字符设置为 c 中的定界符吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49117741/
我有一列data.frame,其中一些空格应该作为分隔符,而某些只是一个空格。 #input data dat <- data.frame(x=c("A 2 2 textA1 textA2 Z1",
所以我试图分解一个包含答案列表的字符串。 例如:答案:1. 梳理。 2.拇指。 3.坟墓(地下墓穴)。 4. 子宫。 5.面包屑。 6. 炸弹。 7. NumPy 。 8.沉着冷静。 9. 屈服。 有
我有包含多个查询的文件。如何使用 DELIMITER 将它们分开?我像在 mysql 中一样尝试过,但它没有编译: DELIMITER % CREATE SEQUENCE logins_seq I
这个问题在这里已经有了答案: Does -* have any special meaning in regular expression? (1 个回答) 关闭 8 年前。 我有一个像 22 +
我正在尝试使用 sscanf 解析以下字符串: query=testword&diskimg=simple.img 如何使用 sscanf 解析出“testword”和“simple.img”? ss
我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示: foo 4.500 bar 3.00 1.3 3 foo bar 如何读取由空格而不是换行符分隔的文件?有什么方法可以设置
这是我的代码: With ad.Tables(2) For i As Integer = 0 To .Rows.Count - 1 If .Rows(i)("name") &
我正在排队,例如 数组大小:4 来自 cin,我想首先检查字符串是否正是这个,然后提取整数。 我找到了读取字符串并提取整数的方法: string start; getline (cin
我正在尝试读取以这种形式编写的文本文件: AB523:[joe, pierre][charlie][dogs,cat] ZZ883:[ronald, zigomarre][pele]
我是 PIG 的新手,有人可以帮助我如何加载一个包含多个字符(在我的例子中为“^^”)作为列分隔符的文件。 例如我有以下列的文件aisforapple^^bisforball^^cisforcat^^
var wrapped_string = "shouldn't this\ work?" JSLint给出了严重的擒纵机构错误。 那么我们如何在 JavaScript 中执行heredoc呢?我有一个
问题:我需要读取 CSV 文件。我使用 FileHelpers 库来实现这一点。 问题是我需要一个动态定界符(用户定义),这意味着任何东西都可以是定界符(逗号、分号、制表符、换行符,以及其他任何东西)
我试图读入一个由 powershell 执行的 sql 文件。一切正常,除了 powershell 因单词定界符而抛出错误。所以我的问题是如何在我的 sql 文件中使用定界符(我只能对 sql 文件进
我需要能够通过 DBI 发出“创建触发器”。我似乎无法使分隔符命令正常工作。任何人都可以找到一种方法来完成这项工作吗? 代码: use strict; use DBI; my $dbargs = {m
我知道我可以使用 Mustache.tags('[[', ']]'); 更改默认分隔符 我深入研究了源代码,但找不到也不知道如何更改不转义 HTML 定界符,默认情况下为 {{{ }}}。感谢您的帮助
我正在尝试获取 std::string 值并将其用作 std::getline() 函数中的分隔符但无论我做什么,编译器都会给我错误。这就是我想要做的: std::stringstream ss(s)
我正在尝试实现我自己的 beanstalkd 客户端作为学习围棋的一种方式。 https://github.com/kr/beanstalkd/blob/master/doc/protocol.txt
我正在编写一个显示代码和输出的样式指南。它目前的结构使得代码只需要描述一次,并以其原始版本和解释版本显示,如下所示: #{ image_tag 'image.png' } PLACE_THE
是否可以将默认字段分隔符从逗号更改为其他字符,例如用于导出的 '|'? 最佳答案 下面是一个使用选项卡的示例。 到一个文件: CSV.open("myfile.csv", "w", {:col_sep
我是一名优秀的程序员,十分优秀!