首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >xv6 C跳过空行

xv6 C跳过空行
EN

Stack Overflow用户
提问于 2022-05-05 03:23:59
回答 1查看 84关注 0票数 0

我试图用C(对于xv6)编写一个程序,该程序返回输入文本或文件的最后"n“行数(本质上是尾部),但它不应该打印空行(”\n“)。目前,我的代码能够正确地忽略空行,但是如果要打印的第一行是空行,它仍然会打印空行。

例如,如果只打印两行,这就是我的输出:

\n (空白处)

那只敏捷的棕色狐狸跳过那只懒惰的狗。

但是输出应该如下所示:

那只敏捷的棕色狐狸跳过那只懒惰的狗。

我尝试过的事情:

  • 检查当前索引和buf[]中的下一个索引是否包含\n
  • ,检查当前索引和buf[]中的前一个索引是否包含\n

我觉得解决办法很简单,但我想不出来。有人有什么想法吗?

编辑-提供的完整代码

代码语言:javascript
复制
#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(); 
    }

}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-05-05 15:07:35

我的课程尾作业的要求是计数,而不是打印空行。因此,这需要对临时文件的编写代码进行一些修改,而我不想这样做,因为这并不是官方为什么要做的事情。

我意识到我的逻辑缺少了一个组件:两行文本之间的空行将显示为空白、空白和非空白。因此,我需要更新我的if语句如下:

代码语言:javascript
复制
if ((buf[chunk_index] == '\n') && (buf[chunk_index + 1] != '\n') &&
    (buf[chunk_index - 1] == '\n')) {
    printf(1, "");
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72121567

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档