【新手求助】关于C读取txt文档存入数组的问题~

aabcg129 2017-11-18 01:17:56
小弟在做扫描WiFi的软件,现在手头有一张txt:内容如下~
IP At MAC Address Count Len MAC Vendor / Hostname
------------------------------------------------------------------------------------------
192.168.1.1 00:ec:ac:ce:b3:ac 1 60 Unknown vendor
192.168.1.42 70:e0:8c:68:0b:03 1 60 Unknown vendor
192.168.1.100 a8:60:b6:23:91:89 1 60 Apple, Inc.
192.168.1.101 88:d7:f6:e2:f2:01 1 60 ASUSTek COMPUTER INC.
192.168.1.104 88:d7:f6:e2:f2:07 1 60 ASUSTek COMPUTER INC.
192.168.1.125 50:bd:5f:0a:6a:93 1 60 TP-LINK TECHNOLOGIES CO.,LTD.
192.168.1.126 70:e0:5d:68:1e:c3 1 60 Unknown vendor
192.168.1.127 88:d7:f6:df:1c:e4 1 60 ASUSTek COMPUTER INC.
192.168.1.145 00:e0:4c:9f:93:c5 1 60 REALTEK SEMICONDUCTOR CORP.
192.168.1.182 10:7b:44:93:8a:dd 1 60 ASUSTek COMPUTER INC.
192.168.1.200 00:0c:29:0f:d4:1c 1 60 VMware, Inc.
192.168.1.201 00:0c:29:9d:04:93 1 60 VMware, Inc.
192.168.1.253 00:0c:29:4e:a0:12 1 60 VMware, Inc.
192.168.1.254 6c:0b:84:aa:3b:33 1 60 Universal Global Scientific Industrial Co., Ltd.
小弟为新手,已经被这个问题困扰好多天了,现在想把这个TXT用C读取,这个txt通过后台获取,每次刷新会覆盖原来的TXT,这个每一行存为一个数组,最后输出类似于下面这个~求代码程序~

a[0] |_____________________|
a[1] |_____________________|
a[2] |_____________________|
a[3] |_____________________|
a[4] |_____________________|
a[5] |_____________________|
a[6] |_____________________|
a[7] |_____________________|
a[8] |_____________________|
a[9] |_____________________|
现在我现学现卖,只会写读取txt文档的代码~不知道如何将每一行数据存为一个数组。。。
这个是读取txt的代码~
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fp;
if(NULL == (fp = fopen("host.txt", "r")))
{
printf("error\n");
exit(1);
}

char ch;
while(EOF != (ch=fgetc(fp)))
{
printf("%c", ch);
}

fclose(fp);

return 0;
}

...全文
408 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
hongwenjun 2017-11-20
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

typedef struct info {
    char ip[16];
    char mac[32];
    int cnt;
    int len;
    char mac_vendor[32];
} INFO_OBJ, *INFO_HANDLE;

int main()
{
    FILE* fp;
    if (NULL == (fp = fopen("host.txt", "r"))) {
        printf("error\n");
        exit(1);
    }

    char line[BUFSIZ];

    info wf;
    int cnt = 0;

    while (fgets(line, BUFSIZ, fp)) {

        char* pch = strtok(line, " ");
        if ((pch != NULL) && isdigit(pch[0])) {  // 检查是否是 ip (数字开头)
            strncpy(wf.ip, pch, 16 - 1);

            pch = strtok(NULL, " ");
            strncpy(wf.mac, pch, 32 - 1);

            pch = strtok(NULL, " ");
            wf.cnt =  atoi(pch);

            pch = strtok(NULL, " ");
            wf.len =  atoi(pch);

            pch = strtok(NULL, "\n"); // 删除最后的换行
            while (!isalpha(*pch++)) // 跳到第一个字母上面
                ;
            strncpy(wf.mac_vendor, pch, 32 - 1);

            printf("a[%d]|%s|%s|%d|%d|%s|\n",
                   cnt++, wf.ip, wf.mac,
                   wf.cnt, wf.len, wf.mac_vendor);
        }
    }

    fclose(fp);
    return 0;
}
这样应该可以了
aabcg129 2017-11-20
  • 打赏
  • 举报
回复
引用 15 楼 zjq9931 的回复:
来晚了。。。
不完呐~~~~~
aabcg129 2017-11-20
  • 打赏
  • 举报
回复
引用 14 楼 hongwenjun 的回复:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

typedef struct info {
    char ip[16];
    char mac[32];
    int cnt;
    int len;
    char mac_vendor[32];
} INFO_OBJ, *INFO_HANDLE;

int main()
{
    FILE* fp;
    if (NULL == (fp = fopen("host.txt", "r"))) {
        printf("error\n");
        exit(1);
    }

    char line[BUFSIZ];

    info wf;
    int cnt = 0;

    while (fgets(line, BUFSIZ, fp)) {

        char* pch = strtok(line, " ");
        if ((pch != NULL) && isdigit(pch[0])) {  // 检查是否是 ip (数字开头)
            strncpy(wf.ip, pch, 16);

            pch = strtok(NULL, " ");
            strncpy(wf.mac, pch, 32);

            pch = strtok(NULL, " ");
            wf.cnt =  atoi(pch);

            pch = strtok(NULL, " ");
            wf.len =  atoi(pch);

            pch = strtok(NULL, " ");  // 这里因为使用 " " 把最后一个隔断了,可以按其他的再改改
            strncpy(wf.mac_vendor, pch, 32);

            printf("a[%d]|%s|%s|%d|%d|%s|\n",
                   cnt++, wf.ip, wf.mac,
                   wf.cnt, wf.len, wf.mac_vendor);
        }
    }

    fclose(fp);
    return 0;
}

真的谢谢您!~~~
  • 打赏
  • 举报
回复
来晚了。。。
hongwenjun 2017-11-18
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

typedef struct info {
    char ip[16];
    char mac[32];
    int cnt;
    int len;
    char mac_vendor[32];
} INFO_OBJ, *INFO_HANDLE;

int main()
{
    FILE* fp;
    if (NULL == (fp = fopen("host.txt", "r"))) {
        printf("error\n");
        exit(1);
    }

    char line[BUFSIZ];

    info wf;
    int cnt = 0;

    while (fgets(line, BUFSIZ, fp)) {

        char* pch = strtok(line, " ");
        if ((pch != NULL) && isdigit(pch[0])) {  // 检查是否是 ip (数字开头)
            strncpy(wf.ip, pch, 16);

            pch = strtok(NULL, " ");
            strncpy(wf.mac, pch, 32);

            pch = strtok(NULL, " ");
            wf.cnt =  atoi(pch);

            pch = strtok(NULL, " ");
            wf.len =  atoi(pch);

            pch = strtok(NULL, " ");  // 这里因为使用 " " 把最后一个隔断了,可以按其他的再改改
            strncpy(wf.mac_vendor, pch, 32);

            printf("a[%d]|%s|%s|%d|%d|%s|\n",
                   cnt++, wf.ip, wf.mac,
                   wf.cnt, wf.len, wf.mac_vendor);
        }
    }

    fclose(fp);
    return 0;
}

aabcg129 2017-11-18
  • 打赏
  • 举报
回复
引用 12 楼 cfjtaishan 的回复:
[quote=引用 8 楼 aabcg129 的回复:] [quote=引用 7 楼 cfjtaishan 的回复:]
#include <stdio.h>
#include <stdlib.h>

typedef struct info{
    char ip[16];
    char mac[32];
    int cnt;
    int len;
    char mac_vendor[32];
}INFO_OBJ, *INFO_HANDLE;

#define MAX_SIZE    100

INFO_OBJ info_list[MAX_SIZE];

int main()
{
    FILE *fp;
    int i = 0, ret = -1;
    //if(!(fp = fopen("host.txt", "r"))) {
    if(!(fp = fopen("1.txt", "r"))) {
        printf("error\n");
        exit(1);
    }
    char title[200];
    fgets(title, 200, fp);
    printf("%s", title);
    fgets(title, 200, fp);
    printf("%s", title);
    ret = fscanf(fp, "%s %s %d %d %[^\n]\n",
                 info_list[i].ip,
                 info_list[i].mac,
                 &info_list[i].cnt,
                 &info_list[i].len,
                 info_list[i].mac_vendor);
    while(EOF != ret) {
        printf("%-15s\t%s\t%d\t%d\t%s\n",
                info_list[i].ip,
                info_list[i].mac,
                info_list[i].cnt,
                info_list[i].len,
                info_list[i].mac_vendor
              );
        i++;
        ret = fscanf(fp, "%s %s %d %d %[^\n]\n",
                 info_list[i].ip,
                 info_list[i].mac,
                 &info_list[i].cnt,
                 &info_list[i].len,
                 info_list[i].mac_vendor);
    }

    printf("total num = %d\n", i);
    fclose(fp);

    return 0;
}
参考一下吧,根据文件的格式选择对应的读写接口
谢谢!!!真的谢谢您~可是如何显示成 a[0] |_____________________| a[1] |_____________________| a[2] |_____________________| a[3] |_____________________| a[4] |_____________________| a[5] |_____________________| a[6] |_____________________| a[7] |_____________________| a[8] |_____________________| a[9] |_____________________| 这样的呢,有数组标识这样的~表示领导看了这个后以为还不是存为数组,只是又读了一遍txt~[/quote] 额,这个总不能把所有问题都做好,楼主交一下作业就完事吧。输出格式,你在printf中改一下即可。 自己吸收一下,在基础上改一改,不然自己怎么进步呢; 是不是用数组,不是看输出的,代码里会说明的。[/quote]好的~~~谢谢大大~~~!
自信男孩 2017-11-18
  • 打赏
  • 举报
回复
引用 8 楼 aabcg129 的回复:
[quote=引用 7 楼 cfjtaishan 的回复:]
#include <stdio.h>
#include <stdlib.h>

typedef struct info{
    char ip[16];
    char mac[32];
    int cnt;
    int len;
    char mac_vendor[32];
}INFO_OBJ, *INFO_HANDLE;

#define MAX_SIZE    100

INFO_OBJ info_list[MAX_SIZE];

int main()
{
    FILE *fp;
    int i = 0, ret = -1;
    //if(!(fp = fopen("host.txt", "r"))) {
    if(!(fp = fopen("1.txt", "r"))) {
        printf("error\n");
        exit(1);
    }
    char title[200];
    fgets(title, 200, fp);
    printf("%s", title);
    fgets(title, 200, fp);
    printf("%s", title);
    ret = fscanf(fp, "%s %s %d %d %[^\n]\n",
                 info_list[i].ip,
                 info_list[i].mac,
                 &info_list[i].cnt,
                 &info_list[i].len,
                 info_list[i].mac_vendor);
    while(EOF != ret) {
        printf("%-15s\t%s\t%d\t%d\t%s\n",
                info_list[i].ip,
                info_list[i].mac,
                info_list[i].cnt,
                info_list[i].len,
                info_list[i].mac_vendor
              );
        i++;
        ret = fscanf(fp, "%s %s %d %d %[^\n]\n",
                 info_list[i].ip,
                 info_list[i].mac,
                 &info_list[i].cnt,
                 &info_list[i].len,
                 info_list[i].mac_vendor);
    }

    printf("total num = %d\n", i);
    fclose(fp);

    return 0;
}
参考一下吧,根据文件的格式选择对应的读写接口
谢谢!!!真的谢谢您~可是如何显示成 a[0] |_____________________| a[1] |_____________________| a[2] |_____________________| a[3] |_____________________| a[4] |_____________________| a[5] |_____________________| a[6] |_____________________| a[7] |_____________________| a[8] |_____________________| a[9] |_____________________| 这样的呢,有数组标识这样的~表示领导看了这个后以为还不是存为数组,只是又读了一遍txt~[/quote] 额,这个总不能把所有问题都做好,楼主交一下作业就完事吧。输出格式,你在printf中改一下即可。 自己吸收一下,在基础上改一改,不然自己怎么进步呢; 是不是用数组,不是看输出的,代码里会说明的。
aabcg129 2017-11-18
  • 打赏
  • 举报
回复
引用 9 楼 hongwenjun 的回复:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
FILE* fp;
if (NULL == (fp = fopen("host.txt", "r"))) {
printf("error\n");
exit(1);
}

char line[BUFSIZ];
while (fgets(line, BUFSIZ, fp)) {

char* pch = strtok(line, " ");
while (pch != NULL) {
printf("%s\t", pch);
pch = strtok(NULL, " ");

}
}

fclose(fp);

return 0;
}


还要再改改
aabcg129 2017-11-18
  • 打赏
  • 举报
回复
引用 9 楼 hongwenjun 的回复:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
    FILE* fp;
    if (NULL == (fp = fopen("host.txt", "r"))) {
        printf("error\n");
        exit(1);
    }

    char line[BUFSIZ];
    while (fgets(line, BUFSIZ, fp)) {

        char*  pch = strtok(line, " ");
        while (pch != NULL) {
            printf("%s\t", pch);
            pch = strtok(NULL, " ");

        }
    }

    fclose(fp);

    return 0;
}

还要再改改
谢谢大佬~~~~很感谢呐!!!可是运行结果有点错行,而且没有显示数组标识~~~请问这个如何解决~~?
hongwenjun 2017-11-18
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
    FILE* fp;
    if (NULL == (fp = fopen("host.txt", "r"))) {
        printf("error\n");
        exit(1);
    }

    char line[BUFSIZ];
    while (fgets(line, BUFSIZ, fp)) {

        char*  pch = strtok(line, " ");
        while (pch != NULL) {
            printf("%s\t", pch);
            pch = strtok(NULL, " ");

        }
    }

    fclose(fp);

    return 0;
}

还要再改改
aabcg129 2017-11-18
  • 打赏
  • 举报
回复
引用 7 楼 cfjtaishan 的回复:
#include <stdio.h>
#include <stdlib.h>

typedef struct info{
    char ip[16];
    char mac[32];
    int cnt;
    int len;
    char mac_vendor[32];
}INFO_OBJ, *INFO_HANDLE;

#define MAX_SIZE    100

INFO_OBJ info_list[MAX_SIZE];

int main()
{
    FILE *fp;
    int i = 0, ret = -1;
    //if(!(fp = fopen("host.txt", "r"))) {
    if(!(fp = fopen("1.txt", "r"))) {
        printf("error\n");
        exit(1);
    }
    char title[200];
    fgets(title, 200, fp);
    printf("%s", title);
    fgets(title, 200, fp);
    printf("%s", title);
    ret = fscanf(fp, "%s %s %d %d %[^\n]\n",
                 info_list[i].ip,
                 info_list[i].mac,
                 &info_list[i].cnt,
                 &info_list[i].len,
                 info_list[i].mac_vendor);
    while(EOF != ret) {
        printf("%-15s\t%s\t%d\t%d\t%s\n",
                info_list[i].ip,
                info_list[i].mac,
                info_list[i].cnt,
                info_list[i].len,
                info_list[i].mac_vendor
              );
        i++;
        ret = fscanf(fp, "%s %s %d %d %[^\n]\n",
                 info_list[i].ip,
                 info_list[i].mac,
                 &info_list[i].cnt,
                 &info_list[i].len,
                 info_list[i].mac_vendor);
    }

    printf("total num = %d\n", i);
    fclose(fp);

    return 0;
}
参考一下吧,根据文件的格式选择对应的读写接口
谢谢!!!真的谢谢您~可是如何显示成 a[0] |_____________________| a[1] |_____________________| a[2] |_____________________| a[3] |_____________________| a[4] |_____________________| a[5] |_____________________| a[6] |_____________________| a[7] |_____________________| a[8] |_____________________| a[9] |_____________________| 这样的呢,有数组标识这样的~表示领导看了这个后以为还不是存为数组,只是又读了一遍txt~
自信男孩 2017-11-18
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <stdlib.h>

typedef struct info{
    char ip[16];
    char mac[32];
    int cnt;
    int len;
    char mac_vendor[32];
}INFO_OBJ, *INFO_HANDLE;

#define MAX_SIZE    100

INFO_OBJ info_list[MAX_SIZE];

int main()
{
    FILE *fp;
    int i = 0, ret = -1;
    //if(!(fp = fopen("host.txt", "r"))) {
    if(!(fp = fopen("1.txt", "r"))) {
        printf("error\n");
        exit(1);
    }
    char title[200];
    fgets(title, 200, fp);
    printf("%s", title);
    fgets(title, 200, fp);
    printf("%s", title);
    ret = fscanf(fp, "%s %s %d %d %[^\n]\n",
                 info_list[i].ip,
                 info_list[i].mac,
                 &info_list[i].cnt,
                 &info_list[i].len,
                 info_list[i].mac_vendor);
    while(EOF != ret) {
        printf("%-15s\t%s\t%d\t%d\t%s\n",
                info_list[i].ip,
                info_list[i].mac,
                info_list[i].cnt,
                info_list[i].len,
                info_list[i].mac_vendor
              );
        i++;
        ret = fscanf(fp, "%s %s %d %d %[^\n]\n",
                 info_list[i].ip,
                 info_list[i].mac,
                 &info_list[i].cnt,
                 &info_list[i].len,
                 info_list[i].mac_vendor);
    }

    printf("total num = %d\n", i);
    fclose(fp);

    return 0;
}
参考一下吧,根据文件的格式选择对应的读写接口
aabcg129 2017-11-18
  • 打赏
  • 举报
回复
引用 4 楼 yuzengyuan 的回复:
小手一抖,经验到手
0.0 大佬能帮忙解决一下么~~~~
天台的故事 2017-11-18
  • 打赏
  • 举报
回复
小手一抖,经验到手
aabcg129 2017-11-18
  • 打赏
  • 举报
回复
引用 2 楼 hongwenjun 的回复:
使用 fgets 每次读取一行处理
    while (fgets(line, BUFSIZ, input)) {
        read_rmc = read_gprmc(line, rmc);

        if (read_rmc) {
      。。。。。。。
        }
    }
首先谢谢大大~~~但是....可以有完整的代码吗~刚学C才三天,我串不起来~
hongwenjun 2017-11-18
  • 打赏
  • 举报
回复
使用 fgets 每次读取一行处理
    while (fgets(line, BUFSIZ, input)) {
        read_rmc = read_gprmc(line, rmc);

        if (read_rmc) {
      。。。。。。。
        }
    }
hongwenjun 2017-11-18
  • 打赏
  • 举报
回复
// 读取一行GPRMC数据,判断是否是GPRMC数据行
bool read_gprmc(const char* line, gprmc_format& rmc)
{

    char line_buf[BUFSIZ];
    strncpy(line_buf, line, BUFSIZ);

    int chksum = 0;

    if ('$' != line_buf[0]) {
        print_error(1);
        return false;;

    } else {

        char* pch = strrchr(line_buf, '*');
        if (pch != NULL) {
            *pch = '\0';
            rmc.mode = *(pch - 1);
            pch++;
            rmc.chksum = strtol(pch, &pch, 16);
            // printf("%X\n", chksum);

            if (rmc.chksum != checksum(line_buf + 1)) {
                print_error(2);
                return false;;
            }
        } else {
            print_error(3);
            return false;;
        }


        if (strstr(line_buf, ",,"))
            multi_replace(line_buf, ",,", ",|,");

        pch = strtok(line_buf, ",");
        if ((pch != NULL) && !strcmp(pch, "$GPRMC")) {
            // printf("%s\n", pch);    //GPRMC

            pch = strtok(NULL, ",");
            rmc.rcv_time = atof(pch);

            pch = strtok(NULL, ",");
            rmc.status = *pch;

            pch = strtok(NULL, ",");
            rmc.lat = atof(pch);

            pch = strtok(NULL, ",");
            rmc.lat_direct  = *pch;

            pch = strtok(NULL, ",");
            rmc.lon = atof(pch);

            pch = strtok(NULL, ",");
            rmc.lon_direct = *pch;

            pch = strtok(NULL, ",");
            rmc.speed  = atof(pch);

            pch = strtok(NULL, ",");
            rmc.cog  = atof(pch);

            pch = strtok(NULL, ",");
            rmc.date  = atoi(pch);

            // 一般空
            rmc.mag_variation   = 0;
            rmc.mag_var_direct  = 'W';

            rmc.mode = rmc.mode; // 之前已经读到

        } else {
            print_error(4);
            return false;;

        }

    }

    return true;
}

69,369

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧