簡體   English   中英

如何在C中創建路由表查找?

[英]How would I be able to create a routing table lookup in C?

分配要求您設計一個路由器表查找例程,該例程將從模擬幀(實際上是模擬幀中的數據包)獲取目標IP地址,然后執行路由表搜索(簡單地說是2 x 2數組)一列是已知IP地址,一列是模擬框中的端口,您可以在其中將綁定到該IP的數據包定向到該端口。

我已經為此工作了一段時間,但被困住了。 有人能幫我嗎! 我創建了一個文本文件,並嘗試將其插入項目中,但仍然無法檢測到它。 我不知道該怎么辦 提前致謝!!

#include <stdio.h>
#include "stdlib.h"
#include "string.h"

void Ouccpy_Routing_Table();

typedef struct RTE
{
unsigned long Dest;
int port;
unsigned long Route;
};



Route[120]; 
struct IP
{
unsigned char Ipverison;
unsigned char TOS;
unsigned short ID;
unsigned short Fragoffset;
unsigned char TTL;
unsigned char Protcl;
unsigned char dcheksum;
unsigned char Data[1];

};

int main()
{
int count;

FILE *ptr_testfile;

struct IP my_testfile;

ptr_testfile = fopen_s("c:\\testroute\\TEST.txt", "rb");

if (!ptr_testfile)
{
    printf("Cannot Open File!");

    return 1;
}

while (count = 2) count <= (sizeof(struct IP)); count++;
{

    fread(&my_testfile, sizeof(struct IP), 2, ptr_testfile);

}

fclose(ptr_testfile);

return 0;
}

void Ouccpy_Routing_Table()
{

}

將文本文件放在相應的位置。

  • C:\\ testroute \\ TEST.TXT

在編譯之前進行驗證。 如果問題仍然存在,請發布錯誤代碼?

關於代碼嘗試打開文件的方式

ptr_testfile = fopen_s("c:\\testroute\\TEST.txt", "rb");

編譯器至少應警告您未提供足夠的參數。

如果您已閱讀fread_s()文檔, fread_s()了解應該調用它:

errno_t en = fopen_s(&ptr_testfile, "c:\\testroute\\TEST.txt", "rb");
if (0 != en)
{
   issue some error message here
}

或者,您可以使用標准的fopen()而不是Microsoft特定的fopen_s() fopen()將像您的原始代碼一樣被調用。


這個

while (count = 2) count <= (sizeof(struct IP)); count++;
{
  fread(...);
}

是相同的

while (count = 2) 
  count <= (sizeof(struct IP)); 

count++;

{
  fread(...);
}

依次與

while (count = 2) /* This repeatly assigns 2 to count. Resulting in an infinite loop.*/
{
  count <= sizeof(struct IP); /* This does nothing. */
}

/* Because of the infinite loop above the code never arrives here. */

count++; 

fread(...);

請再看看。

暫無
暫無

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

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