读取直到 EOF 但执行 \n 之前的每一行
仅使用系统调用,如何读取文件直到 EOF,并在此过程中执行程序中的每一行,直到行尾。 我的文件中的每一行都有一个必须执行的程序名称。
size_t fd1 = open("inputfile.txt", O_RDWR);
char buf1[BUFFSIZE];
while(read(fd1,buf1,10) != EOF)
{
if(fd1[MAXDATA] == "\n")
}
Using just System Calls, how do you read a file upto the EOF and in the process execute every line in the program, upto end of line.
Every line in my file, will have a program name that has to be executed.
size_t fd1 = open("inputfile.txt", O_RDWR);
char buf1[BUFFSIZE];
while(read(fd1,buf1,10) != EOF)
{
if(fd1[MAXDATA] == "\n")
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我最近创建了一个具有类似功能的程序:
i created a program with similar functionality recently:
尝试这样的操作:
另请注意:
在生产代码中,需要进行错误检查,因为 fgetline() 不区分错误和 EOF,并且在两种情况下都返回 NULL。
仅仅从文件中读出命令并执行它们是有潜在危险的。人们可以插入恶意代码并使其执行。
为了简单起见,我使用 1024 字节的缓冲区,但在现实生活中,您必须进行边界检查和/或动态内存分配,以避免缓冲区溢出。
Try something like this:
Please note also:
in production code, error checking will be needed, since fgetline() doesn't distinguish between error and EOF, and returns NULL in both cases.
It's potentially dangerous to just read out commands from a file and execute them. One can insert malicious code and make it executed.
I use a buffer of 1024 bytes for the sake of simplicity, but IRL you'll have to do bound-checking and/or dynamic memory allocation in order to avoid buffer overflows.