The following is an excerpt from 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;
}
This takes the input in the form of echo 1234 | myprogram and the program generates an output and terminates.
I would like to make this program execute continuously and accept user inputs where two separate inputs are differentiated by a newline (\n) character. Say for example I would like to give 1234 \n 5678 \n 6363 \n as the input. The program should first take the input 1234, generate an output for it, then see that there is a newline and treat 5678 as the second input, generate an output and so on.
I have tried modifying the ReadInputData function as follows:
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;
}
and enclosed the workings of the main within a while(1) loop but this just produces the output for a single input and terminates the program.
Can someone help me figure out what I am doing wrong here?
EDIT: According to the suggestions, I modified the ReadInputData function as follows:
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;
}
}
This solves the problem of newline characters but the program exits after taking one input instead of accepting continuous inputs. The contents of main are inside a while(1) loop.
EDIT:
main with while loop. No exit(0) present in main.
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
}
}
\nas part of the input. Normally it is an input terminator. You can let your program loop, reading input after each action.fp;-). You'll have to check the return value ofread()for 0.fgets?fgetsworks just fine with binary data if it's delimited by\n(which you say in your question). Though delimiting binary data with newlines is somewhat strange and limiting because the data itself won't be able to hold\ncharacters.