1

我在我的程序中创建了一个文本文件,其中保存了连接到我的服务器的客户端的 IP 地址和端口号。每 30 秒后,我想从文本文件中读取客户端的 IP 和端口号,向该客户端发送一些数据,等待回复,读取第二个客户端的 IP 和端口号,以此类推客户。

我做了什么:

fgets ( line, sizeof line, fp ); /* read a line */
{
    MessageBox(NULL,
               line,
               "First Line of File",
               MB_ICONINFORMATION);
}
fp=fp++; //move to the next line
fgets ( line, sizeof line, fp ); /* read a line */
{
    MessageBox(NULL,
               line,
               "Second Line of File",
               MB_ICONINFORMATION);
}

执行上述代码时,从文本文件中读取的第一行和第二行是相同的。

我的文本文件如下所示:

10.0.1.25
56732

10.0.1.25
56733

10.0.1.25
56733

10.0.1.25
56733
4

4 回答 4

2

你必须从你的代码中删除这一行

fp=fp++; //move to the next line

fgets()函数将读取文件的位置自动移动到下一行

读取和拆分文件的数据(IP + 端口),我建议您使用fscanf()而不是fgets()

例子:

char ip[16];
int port;

while (fscanf(fp, " %s %d", ip, &port) > 0) {
    printf("ip: %s, port: %d", ip, port);
}
于 2013-04-01T08:50:24.270 回答
1

我的建议:strtok剪掉每一行。第一部分是ip,第二部分是端口。

fgets以文本形式读取您的文件。而且你误用fgets了,在读取同一个文件的时候,不需要改变 的第三个参数fgets,可以循环调用,直到它返回NULL。每次调用时fgetsstrtok用来截断读取行(具体使用方法strtok请google strtok)。

于 2013-04-01T08:11:09.230 回答
1

您可以使用此代码从文件中获取行。现在您可以根据需要提前解析它。

#include <stdlib.h> /* exit, malloc, realloc, free */
#include <stdio.h>  /* fopen, fgetc, fputs, fwrite */

struct line_reader {
    /* All members are private. */
    FILE    *f;
    char    *buf;
    size_t   siz;
};

/*
 * Initializes a line reader _lr_ for the stream _f_.
 */
void
lr_init(struct line_reader *lr, FILE *f)
{
    lr->f = f;
    lr->buf = NULL;
    lr->siz = 0;
}

/*
 * Reads the next line. If successful, returns a pointer to the line,
 * and sets *len to the number of characters, at least 1. The result is
 * _not_ a C string; it has no terminating '\0'. The returned pointer
 * remains valid until the next call to next_line() or lr_free() with
 * the same _lr_.
 *
 * next_line() returns NULL at end of file, or if there is an error (on
 * the stream, or with memory allocation).
 */
char *
next_line(struct line_reader *lr, size_t *len)
{
    size_t newsiz;
    int c;
    char *newbuf;

    *len = 0;           /* Start with empty line. */
    for (;;) {
        c = fgetc(lr->f);   /* Read next character. */
        if (ferror(lr->f))
            return NULL;

        if (c == EOF) {
            /*
             * End of file is also end of last line,
        `    * unless this last line would be empty.
             */
            if (*len == 0)
                return NULL;
            else
                return lr->buf;
        } else {
            /* Append c to the buffer. */
            if (*len == lr->siz) {
                /* Need a bigger buffer! */
                newsiz = lr->siz + 4096;
                newbuf = realloc(lr->buf, newsiz);
                if (newbuf == NULL)
                    return NULL;
                lr->buf = newbuf;
                lr->siz = newsiz;
            }
            lr->buf[(*len)++] = c;

            /* '\n' is end of line. */
            if (c == '\n')
                return lr->buf;
        }
    }
}

/*
 * Frees internal memory used by _lr_.
 */
void
lr_free(struct line_reader *lr)
{
    free(lr->buf);
    lr->buf = NULL;
    lr->siz = 0;
}

/*
 * Read a file line by line.
 * http://rosettacode.org/wiki/Read_a_file_line_by_line
 */
int
main()
{
    struct line_reader lr;
    FILE *f;
    size_t len;
    char *line;

    f = fopen("foobar.txt", "r");
    if (f == NULL) {
        perror("foobar.txt");
        exit(1);
    }

    /*
     * This loop reads each line.
     * Remember that line is not a C string.
     * There is no terminating '\0'.
     */
    lr_init(&lr, f);
    while (line = next_line(&lr, &len)) {
        /*
         * Do something with line.
         */
        fputs("LINE: ", stdout);
        fwrite(line, len, 1, stdout);
    }
    if (!feof(f)) {
        perror("next_line");
        exit(1);
    }
    lr_free(&lr);

    return 0;
}

你可以在http://rosettacode.org/wiki/Read_a_file_line_by_line获得更多的东西

于 2013-04-01T08:48:45.330 回答
1

下面的 while 循环将执行读取部分

while (fgets(buffer, sizeof(buffer), fp))

我在你的代码中看到的一个严重错误是

fp = fp++;
于 2013-04-01T09:04:01.443 回答