使用 strtok 时如何跳过输入文件中的空行?
我想使用 strtok 传递文件的行;值以逗号分隔。但是,strtok 也会读取仅包含空格的空行。在这种情况下不是应该返回空指针吗?
我怎么能忽略这样的一行呢?我尝试检查 NULL,但如上所述,它不起作用。
I want to pass lines of a file using strtok; the values are comma separated. However, strtok also reads blank lines which only contain spaces. Isn't it suppose to return a null pointer in such a situation?
How can I ignore such a line? I tried to check NULL, but as mentioned above it doesn't work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
所以要解释一下。
您正在使用 while 循环读取文件,该循环将整个文件逐行 (
fgets
) 读取到数组line_read
中。每次读入一行时,它都会检查该行是否包含任何内容(
NULL
检查)。如果它确实包含某些内容,则使用
strtok
解析它并将其读入keep_me
中,否则它将保留在line_read
数组中,而您显然不会这样做。不要在你的程序中使用。So to explain.
You're reading in your file using a while loop which reads the entire file line by line (
fgets
) into the arrayline_read
.Every time it reads in a line it will check to see if it contains anything (the
NULL
check).If it does contain something it was parse it using
strtok
and read it intokeep_me
otherwise it will stay in theline_read
array which you obviously don't use in your program.