所以我在终端中使用mkfifo()
功能创建了一个FIFO文件,我发送的数据为:echo"12.5 123 5 -2.1" > rndfifo
我的程序需要读取这些数字并将它们放在一个数组中,以便我以后可以使用它们。我目前只成功创建了一个程序来读取这些数字并将它们放在一个char数组中,但是我被卡住了,并且不知道如何继续这样的任何帮助?
代码:
在 16
int main(int argc, char **argv) {
if(2 != argc)
exit(EXIT_FAILURE);
int fd = open(argv[1], O_RDONLY);
if(-1 == fd);
exit(EXIT_FAILURE);
do {
char buf[MSG_LEN];
int bytesRead;
if(-1 == (bytesRead = read(fd, buf,MSG_LEN))){
perror("Reading from PIPE failed");
exit(exit_failure);
}
if (0 == bytesRead)
break;
printf("Read number: %d\n", atoi(buf));
} while (true);
close(fd);
return 0;
}
分离我写的数字的解决方案(感谢Chintan)
(如果还有更好的那些请写出来)
如果管道发送其他东西然后是数字,我该怎么办才能停止程序?
char *deo;
float tmp;
deo = strtok(buf," ");
while(deo != NULL){
sscanf(deo,"%f",&tmp);
//tmp one number from buf(sent from FIFO)
deo = strtok(NULL," ");
}
答案 0 :(得分:1)
读完FIFO消息后,将内容放入char数组中,取出char数组并从中解析数字,或者如果还有其他数字则停止程序。
用于从char数组中读取数字
double strtod(const char *nptr, char **endptr)
要查看如何查找char数组中是否存在某些无效字符,请阅读第一个示例,然后参阅第二个(长)示例中的NOTE。
尝试使用不同字符串的以下简短示例:
char buffer[128] = "12.5 123 5 -2.1 text text 0.0 -15";
char buffer[128] = "text";
char buffer[128] = "1 2 39 3.45 0 17.3 0 10e2 78.33";
char buffer[128] = "12.5 123 5 -2.1";
char *beginPtr = buffer;
char *endPtr = NULL;
double current = 0;
while(true)
{
// After strtod call, if parsing was successful,
// endPtr will point to the first character after
// parsed number.
// If no number is parsed, endPtr will not change.
current = strtod(beginPtr, &endPtr);
// If endPtr is unchanged from last iteration,
// strtod encountered some non-float character
// EOF for example
if(beginPtr == endPtr) {
break;
}
// Moving beginPtr
beginPtr = endPtr;
printf("%lf\n", current);
}
从FIFO消息中获取数字的平均值的更长示例。 如果你需要将这些数字放在数组中,这里是如何(如果你需要取平均值,你甚至不需要将它们放在数组中:))
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#define MAX_LEN (256)
// cond should be TRUE if there is no error
void CHECKERR(bool cond, char* msg);
void printArray(double *array, int n);
int main(int argc, char** argv)
{
// For errno 22, perror will print 'Invalid argument'
errno = 22;
CHECKERR(2 == argc, "Usage: ./2 pathToFifoFile\n");
errno = 0;
int fileDesc = 0;
char buffer[MAX_LEN];
char *beginPtr = NULL;
char *endPtr = NULL;
double *array = NULL;
double current = 0.0;
double sum = 0.0;
double avg = 0.0;
int count = 0;
fileDesc = open(argv[1], O_RDONLY);
CHECKERR(-1 != fileDesc, "Failed to open file.\n");
CHECKERR(-1 != read(fileDesc, buffer, sizeof buffer), "Failed to read file.\n");
beginPtr = buffer;
while(true)
{
// Move the endPtr if parsing was successful
current = strtod(beginPtr, &endPtr);
// NOTE: echo "12.5 123 5 -2.1" > /tmp/rndfifo
// will put '\n' at the end of the string.
// If you have regular string, change the *endPtr != '\0'
if(beginPtr == endPtr && *endPtr != '\n')
{
printf("Further characters after float: %s\n", endPtr);
errno = 22;
CHECKERR(false, "FIFO contains non-float characters");
}
if(beginPtr == endPtr) {
break;
}
// Moving beginPtr
beginPtr = endPtr;
sum += current;
count++;
}
// Print out average
avg = sum / count;
printf("Average: %.2lf\n", avg);
// At this point we know how much numbers are stored in buffer
array = (double*)malloc(count * sizeof(double));
CHECKERR(NULL != array, "malloc() failed.\n");
// Go trough buffer again to put numbers in array
beginPtr = buffer;
int i;
for(i = 0; i < count; i++)
{
current = strtod(beginPtr, &endPtr);
beginPtr = endPtr;
array[i] = current;
}
close(fileDesc);
printArray(array, count);
free(array);
return 0;
}
最后, printArray 和 CHECKERR 实施:
void printArray(double *array, int n)
{
int i;
for(i = 0; i < n; i++) {
printf("%.2lf ", array[i]);
}
putchar('\n');
}
void CHECKERR(bool cond, char* msg)
{
if(!cond)
{
perror(msg);
exit(EXIT_FAILURE);
}
}