【发布时间】:2017-07-18 16:37:24
【问题描述】:
我正在使用 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 未正确读取有关?
【问题讨论】:
-
我没有看到你提到的“重叠”。 应该输出如何?
-
也许你根本不应该打印
\r字符。 -
read()告诉您它实际读取了多少字节,并且您尽职尽责地将其记录在变量numRead中。为什么不使用它来确定要处理的缓冲区的字节数? -
在带有 -n 标志的“1”行上,开头缺少“An”。在“3”行,它缺少“1”。 before 'Change 另外,在使用 -b 标志时,我遇到了类似的重叠问题。 -b 在打印行号时也会错过文本文件的第二行和最后一行,我不知道为什么。
-
这很好,约翰 - 我确实将第二个 for() 循环更改为使用 numRead 而不是 BUFFERSIZE,但我仍然遇到打印正确行号的问题,我不能找出原因。感谢您提及有关 numRead 的内容。
标签: c file printf system-calls