2016-12-08 44 views
1

所以我有我mkfifo()功能在終端做出的FIFO文件,我與發送數據:echo"12.5 123 5 -2.1" > rndfifoFIFO接收號碼(C代碼)

我的程序需要讀取這些數字,並把它們一個數組中以便我可以稍後使用它們。我目前只是成功地創建了一個程序來讀取這些數字並將它們放置在一個char數組中,但是我陷入了困境,並且不知道如何繼續以提供任何幫助?

代碼:
MSG_LEN

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)號
(如果有任何更好的,請寫出來)

另外我能做些什麼來阻止程序,如果一個點子e發送其他東西然後一個數字?

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

請研究'sscanf'或'strtok' – Chintan

+0

謝謝,我想strtok的幫助 – Nikola

回答

1

您已經閱讀後,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; 
} 

,最後,printArrayCHECKERR實現:

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); 
    } 
}