我正在使用C语言编写一个程序,该程序将使用Linux系统调用打开,读取和关闭文件,并将文件的内容打印到屏幕上。命令格式为
$ mycat [-bens] f1 [f2 ...].
开关如下:
-b
显示每个非空白行的行号,从1开始
-e
会显示' $'在每一行的末尾
-n
显示每行的行号
-s
从输出中删除所有空行(实际上是单行间隔输出)
问题在于,当我使用-b
或-n
切换时,printf
似乎是"重叠"缓冲区尝试从文本文件本身打印的行号。
以下是我为该计划编写的代码:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <unistd.h>
#include <fcntl.h>
#include <getopt.h>
#define BUFFERSIZE 4096
void oops(char *, char *);
int main(int ac, char *av[])
{
int fd, numRead, curr, i, c;
char buf[BUFFERSIZE] = {0};
extern char *optarg;
extern int optind;
int tmpS = 0;
int tmpB = 0;
int bFlag = 0;
int eFlag = 0;
int nFlag = 0;
int sFlag = 0;
int bLineNum = 1;
int nLineNum = 1;
/* Flag processing in argument list */
while( (c = getopt(ac, av, "bens")) != -1)
{
switch(c)
{
case 'b':
bFlag = 1;
break;
case 'e':
eFlag = 1;
break;
case 'n':
nFlag = 1;
break;
case 's':
sFlag = 1;
break;
default:
exit(EXIT_FAILURE);
}
}
/* Scan through each argument after flag */
for(i = optind; i < ac; i++)
{
/* Error handling when opening each file */
if((fd = open(av[i], O_RDONLY)) == -1)
oops("Cannot open ", av[i]);
/* Read from file to buffer, until end is reached */
while( (numRead = read(fd, buf, BUFFERSIZE)) > 0)
{
/* Once buffer is filled, process each address in buffer */
for(curr = 0; curr < BUFFERSIZE; curr++)
{
/* sFlag squeezes output, eliminating blank lines */
if(sFlag && buf[curr] == '\n')
{
tmpS = curr + 1;
while(buf[tmpS] != '\r')
{
if(isspace(buf[tmpS]))
tmpS++;
else
break;
}
curr = tmpS + 1;
}
/* nFlag numbers each line, starting from 1 */
if(nFlag && buf[curr] == '\n')
printf("%d ", nLineNum++);
/* eFlag puts a '$' at the end of every line */
if(eFlag && buf[curr] == '\r')
printf(" $");
/* bFlag numbers every non-blank line, starting from 1 */
if(bFlag && buf[curr] == '\n')
{
tmpB = curr + 1;
if(isEmptyLine(buf, tmpB))
printf("%d ", bLineNum++);
}
/* Print the current character in the buffer address */
printf("%c", buf[curr]);
}
}
if(numRead == -1)
oops("Read error from ", av[i]);
}
return 0;
}
void oops(char *s1, char *s2)
{
fprintf(stderr, "Error: %s ", s1);
perror(s2);
exit(1);
}
int isEmptyLine(char *buf, int tmp)
{
while(buf[tmp] != '\n')
{
if(!isspace(buf[tmp]))
return 0;
tmp++;
}
return 1;
}
示例输入(file1.txt):
An excerpt from LEARNING DOS FOR THE COMPLETE NOVICE, by Steven Woas, copyright 1993.
1. Change to the compressed drive and then issue a CHKDSK
command like so:
c: <ENTER>
chkdsk /f <ENTER>
The /F tells DOS to fix errors.
Another option is to do it like so:
dblspace /chkdsk /f <ENTER>
A shortcut for the DBLSPACE /CHKDSK /F command is:
dblspace /chk /f <ENTER>
输出-n标志并运行:
sh-4.2$ ./main -n file1.txt
1 excerpt from LEARNING DOS FOR THE COMPLETE NOVICE, by Steven Woas, copyright 1993.
2
3 Change to the compressed drive and then issue a CHKDSK
4 command like so:
5
6 c: <ENTER>
7
8 chkdsk /f <ENTER>
9
10 The /F tells DOS to fix errors.
11
12 Another option is to do it like so:
13
14 dblspace /chkdsk /f <ENTER>
15
16 A shortcut for the DBLSPACE /CHKDSK /F command is:
17
18 dblspace /chk /f <ENTER>
我对-b
旗帜有同样的问题,我不知道为什么。是否与\r
和\n
无法正确阅读有关?
答案 0 :(得分:0)
您的程序会显示您为Windows(DOS)样式行结尾(\r\n
)描述的文件的错误行为,但对于具有UNIX样式行结尾的文件(仅\n
)和但是,对于具有MacOS类样式行结尾的文件,却存在不同的错误行为(仅\r
)。因为你似乎总体上假设了Windows风格的行结尾,所以我会关注它。
考虑当你的程序到达行尾时会发生什么。它首先处理\r
字符,最终打印它。这会导致输出位置返回到行的开头(这是可能的,因为默认情况下标准输出是行缓冲的)。然后打印行号,覆盖以前可能存在的行号,最后打印\n
字符,使缓冲区刷新,输出移动到下一行。
您可能应该将\r\n
序列识别为行尾,而不是单独尝试处理这些字符。这可能有点具有挑战性,因为你需要考虑这对被分割成两个read()
的可能性,但这不应该太难。如果您遇到单独的\n
和/或单独的\r
,这也可以让您考虑如何做,您的程序可以比现在更优雅地处理。