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

回复:
user: user-1
content: #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; }   这样应该可以了

user: user-0
content: 引用 15 楼 zjq9931 的回复:来晚了。。。不完呐~~~~~

user: user-0
content: 引用 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; }  真的谢谢您！~~~

user: user-2
content: 来晚了。。。

user: user-1
content: #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; }

user: user-0
content: 引用 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]好的~~~谢谢大大~~~！

user: user-3
content: 引用 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中改一下即可。 自己吸收一下，在基础上改一改，不然自己怎么进步呢； 是不是用数组，不是看输出的，代码里会说明的。

user: user-0
content: 引用 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; }    还要再改改

user: user-0
content: 引用 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; }    还要再改改谢谢大佬~~~~很感谢呐！！！可是运行结果有点错行，而且没有显示数组标识~~~请问这个如何解决~~？

user: user-1
content: #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; }    还要再改改

user: user-0
content: 引用 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~

user: user-3
content: #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; } 参考一下吧，根据文件的格式选择对应的读写接口

user: user-0
content: 引用 4 楼 yuzengyuan 的回复:小手一抖，经验到手0.0     大佬能帮忙解决一下么~~~~

user: user-4
content: 小手一抖，经验到手

user: user-0
content: 引用 2 楼 hongwenjun 的回复:使用 fgets 每次读取一行处理     while (fgets(line, BUFSIZ, input)) {         read_rmc = read_gprmc(line, rmc);          if (read_rmc) {       。。。。。。。         }     }首先谢谢大大~~~但是....可以有完整的代码吗~刚学C才三天，我串不起来~

user: user-1
content: 使用 fgets 每次读取一行处理     while (fgets(line, BUFSIZ, input)) {         read_rmc = read_gprmc(line, rmc);          if (read_rmc) {       。。。。。。。         }     }

user: user-1
content: // 读取一行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; }