简体   繁体   English

xv6 C 跳过空行

[英]xv6 C skipping blank lines

I am trying to write a program in C (for xv6) that returns the last "n" number of lines of input text or a file (essentially tail) with the exception that it should not print blank lines("\n").我正在尝试在 C(对于 xv6)中编写一个程序,它返回输入文本或文件(基本上是尾部)的最后“n”行,但它不应该打印空行(“\n”)。 At the moment, my code is able to correctly ignore blank lines, but it will still print a blank line if the first line to be printed is a blank line.目前,我的代码能够正确地忽略空行,但如果要打印的第一行是空行,它仍然会打印空行。

For example, this is my output if only 2 lines are to be printed:例如,如果只打印 2 行,这是我的 output:

\n (a blank space) \n(一个空格)

the quick brown fox jumped over the lazy dog敏捷的棕色狐狸跳过了懒惰的狗

But the output should look like:但是 output 应该是这样的:

the quick brown fox jumped over the lazy dog敏捷的棕色狐狸跳过了懒惰的狗

Things I have tried:我尝试过的事情:

  • Checking if the current index, AND the next index in buf[] contains \n检查当前索引和 buf[] 中的下一个索引是否包含 \n
  • Checking if the current index AND the previous index in buf[] does not contain \n检查 buf[] 中的当前索引和先前索引是否不包含 \n

I feel like the solution is simple, but I can't figure it out.我觉得解决方案很简单,但我无法弄清楚。 Does anybody have any ideas?有人有什么想法吗?

edit - provided full code编辑 - 提供完整代码


#include "types.h"
#include "stat.h"
#include "user.h"
#include "fcntl.h"

char buf [1024];

void tail (int fd, char* name, int lines){
    int chunk_index; //keeps track of the chunk index 
    int chunk_size; //keeps track of the size of the chunk 
    int lines_in_doc = 0; //keeps track of the total number of lines
    int current_line_num = 0; //keeps track of the character count in each chunk
    int temp_line = open("temporary_file", O_CREATE | O_RDWR);

    while ((chunk_size = read(fd, buf, sizeof(buf))) > 0){
        write(temp_line, buf, chunk_size); 
        for (chunk_index = 0; chunk_index <= chunk_size; chunk_index++){
            if (buf[chunk_index] != '\n'){
                continue; 
            }else{
                lines_in_doc++; 
            }
        }
    }
    close(temp_line); 

    if (chunk_size < 0){ 
        printf(1, "tail - read error \n");
        exit();
    }
    //int total_chunks_read = 0; 
    temp_line = open("temporary_file", 0);

    while((chunk_size = read(temp_line, buf, sizeof(buf))) > 0){
        for (chunk_index = 0; chunk_index < chunk_size; chunk_index++){
            if (current_line_num >= (lines_in_doc - lines)){
                if ((buf[chunk_index] == '\n') && (buf[chunk_index+1] == '\n') && (buf[chunk_index-1] == '\n')){
                    printf(1,"haha!");
                }
                else{
                    printf(1, "%c", buf[chunk_index]); 
                }          
            }
            else if (buf[chunk_index] == '\n'){
                current_line_num++; 
            }
        }
    }
    close(temp_line); 
    unlink("temporary_file");
} 

//main function 

int 
main(int argc, char *argv[]){
    int i; 
    int fd = 0; 
    int x = 10; 
    char *file; 
    char a; 
    file = ""; 
    if (argc <= 1){
        tail(0, "", 10); 
        exit(); 
    } else{
        for (i = 1; i < argc; i++){
            a = *argv[i]; 
            if (a == '-'){
                argv[i]++;
                x = atoi(argv[i]++); 
            }else{
                if ((fd = open(argv[i], 0)) < 0){
                    printf(1, "tail: cannot open %s \n", argv[i]);
                    exit(); 
                }
            }

        }
    tail(fd, file, x); 
    close(fd); 
    exit(); 
    }

}


The requirements for my course's tail assignment was to count , but not print blank lines.我课程尾部作业的要求是计算,但不打印空行。 So, it would have taken some tinkering of the code of writing to the temporary file which I didn't want to do, since this is not what official tail.c does.因此,我不想对写入临时文件的代码进行一些修改,因为这不是官方的 tail.c 所做的。

I realized that my logic was missing a component: a blank line between two lines of text would appear as a blankspace, a blankspace, and a non-blankspace.我意识到我的逻辑缺少一个组件:两行文本之间的空行将显示为空格、空格和非空格。 So, I needed to update my if statement to be the following:所以,我需要更新我的 if 语句如下:

if ((buf[chunk_index] == '\n') && (buf[chunk_index + 1] != '\n') &&
    (buf[chunk_index - 1] == '\n')) {
    printf(1, "");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM