您已經閱讀後,FIFO消息,並放置在字符數組的內容,採取字符數組,並從中解析數字,或者如果有別的東西,然後一個號碼停止程序。 對於從字符數組中讀取數字使用 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);
}
}
請研究'sscanf'或'strtok' – Chintan
謝謝,我想strtok的幫助 – Nikola