C 的 fget 可以被哄骗来处理文件中的“非”字符串吗?
具体来说,代码示例 此处效果很好,但仅限于字符串存储在文件中时。
有时我需要它来处理生成的字符串(存储在字符串变量中),但我很难说服 fgets 的第三个参数使用字符串变量,因为它是 指向 FILE 结构的指针。
或者也许有一个与 fgets 等效的功能可以用于字符串?
有什么建议吗?谢谢!
Specifically, the code sample here works great, but only when the string is stored in a file.
Sometimes I need it to process a generated string (stored in a string variable), but I'm having trouble convincing fgets's third parameter to work with string variables because it's a pointer to a FILE structure.
Or perhaps there's a functional equivalent to fgets that may be used on strings?
Any suggestions? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
本着快速拼凑答案的精神,这是我刚刚写的“sgets”。它尝试模拟 fgets 但使用字符串输入。
编辑修复了蒙特指出的错误(谢谢)。疯狂地输入一个实用程序,同时相信至少有 15 个具有完全相同想法的人正在疯狂地做同样的事情,并不会产生经过良好测试的代码。坏我了。原始版本在后续调用中包含换行符。
In the spirit of hacking together quick answers, here is "sgets" that I just wrote. It attempts to emulate fgets but with string input.
Edit Fixed a bug that Monte pointed out (thanks). Madly typing out a utility while believing that at least 15 other people with the exact same idea are frantically doing the same thing does not lead to well-tested code. Bad me. The original version was including the newline character on the succeeding call.
标准 C 库不提供该功能。
但是 AT&T 的安全/快速 I/O 库 确实启用了内存流,并且还提供了包装器代码以使用 FILE API 及其扩展。最后一次更新是 2005 年 2 月,所以要么他们最终解决了所有错误,要么他们现在无法承担维护它的费用 卢克·威尔逊 (Luke Wilson) 在工资单上 :-(
该软件包可以是 在此处下载。
The standard C library does not provide that functionality.
But AT&T's safe/fast I/O library does enable memory streams and also provides wrapper code to use the FILE API with their extensions. The last update is from Feb 2005 so either they finally worked out all the bugs or they can no longer afford to maintain it now that Luke Wilson is on the payroll :-(
The package can be downloaded here.
sscanf 应该可以做到。当然语义不同。
sscanf should do it. Ofcourse the semantics are different.
使用管道,然后使用
fdopen
打开管道以获取FILE *
,然后从中读取。Use a pipe, and then open the pipe with
fdopen
to obtain aFILE *
, then read from that.如果字符串已经在内存中,您可以在换行符上进行标记(如果您可以改变字符串并且不需要担心重入,则可以使用
strtok
,或者手动使用strchr
并自己复制到单独的缓冲区)。然而,您不会获得 stdio 函数通常为您提供的依赖于平台的换行符转换,因此,如果内存中的字符串使用 CRLF 行终止符,则需要额外小心。
If the string is already in memory, you could tokenize on newlines (either with
strtok
if you're okay with mutating the string and if don't need to worry about re-entrancy, or by manually usingstrchr
and copying to a separate buffer yourself).You wouldn't get platform-dependent newline conversion that the stdio functions would normally give you, however, so some extra care would be needed if your strings in memory use, say, CRLF line terminators.
您需要做的就是对字符串中的行结尾执行线性搜索。这是一个小程序,可帮助您开始编写自己的字符串流类。
All you need to do is perform a linear search for line endings in the string. Here is a small program to get you started writing your own string streaming class.
我修改了fgets函数的源代码:
和主函数:
和输出:
i modified fgets function's source code:
and main function:
and output: