使用Win32 API逐行读取文本文件

时间:2013-04-01 08:04:57

标签: c windows file winapi

我在程序中创建了一个文本文件,其中保存了连接到我的服务器的客户端的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 个答案:

答案 0 :(得分: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);
}

答案 1 :(得分:1)

我的建议:strtok削减每一行。第一部分是ip,第二部分是port。

fgets以文本形式读取您的文件。并且你误用fgets,在读取同一个文件时,不需要更改fgets的第三个参数,你可以循环调用它直到它返回NULL。每次你调用fgets,使用strtok剪切阅读线(详细使用strtok请google strtok)。

答案 2 :(得分: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

获得更多内容

答案 3 :(得分:1)

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

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

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

fp = fp++;