由于需要可靠地检测两个underlength和超长线,并重新同步你输入之后或者,它可能比较容易编写使用getc()
读取数据的功能。
你的标准功能选项包括:
fgets()
—将无法读取数据太多,但你必须确定它是否有一个换行符(这将被包含在输入)和处理阅读长度过长的行时重新同步(不是非常困难)。
fread()
—将读取正确的长度,并且如果您认为长度过长和长度过短的行将是极少出现的情况,那么这将是一个不错的选择。出现错误后的重新同步不是微不足道的,特别是如果出现相邻的错误行。
getline()
— POSIX 2008.分配足够的内存用于它读取的行的长度,如果你只是要放弃长度过长的行,这是有点浪费。
因为它们不合适,你最终会写自己的。
现在测试的代码。 (在第一个if
需要修复,通过Dave诊断出来,问题是我最初写了反相条件(if ((c = getc(fp)) != EOF && c != '\n')
),然后在我将逻辑反转后导致分心,导致条件“不完全倒置”。)
这个关键部分是两个while循环。
第一次while循环读取到行尾,存储数据和计数字符—正常操作。如果该行长度正确,则在读取换行符时循环将被中断。请注意<=
的条件;如果你考虑linelen == 1
时的循环,你会发现<=
在这里是正确的,尽管<
比较平常。如果线路短路,则count
将指示该线路。
while while循环处理过长的行,读到行尾并丢弃结果。它使用x
而不是c
,因为在返回语句中需要c
。
/*
@(#)File: $RCSfile: rdfixlen.c,v $
@(#)Version: $Revision: 1.2 $
@(#)Last changed: $Date: 2012/04/01 00:15:43 $
@(#)Purpose: Read fixed-length line
@(#)Author: J Leffler
*/
/* Inspired by https://stackoverflow.com/questions/9957006 */
#include <stdio.h>
#include <assert.h>
extern int read_fixed_length_line(FILE *fp, char *buffer, int linelen);
/* Read line of fixed length linelen characters followed by newline. */
/* Buffer must have room for trailing NUL (newline is not included). */
/* Returns length of line that was read (excluding newline), or EOF. */
int read_fixed_length_line(FILE *fp, char *buffer, int linelen)
{
int count = 0;
int c;
assert(fp != 0 && buffer != 0 && linelen > 0);
while (count < linelen)
{
if ((c = getc(fp)) == EOF || c == '\n')
break;
buffer[count++] = c;
}
buffer[count] = '\0';
if (c != EOF && c != '\n')
{
/* Gobble overlength characters on line */
int x;
while ((x = getc(fp)) != EOF && x != '\n')
count++;
}
return((c == EOF) ? EOF : count);
}
#ifdef TEST
#include "posixver.h"
#include <stdarg.h>
#include <unistd.h>
#include <string.h>
int main(void)
{
enum { MAXLINELEN = 10 };
int actlen;
char line[16];
int lineno = 0;
memset(line, sizeof(line), '\0');
while ((actlen = read_fixed_length_line(stdin, line, MAXLINELEN)) != EOF)
{
lineno++;
if (actlen != MAXLINELEN)
{
if (actlen > MAXLINELEN)
printf("%2d:L: length %2d <<%s>>\n", lineno, actlen, line);
else
printf("%2d:S: length %2d <<%s>>\n", lineno, actlen, line);
}
else
printf("%2d:R: length %2d <<%s>>\n", lineno, actlen, line);
assert(line[MAXLINELEN-0] == '\0');
assert(line[MAXLINELEN+1] == '\0');
}
return 0;
}
#endif /* TEST */
测试数据和输出
$ cat xxx
abcdefghij
a
Abcdefghij
ab
aBcdefghij
abc
abCdefghij
abcd
abcDefghij
abcde
abcdEfghij
abcdef
abcdeFghij
abcdefg
abcdefGhij
abcdefgh
abcdefgHij
abcdefghi
abcdefghIj
abcdefghiJ
abcdefghiJ1
AbcdefghiJ
abcdefghiJ12
aBcdefghiJ
abcdefghiJ123
$ ./rdfixlen < xxx
1:S: length 0 <<>>
2:R: length 10 <<abcdefghij>>
3:S: length 1 <<a>>
4:R: length 10 <<Abcdefghij>>
5:S: length 2 <<ab>>
6:R: length 10 <<aBcdefghij>>
7:S: length 3 <<abc>>
8:R: length 10 <<abCdefghij>>
9:S: length 4 <<abcd>>
10:R: length 10 <<abcDefghij>>
11:S: length 5 <<abcde>>
12:R: length 10 <<abcdEfghij>>
13:S: length 6 <<abcdef>>
14:R: length 10 <<abcdeFghij>>
15:S: length 7 <<abcdefg>>
16:R: length 10 <<abcdefGhij>>
17:S: length 8 <<abcdefgh>>
18:R: length 10 <<abcdefgHij>>
19:S: length 9 <<abcdefghi>>
20:R: length 10 <<abcdefghIj>>
21:R: length 10 <<abcdefghiJ>>
22:L: length 11 <<abcdefghiJ>>
23:R: length 10 <<AbcdefghiJ>>
24:L: length 12 <<abcdefghiJ>>
25:R: length 10 <<aBcdefghiJ>>
26:L: length 13 <<abcdefghiJ>>
$
@gbulmer'fgets' does not NOT ** discard the newline。 – Dave 2012-03-31 16:29:37
@Dave - 感谢您发现。非常容易混淆:-(我猜我在考虑gets()(这更难以使用,因为它不需要缓冲区长度)我将删除以保存混淆 – gbulmer 2012-03-31 16:38:56
@JonathanLeffler'c == EOF && c!=' \ n''?第二部分永远不会失败 – Dave 2012-03-31 16:47:45