将文件从指定行读取到另一行

时间:2023-02-06 08:20:38

I currently have a shell script in C that takes a command of a file, two numbers, mth line and nth line, and supposedly should output whats in between those lines:

我目前在C中有一个shell脚本,它接受一个文件,两个数字,第m行和第n行的命令,并且应该在这些行之间输出什么:

for example:

例如:

./output file1 2 6

would output file from line 2 to line 6

将文件从第2行输出到第6行

I have it implemented currently in a way of outputting the whole file, I have been trying to change it to output specifically between those lines

我目前以输出整个文件的方式实现它,我一直在尝试将其更改为在这些行之间具体输出

this is my code

这是我的代码

#include <fcntl.h>

int main(int argc, char *argv[])
{
    int file;
    int size;
    int l[1000];

    int firstNum = atoi(argv[2]);

    int secondNum = atoi(argv[3]);


    file = open(argv[1], O_RDONLY);

    if( file == -1)
    {
        printf("\n cannot open file \n");
    }

    while ((size=read(file,l,80)) > 0)
        write(1,l,size);




}

I tried to change l and size to firstNum and secondNum, which are the numbers entered from the command line, but still did not work and outputted one single line.

我试图将l和size更改为firstNum和secondNum,这是从命令行输入的数字,但仍然无法工作并输出一行。

What is a better way of doing so ?

这样做的更好方法是什么?

2 个解决方案

#1


2  

There are several issues with your code, so just go through them sequentially:

您的代码存在几个问题,因此请按顺序执行:

  • It's better to use high level fopen rather than low level open to open a file. So it's better to write this way:

    最好使用高级fopen而不是低级open来打开文件。所以最好这样写:

    FILE *file = fopen(argv[1], "r");
    if (file == NULL)
        exit(EXIT_FAILURE);
    
  • Your read is wrong as it reads exactly 80 characters instead of a line as you expect.

    您的读取错误,因为它正好可以读取80个字符而不是您期望的行。

    while ((size=read(file,l,80)) > 0)   // <-- WRONG because this reads 80 chars instead of one line
    
  • For similar reason as with open, it's better to use alternative like printf instead of low level read and write.

    出于与open相似的原因,最好使用类似printf的替代方法而不是低级读写。

  • To read line by line, you should use library function getline.

    要逐行阅读,您应该使用库函数getline。

  • To control what line number to print, a simple way is to have a variable tracking what line number and compare with your command line arguments.

    要控制要打印的行号,一种简单的方法是使用变量跟踪行号并与命令行参数进行比较。

So put them together, you would need something like this:

所以把它们放在一起,你需要这样的东西:

FILE *file = fopen(argv[1], "r");
if (file == NULL)
    exit(EXIT_FAILURE);

int line_num = 0;

char * line = NULL;
size_t len = 0;
ssize_t read = 0;
while ((read = getline(&line, &len, file)) != -1)
{
    line_num++;
    if( firstNum <= line_num && line_num <= secondNum )
    {
        printf( "line %d: %s", line_num, line );
        if( line_num == secondNum )
            break;
    }
}

#2


0  

Try looking at this post: C read file line by line

试着看这篇文章:C逐行读取文件

From there it should be a simple matter of counting lines read and discarding them until you have read firstNum lines at which point print until you've reached secondNum.

从那里开始计算行读数并丢弃它们应该是一个简单的事情,直到你读取firstNum行,然后打印到你的secondNum。

#1


2  

There are several issues with your code, so just go through them sequentially:

您的代码存在几个问题,因此请按顺序执行:

  • It's better to use high level fopen rather than low level open to open a file. So it's better to write this way:

    最好使用高级fopen而不是低级open来打开文件。所以最好这样写:

    FILE *file = fopen(argv[1], "r");
    if (file == NULL)
        exit(EXIT_FAILURE);
    
  • Your read is wrong as it reads exactly 80 characters instead of a line as you expect.

    您的读取错误,因为它正好可以读取80个字符而不是您期望的行。

    while ((size=read(file,l,80)) > 0)   // <-- WRONG because this reads 80 chars instead of one line
    
  • For similar reason as with open, it's better to use alternative like printf instead of low level read and write.

    出于与open相似的原因,最好使用类似printf的替代方法而不是低级读写。

  • To read line by line, you should use library function getline.

    要逐行阅读,您应该使用库函数getline。

  • To control what line number to print, a simple way is to have a variable tracking what line number and compare with your command line arguments.

    要控制要打印的行号,一种简单的方法是使用变量跟踪行号并与命令行参数进行比较。

So put them together, you would need something like this:

所以把它们放在一起,你需要这样的东西:

FILE *file = fopen(argv[1], "r");
if (file == NULL)
    exit(EXIT_FAILURE);

int line_num = 0;

char * line = NULL;
size_t len = 0;
ssize_t read = 0;
while ((read = getline(&line, &len, file)) != -1)
{
    line_num++;
    if( firstNum <= line_num && line_num <= secondNum )
    {
        printf( "line %d: %s", line_num, line );
        if( line_num == secondNum )
            break;
    }
}

#2


0  

Try looking at this post: C read file line by line

试着看这篇文章:C逐行读取文件

From there it should be a simple matter of counting lines read and discarding them until you have read firstNum lines at which point print until you've reached secondNum.

从那里开始计算行读数并丢弃它们应该是一个简单的事情,直到你读取firstNum行,然后打印到你的secondNum。

相关文章