Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
想象一下,我有一个包含以下内容的文本文件:
你好世界 #只是随机文本 123 010 780
而且我想做一个能够根据文件中的位置返回的函数,仅返回 123 作为 int,另一个仅返回 780。所有这些都忽略了注释“#asdjhajsdkn”。
首先使用fgets, 去除前导(可能还有尾随)空格来读取一行。如果第一个字符 then 是 a'#'则继续读取下一行。如果第一个字符是数字(使用 eg isdigit),则使用strtol转换为数字,并使用第二个参数知道行中的下一个数字在哪里,因此您应该strtol循环调用。
fgets
'#'
isdigit
strtol