69,369
社区成员
发帖
与我相关
我的任务
分享
#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;
}
这样应该可以了#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;
}
#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;
}
还要再改改#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;
}
参考一下吧,根据文件的格式选择对应的读写接口 while (fgets(line, BUFSIZ, input)) {
read_rmc = read_gprmc(line, rmc);
if (read_rmc) {
。。。。。。。
}
}
// 读取一行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;
}