我对 C 很陌生,但我想构建一个程序,允许用户存储从首先存储到文本文件中的 traceroute/tracert 获得的 IP 地址。然后它允许他们打印下一个/上一个跃点。我使用了一个链表,但打印给了我一个分段错误。
我尝试浏览它,但我找不到任何错误,有人会指出我的错误并指导我吗?提前致谢!!
(抱歉压痕不好!!)
# include <stdio.h>
# include <string.h>
# include <stdlib.h>
int id = 0;
int list = 0;
int nodes = 0;
FILE *readFile(char *fileName)
{
FILE *rawdata = fopen(fileName, "r");
return rawdata;
}
void printMenu()
{
printf(" ========== MENU =============\n\n");
printf("1. Report ID of Previous Hops of a network node\n");
printf("2. Identify the next hops of a network node\n");
printf("3. Quit\n");
}
int getInput()
{
int choice;
printf("Please select your choice (1 to 3) =>: ");
scanf("%d", &choice);
return choice;
}
struct NodeInfo {
int ID;
int IP1;
int IP2;
int IP3;
int IP4;
struct NodeInfo *next;
struct NodeInfo *prev;
};
typedef struct NodeInfo Node;
Node *Topology;
Node *createNode(int ip1, int ip2, int ip3, int ip4)
{
Node *newNode = malloc(sizeof(Node));
newNode->IP1 = ip1;
newNode->IP2 = ip2;
newNode->IP3 = ip3;
newNode->IP4 = ip4;
newNode->next = 0; // NULL Pointer
newNode->prev = 0; // NULL Pointer
return newNode;
}
void addToBack(Node * tempnode)
{
Node *n = Topology;
Node *tail = 0;
while (n != NULL) {
tail = n;
n = n->next;
}
tail->next = tempnode;
tempnode->prev = tail;
}
void printFile(FILE * newFile)
{
char data[256], nth1[50], nth2[50], nth3[50], nth4[50], nth5[50],
nth6[50], nth7[50], ip[50], ip2[15], ip2new[14];
int linecount = -1, strlength;
int ip1, ip2x, ip3, ip4;
int ip11, ip21, ip31, ip41;
if (newFile == NULL) {
printf("There is an error with opening this file\n");
} else {
while (fgets(data, 256, newFile) != NULL) {
if (linecount != 3) {
linecount++;
continue;
} else {
if (linecount == 3 && data[2] != '\0') {
sscanf(data, "%s %s %s %s %s %s %s %s", nth1, nth2, nth3, nth4,
nth5, nth6, nth7, ip);
sscanf(data, "%s %s %s %s %s %s %s %d.%d.%d.%d", nth1, nth2,
nth3, nth4, nth5, nth6, nth7, &ip1, &ip2x, &ip3, &ip4);
if ((ip[0] <= 'z' && ip[0] >= 'a')
|| (ip[0] <= 'Z' && ip[0] >= 'A')) {
sscanf(data, "%s %s %s %s %s %s %s %s %s",
nth1, nth2, nth3, nth4, nth5, nth6, nth7, ip, ip2);
//Rescanning for anomaly results with additional hostname
strncpy(ip2new, ip2 + 1, strlen(ip2) - 2);
ip2new[strlen(ip2) - 2] = '\0';
int i;
char *temp;
char *ipcmp[4];
i = 0;
temp = strtok(ip2new, ".");
while (temp != NULL) {
ipcmp[i++] = temp;
temp = strtok(NULL, ".");
}
Node *tempnode = createNode(ip2new);
if (Topology != 0) {
addToBack(tempnode);
} else {
Topology = tempnode;
}
} else {
printf("%s\n", ip);
printf("%d.%d.%d.%d\n", ip1, ip2x, ip3, ip4);
Node *tempnode2 = createNode(ip);
if (Topology != 0) {
addToBack(tempnode2);
} else {
Topology = tempnode2;
}
continue;
}
}
if (linecount == 3 && data[2] == '\0') {
linecount = -2;
printf("\n");
}
}
}
}
}
void printNodes()
{
Node *n = Topology;
while (n != 0) {
printf("The node is %d.%d.%d.%d\n", n->IP1, n->IP2, n->IP3, n->IP4);
n = n->next; // Jump to next node
}
}
int main(int argc, char *argv[])
{
int option, fail;
FILE *filedata;
char *file;
file = argv[1];
filedata = readFile(file); //open file
printFile(filedata); //prints the ip addresses
do {
printMenu();
option = getInput();
switch (option) {
case 1:
printf("You have selected 1\n\n");
fail = 0;
printNodes();
break;
case 2:
printf("You have selected 2\n\n");
fail = 0;
break;
case 3:
fail = 1;
break;
default:
printf("Please enter a valid choice (1-3) \n");
fail = 0;
break;
}
} while (fail != 1);
while (Topology != 0) {
free(Topology);
Topology = Topology->next;
}
}