簡體   English   中英

將recvfrom()結果接收到的緩沖區與文本文件進行比較

[英]Comparing received buffer as a result of recvfrom() to a text file

由於recvfrom(),我收到了一個緩沖區。 可以說

char receiveBuffer [12] =“ NewClient 5”;

我想要一個文本文件,如下所示:

NewClient 1 192.168.1.1 8881
NewClient 2 192.168.1.1 8882
NewClient 3 192.168.1.1 8883
NewClient 4 192.168.1.1 8884
NewClient 5 192.168.1.1 8885
NewClient 6 192.168.1.1 8886
NewClient 7 192.168.1.1 8887
NewClient 8 192.168.1.1 8888
and so on..

假設receiveBuffer中有“ NewClient 5”。 現在我想要的是將我的receiveBuffer與文件內容進行比較。 當receiveBuffer與文件內容匹配時,例如,在這種情況下,第5行的起始內容與我的receiveBuffer中的內容相同,那么我想將相應的數字“ 192.168.1.1 8885”復制到sendBuffer中。

如何才能做到這一點? 如何只將包含文本文件空間的前11個字節與我的receiveBuffer比較? :(

我可以通過使用getc()逐個字符地讀取文件來做到這一點,但我不知道如何比較文件中固定數量的字節,如果比較結果為假,則跳轉到下一行,而忽略其中的所有其他剩余字節線?

歡迎任何幫助。 提前致謝 :)

char * filename; // needs to be defined somewhere
FILE* fp = fopen(filename, "r");

char line[1024]; // assuming 1024 is the longest a line can be
while (fgets(line, sizeof(line), fp)) {
    if (!memcmp(line, receiveBuffer, strlen(receiveBuffer))) {
        // found the line

        char * remainder_of_line = line + strlen(receiveBuffer);
        // do whatever you want with the rest of the line found at remainder_of_line


        // optionally if you are only interested in the first matching line
        break;
    }
}

fclose(fp);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM