【新手求助】关于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;
}

...全文
542 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;
}
源码下载地址: https://pan.quark.cn/s/8d2c461c797c JavaWeb程序设计构成了掌握Web交互式应用程序开发的核心领域,对于初学者来说,精通这一技术具有决定性意义。在“JavaWeb程序设计(第三版)作业答案”中,我们可以预期获得针对该教材习题的一系列深入解析,从而协助学习者强化知识体系。 JavaWeb所包含的技术组件涵盖了Servlet、JSP(JavaServer Pages)、JDBC(Java Database Connectivity)以及各类框架如Spring MVC、Struts等。Servlet是Java平台提供的一种扩展服务器功能的接口,能够处理HTTP请求并生成相应的反馈。JSP则是一种用于构建动态网页的工具,它支持开发者将HTML代码与Java代码进行整合编写,从而简化了Web应用程序的开发流程。 作业答案通常会涉及以下几个核心内容: 1. **Servlet基础**:可能包含Servlet生命周期、init(), service(), destroy()方法的应用,以及如何在web.xml文件中设定Servlet的映射关系。 2. **JSP基础**:JSP的九大内置对象,如request、response、session、application等的使用,以及EL(Expression Language)和JSTL(JavaServer Pages Standard Tag Library)的实际操作。 3. **HTTP协议理解**:GET和POST请求方法的差异,请求头与响应头的应用,以及会话管理的概念阐释。 4. **JDBC数据库操作**:与数据库建立连接,执行SQL指令,处理查询结果集,以及...
源码链接: https://pan.quark.cn/s/a4b39357ea24 斐讯K2是一款广受用户青睐的无线路由器,其运行表现稳定且具备较高的可操作性,在DIY爱好者群体中拥有极高的声誉。本资料将系统性地阐述斐讯K2的固件刷机方法及其关联的技术要点。固件升级是路由器爱好者改善设备性能、扩展功能的一种普遍手段,经由替换出厂固件,能够达成更加个性化的网络配置、增强安全防护等目标。斐讯K2固件资源库涵盖了多种知名的非官方固件,诸如Tomato Pheonix 不死鸟、高恪、PandoraBox 潘多拉等,这些固件均具备独特的优势,能够适配不同用户的需求。 1. Tomato Pheonix 不死鸟:Tomato是一款立足于Linux的开源固件,以其精巧、高效而备受推崇。不死鸟版本是专门为华硕及斐讯路由器优化的分支,提供了卓越的QoS(服务质量)配置、详尽的图表监控以及便捷的固件升级途径。对于那些需要精准调控带宽和监测网络状态的用户而言,这是一个理想的选项。 2. 高恪:高恪固件是OpenWrt的定制化版本,着重于操作的便捷性和运行的可靠性,特别适合对路由器操作不甚熟悉的用户群体。它提供了一些实用的功能,例如内置的广告屏蔽、快速测速工具等,同时保留了OpenWrt的适应性。 3. PandoraBox 潘多拉:潘多拉盒是另一款基于OpenWrt的固件,它以丰富的插件库和强大的自定义潜力而闻名。用户能够依据个人需求安装各类插件,实现更多功能,如远程接入、DDNS(动态域名解析服务)等。 4. 官方固件的纯净版本与定制版本:官方固件通常更侧重于稳定性,纯净版意味着未预置额外的应用或服务,适合注重稳定性的用户。定制版则可能包含了制造商的特色功能或优...

70,038

社区成员

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

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