【发布时间】:2019-10-23 14:10:47
【问题描述】:
以下是myprogram.c的摘录:
int main(int argc, char *argv[])
{
UserOptions opt;
DmtxEncode *enc;
unsigned char codeBuffer[BUFFER_SIZE];
int codeBufferSize;
opt = GetDefaultOptions();
/* Read input data into buffer */
codeBufferSize = ReadInputData(codeBuffer, &opt);
//do something with the input data
exit(0);
}
static size_t ReadInputData(unsigned char *codeBuffer, UserOptions *opt)
{
int fd;
ssize_t bytesRead;
size_t bytesReadTotal;
/* Open file or stdin for reading */
fd = (opt->inputPath == NULL) ? 0 : open(opt->inputPath, O_RDONLY);
/* Read input contents into buffer */
for(bytesReadTotal = 0;; bytesReadTotal += bytesRead) {
bytesRead = read(fd, codeBuffer + bytesReadTotal, BUFFER_SIZE);
if(bytesRead == 0)
break;
}
return bytesReadTotal;
}
这以echo 1234 | myprogram 的形式接受输入,程序生成输出并终止。
我想让这个程序连续执行并接受用户输入,其中两个单独的输入由换行符 (\n) 区分。例如,我想将1234 \n 5678 \n 6363 \n 作为输入。程序应该首先获取输入1234,为其生成输出,然后看到有换行符并将5678 视为第二个输入,生成输出等等。
我试过修改ReadInputData函数如下:
static size_t ReadInputData(unsigned char *codeBuffer, UserOptions *opt) {
int fd;
ssize_t bytesRead;
size_t bytesReadTotal;
size_t startOfBlock;
unsigned char tmpBuffer[BUFFER_SIZE];
/* Open file or stdin for reading */
fd = (opt->inputPath == NULL) ? 0 : open(opt->inputPath, O_RDONLY);
for (bytesReadTotal = 0;; bytesReadTotal += bytesRead) {
startOfBlock = tmpBuffer + bytesReadTotal;
bytesRead = read(fd, startOfBlock, BUFFER_SIZE);
for (int i = startOfBlock; i < startOfBlock + bytesRead; i++) {
if (tmpBuffer[i] != '\n')
codeBuffer[i] = tmpBuffer[i];
else
break;
}
}
return bytesReadTotal;
}
并将main 的工作原理封装在while(1) 循环中,但这只会产生单个输入的输出并终止程序。
有人可以帮我弄清楚我在这里做错了什么吗?
编辑:根据建议,我修改ReadInputData函数如下:
static size_t
ReadInputData(unsigned char *codeBuffer, UserOptions *opt) {
int fd;
ssize_t bytesRead;
size_t bytesReadTotal;
unsigned char* startOfBlock;
unsigned char tmpBuffer[BUFFER_SIZE];
/* Open file or stdin for reading */
fd = (opt->inputPath == NULL) ? 0 : open(opt->inputPath, O_RDONLY);
for (bytesReadTotal = 0;; bytesReadTotal += bytesRead) {
startOfBlock = tmpBuffer + bytesReadTotal;
bytesRead = read(fd, startOfBlock, DMTXWRITE_BUFFER_SIZE);
if (bytesRead == 0)
break;
if (bytesRead == -1)
FatalError(EX_IOERR, _("Message read error"));
else if (bytesReadTotal > DMTXWRITE_BUFFER_SIZE)
FatalError(EX_DATAERR, _("Message to be encoded is too large"));
for (int i = 0; i < bytesRead; i++) {
if (startOfBlock[i] != '\n')
codeBuffer[i] = startOfBlock[i];
else
break;
}
}
这解决了换行符的问题,但程序在接受一个输入后退出,而不是接受连续输入。 main 的内容在 while(1) 循环内。
编辑:
main 带有 while 循环。 main 中没有 exit(0)。
int main(int argc, char *argv[])
{
UserOptions opt;
DmtxEncode *enc;
unsigned char codeBuffer[BUFFER_SIZE];
int codeBufferSize;
opt = GetDefaultOptions();
while(1){
/* Read input data into buffer */
codeBufferSize = ReadInputData(codeBuffer, &opt);
//do something with the input data
}
}
【问题讨论】:
-
我不确定您是否可以将
\n作为输入的一部分阅读。通常它是一个输入终止符。你可以让你的程序循环,在每个动作之后读取输入。 -
@Paul 他们当然可以阅读 '\n';实际上,这是一个常见的错误来源。
-
@JL2210 没有
fp;-)。您必须检查read()的返回值是否为 0。 -
为什么不简单地用
fgets逐行读取输入? -
fgets如果二进制数据由\n分隔(您在问题中说),则可以很好地处理二进制数据。虽然用换行符分隔二进制数据有点奇怪和限制,因为数据本身将无法容纳\n字符。
标签: c