读取直到 EOF 但执行 \n 之前的每一行

发布于 2024-12-28 02:51:00 字数 242 浏览 4 评论 0原文

仅使用系统调用,如何读取文件直到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

万劫不复 2025-01-04 02:51:00

我最近创建了一个具有类似功能的程序:

#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#define BUFFERSIZE 1024
#define LINEMAXSIZE 2000
void main(int argvc,char** argv)
{
int filedesc=open(argv[1],O_RDONLY);
char buffer[BUFFERSIZE]; 

char expression[LINEMAXSIZE]; //the line
int exprindex=0;        //line index

int count=read(filedesc,buffer,sizeof(buffer));//read bytes

while(count!=EOF)
      {
            int i=0;
            while(i<count)
            {
            char c=buffer[i];
            if(c=='\n')
            {
                expression[exprindex++]='\0';
                char* line=strdup(expression);//create a new instance of the string
                system(line); //execute the line 
                exprindex=0;//set line index to 0
            }
            else
            {
                   expression[exprindex++]=c;

                if(exprindex>=LINEMAXSIZE)
                {
                printf("Line Max length reached\n");
                }

            }

        i++;
        }


        count=read(filedesc,buffer,sizeof(buffer));
    }
}

i created a program with similar functionality recently:

#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#define BUFFERSIZE 1024
#define LINEMAXSIZE 2000
void main(int argvc,char** argv)
{
int filedesc=open(argv[1],O_RDONLY);
char buffer[BUFFERSIZE]; 

char expression[LINEMAXSIZE]; //the line
int exprindex=0;        //line index

int count=read(filedesc,buffer,sizeof(buffer));//read bytes

while(count!=EOF)
      {
            int i=0;
            while(i<count)
            {
            char c=buffer[i];
            if(c=='\n')
            {
                expression[exprindex++]='\0';
                char* line=strdup(expression);//create a new instance of the string
                system(line); //execute the line 
                exprindex=0;//set line index to 0
            }
            else
            {
                   expression[exprindex++]=c;

                if(exprindex>=LINEMAXSIZE)
                {
                printf("Line Max length reached\n");
                }

            }

        i++;
        }


        count=read(filedesc,buffer,sizeof(buffer));
    }
}
記柔刀 2025-01-04 02:51:00

尝试这样的操作:

FILE *f = fopen("filename", "r");
char *line = NULL;
size_t length = 0;
char buf[1024];
do
{
    line = fgetline(f, &length);
    if (line)
    {
        strncpy(buf, line, length);
        buf[length] = '\0';
        system(buf);
    }
} while (line);
fclose(f);

另请注意:

  1. 在生产代码中,需要进行错误检查,因为 fgetline() 不区分错误和 EOF,并且在两种情况下都返回 NULL。

  2. 仅仅从文件中读出命令并执行它们是有潜在危险的。人们可以插入恶意代码并使其执行。

  3. 为了简单起见,我使用 1024 字节的缓冲区,但在现实生活中,您必须进行边界检查和/或动态内存分配,以避免缓冲区溢出。

Try something like this:

FILE *f = fopen("filename", "r");
char *line = NULL;
size_t length = 0;
char buf[1024];
do
{
    line = fgetline(f, &length);
    if (line)
    {
        strncpy(buf, line, length);
        buf[length] = '\0';
        system(buf);
    }
} while (line);
fclose(f);

Please note also:

  1. in production code, error checking will be needed, since fgetline() doesn't distinguish between error and EOF, and returns NULL in both cases.

  2. It's potentially dangerous to just read out commands from a file and execute them. One can insert malicious code and make it executed.

  3. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文