使用 atoi() 不打印所需的输出

using atoi() not printing desired output

我的文本文件是 IP_CONFIG.txt

"192.168.128.3" IP_CS 
"192.168.128.2" IP_HM 
"192.168.128.1" IP_OB 
"192.168.128.4" IP_AS 
"127.0.0.1" IP_RS 
"127.0.0.1" IP_RS_D 
"1901" PORT_CS 
"1901" PORT_HM
"1901" PORT_OB
"3567" PORT_AS
"4444" PORT_RS
"7777" PORT_RS_D

我的代码是

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

#define MAXWORDS 30

int main(){
    FILE *fp;
    int i=0, j;
    char *words=NULL,*word=NULL,c;
    char *allwords[MAXWORDS];

    if ((fp=fopen("IP_CONFIG.txt","r"))==NULL){ 
        printf("Error Opening File\n");
        exit(1);}
    while ((c = fgetc(fp))!= EOF){
        if (c=='\n'){ c = ' '; }
        words = (char *)realloc(words, ++i*sizeof(char));
        words[i-1]=c;}
    word=strtok(words," ");
    i=0;
    while(word!= NULL && i < MAXWORDS){
        //printf("%s\n",word);
        allwords[i] = malloc(strlen(word));
        strcpy(allwords[i], word);
        word = strtok(NULL," ");
        i++;        
    }
    printf("\nNow printing each saved string:\n");
    for (j=0; j<i; j++){
        printf("String %d: %s\n", j, allwords[j]);
        }
   char * IP_CS   = allwords[0];    
   char *IP_HM   = allwords[2];   
   char *IP_OB  = allwords[4];      
   char *IP_AS   = allwords[6];      
   char *IP_RS   = allwords[8];      
   char *IP_RS_D = allwords[10]; 

   int PORT_CS   = atoi(allwords[12]);
   int PORT_HM   = atoi(allwords[14]); 
   int PORT_OB   = atoi(allwords[16]);
   int PORT_AS   = atoi(allwords[18]);
   int PORT_RS   = atoi(allwords[20]);
   int PORT_RS_D = atoi(allwords[22]);




   printf("The IPs are \n %s\n %s\n %s\n %s\n %s\n %s\n",IP_CS,IP_HM,IP_OB,IP_AS,IP_RS,IP_RS_D);
   printf("The PORTSs are \n %d\n %d\n %d\n %d\n %d\n %d\n",PORT_CS,PORT_HM,PORT_OB,PORT_AS,PORT_RS,PORT_RS_D);    

    exit(0);
}

我已经解决了正确获取 IP 地址的问题,但我无法解决端口问题。因为在我的其他程序中它应该是一个整数值,所以我使用了 atoi。但为什么我的输出只是零..而不是 1901,3567 等..(下面是输出)

Now printing each saved string:
String 0: "192.168.128.3"
String 1: IP_CS
String 2: "192.168.128.2"
String 3: IP_HM
String 4: "192.168.128.1"
String 5: IP_OB
String 6: "192.168.128.4"
String 7: IP_AS
String 8: "127.0.0.1"
String 9: IP_RS
String 10: "127.0.0.1"
String 11: IP_RS_D
String 12: "1901"
String 13: PORT_CS
String 14: "1901"
String 15: PORT_HM
String 16: "1901"
String 17: PORT_OB
String 18: "3567"
String 19: PORT_AS
String 20: "4444"
String 21: PORT_RS
String 22: "7777"
String 23: PORT_RS_D
The IPs are 
 "192.168.128.3"
 "192.168.128.2"
 "192.168.128.1"
 "192.168.128.4"
 "127.0.0.1"
 "127.0.0.1"
The PORTSs are 
 0
 0
 0
 0
 0
 0

如何获得文本文件中描述的确切端口号。谢谢

  1. 您的字符串周围有 ",这导致转换失败。
  2. 您没有 '[=14=]' 终止 words 缓冲区,因此将其传递给 strtok() 会导致未定义的行为。

  3. 你的问题很严重

    allwords[i] = malloc(strlen(word));
    

    应该是

    allwords[i] = malloc(1 + strlen(word));
    

    因为您需要考虑终止 '[=14=]' 字节。

我已经修复了你的程序,就在这里

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

#define MAXWORDS 30

int main()
{
    FILE *fp;
    int   i = 0, j;
    char *words = NULL, *word = NULL, c;
    char *allwords[MAXWORDS] = {NULL};

    if ((fp=fopen("IP_CONFIG.txt","r"))==NULL)
    {
        printf("Error Opening File\n");
        return 1;
    }
    while ((c = fgetc(fp)) != EOF)
    {
        void *pointer;
        if (c=='\n')
            c = ' ';
        pointer = realloc(words, (i + 1) + 1);
        if (pointer == NULL)
        {
            free(words);
            return 2;
        }
        words    = pointer;
        words[i++] = c;
    }
    words[i] = '[=12=]';

    word = strtok(words, " ");
    i    = 0;
    while ((word != NULL) && (i < MAXWORDS))
    {
        const char  *pointer;
        size_t       length;

        pointer = word;
        while ((isspace(pointer[0]) != 0) || (pointer[0] == '"'))
            pointer++;
        length = strlen(pointer);
        while ((isspace(pointer[length - 1]) != 0) || (pointer[length - 1] == '"'))
            length--;
        allwords[i] = malloc(1 + length);
        if (allwords[i] != NULL)
        {
            allwords[i][length] = '[=12=]';
            memcpy(allwords[i], pointer, length);
        }
        word = strtok(NULL, " ");

        i++;
    }
    printf("\nNow printing each saved string:\n");
    for (j = 0 ; j < i ; j++)
        printf("String %d: %s\n", j, allwords[j]);

    enum Ids
    {
        CS, HM, OB, AS, RS, RS_D, EnumSize
    };
    enum Ids ids;
    char *names[] = {"CS", "HM", "OB", "AS", "RS", "RS_D"};

    for (ids = CS ; ((ids < i) && (ids < EnumSize)) ; ids++)
    {
        char       *endptr;
        const char *ip;
        int         port;

        ip = allwords[2 * ids];
        if (allwords[12 + 2 * ids] != NULL)
        {
            port = strtol(allwords[12 + 2 * ids], &endptr, 10);
            if (*endptr != '[=12=]')
                printf("error, invalid port value %s\n", allwords[12 + 2 * ids]);
        }
        else if (ip != NULL)
            printf("%-4s:\n\tip  : %s\n\tport: %d\n", names[ids], ip, port);
    }
    return 0;
}