1.固定格式文件读写
具体文件格式如下:
zhangleilei@compile_8:~/bin/test_C/head$ cat zllarp
IP address HW type Flags HW address Mask Device
192.168.0.108 0x1 0x2 14:5a:fc:1b:a9:27 * br-lan
192.168.0.101 0x1 0x2 68:a0:3e:10:62:70 * br-lan
192.168.0.100 0x1 0x2 32:80:94:79:e4:56 * br-lan
要求:读取ip和mac内容后,如下:
zhangleilei@compile_8:~/bin/test_C/head$ cat zll.txt
192.168.0.108 0x1 0x2 14:5a:fc:1b:a9:27 * br-lan
192.168.0.101 0x1 0x2 68:a0:3e:10:62:70 * br-lan
192.168.0.100 0x1 0x2 32:80:94:79:e4:56 * br-lan
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define ARP_P "./head/zllarp"
#define ZLL "./head/zll.txt"
//1.获取arp参数表
void test01()
{
FILE *fp = fopen(ARP_P, "r");
FILE *fm = fopen(ZLL, "w+");
char dest[81];
if(fm == NULL || fp == NULL) {
printf("File is Error!\n");
return -1;
}
fseek(fp, 79, SEEK_SET);
while(!feof(fp)){
memset(dest, 0, sizeof(dest));
fgets(dest, 81, fp);
//fwrite(dest, sizeof(char), sizeof(dest)*sizeof(char), fm);这个写入不加换行,读取的时候就有问题!!闭坑
fprintf(fm, "%s\n", dest);
}
fclose(fp);
fclose(fm);
}
int main()
{
test01();
return 0;
}
执行后结果如下:
zhangleilei@compile_8:~/bin/test_C/head$ ls
fgets.txt log.h zll zllarp zll.txt
zhangleilei@compile_8:~/bin/test_C/head$ cat zllarp
IP address HW type Flags HW address Mask Device
192.168.0.108 0x1 0x2 14:5a:fc:1b:a9:27 * br-lan
192.168.0.101 0x1 0x2 68:a0:3e:10:62:70 * br-lan
192.168.0.100 0x1 0x2 32:80:94:79:e4:56 * br-lan
zhangleilei@compile_8:~/bin/test_C/head$ cat zll.txt
192.168.0.108 0x1 0x2 14:5a:fc:1b:a9:27 * br-lan
192.168.0.101 0x1 0x2 68:a0:3e:10:62:70 * br-lan
192.168.0.100 0x1 0x2 32:80:94:79:e4:56 * br-lan
2.不固定格式文件读写
有文件dhcp.txt
现在要从其中提取出来ip地址
第一步:先读取文件成功,验证完毕
#define PTH "/home/zhangleilei/bin/test_C/pointer/dhcp.txt"
void fc()
{
FILE *fp;
char str;
fp = fopen(PTH, "r");
do{
str = fgetc(fp);
printf("%c", str);
}while(!feof(fp));
}
int main()
{
fc();
return 0;
}
zhangleilei@compile_8:~/bin/test_C/pointer$ cat dhcp.txt
105915 52:10:a1:4c:33:3f 192.168.0.101 * 01:52:10:a1:4c:33:3f
90126 00:0e:c6:5d:0a:8b 192.168.0.100 zhangleilei23051011 01:00:0e:c6:5d:0a:8b
zhangleilei@compile_8:~/bin/test_C/pointer$ ./edit
105915 52:10:a1:4c:33:3f 192.168.0.101 * 01:52:10:a1:4c:33:3f
90126 00:0e:c6:5d:0a:8b 192.168.0.100 zhangleilei23051011 01:00:0e:c6:5d:0a:8b
第二步:逐个读取,然后截取ip后存储到其他地方备用
#define PTH "/home/zhangleilei/bin/test_C/pointer/dhcp.txt"
void fc()
{
FILE *fp;
char str;
char getc[4];
char ss[14];
char *cp;
char *dp = "192.168.0.";
fp = fopen(PTH, "r");
do{
str = fgetc(fp);
if (str == '1') {
if (fgetc(fp) == '9') {
if (fgetc(fp) == '2') {
if (fgetc(fp) == '.') {
for(int i = 0; i < 6;i++) {
fgetc(fp);
}
for(int i = 0; i < 3;i++){
str = fgetc(fp);
getc[i] = str;
}
strcat(ss, dp);
getc[3] = '\0';
cp = getc;
printf("---zll_txt ----%s----\n", ss);
strcat(ss, cp);
printf("-----zll_cp-----%s---\n", cp);
printf("------zll_debug: %s ----------\n", ss);
goto EXIT;
}
}
}
}
EXIT:
str = fgetc(fp);
}while(!feof(fp));
}
int main()
{
fc();
return 0;
}
zhangleilei@compile_8:~/bin/test_C/pointer$ make
gcc -c func_000.c -lpthread
gcc -o edit func_000.o -lpthread
zhangleilei@compile_8:~/bin/test_C/pointer$ ./edit
---zll_txt ----192.168.0.----
-----zll_cp-----101---
------zll_debug: 192.168.0.101 ----------
成功提取出ip
下面提取第二个ip
如下:
#define PTH "/home/zhangleilei/bin/test_C/pointer/dhcp.txt"
void fc()
{
FILE *fp;
char str;
char getc[4];
char ss[512] = {0};
char *cp;
char *dp = "192.168.0.";
char *ptr = " ";
fp = fopen(PTH, "r");
do{
str = fgetc(fp);
if (str == '1') {
if (fgetc(fp) == '9') {
if (fgetc(fp) == '2') {
if (fgetc(fp) == '.') {
for(int i = 0; i < 6;i++) {
fgetc(fp);
}
for(int i = 0; i < 3;i++){
str = fgetc(fp);
getc[i] = str;
}
strcat(ss, dp);
getc[3] = '\0';
cp = getc;
strcat(ss, cp);
strcat(ss, ptr);
goto EXIT;
}
}
}
}
EXIT:
printf("------end------\n");
}while(!feof(fp));
printf("ss: %s \n", ss);
}
int main () {
//pthread_mutex_init(&lock, NULL);
//test_007();
//test_009(&ccu);
#if 0
operation();
printf("##########已下是锁操作############\n");
add_suo();
printf("----main-----global_num = %d--------\n", global_num);
#endif
#if (defined(DEBUG) && defined(__DD))
LOGSZ("[以下是system()验证]");
lin_00();
LOGSZ("[验证完毕]");
lin_01();
lin_02();
#endif
//zll_000();
//zll_001();
fc();
return 0;
}
结果如下:
zhangleilei@compile_8:~/bin/test_C/pointer$ ./edit
------end------
------end------
------end------
------end------
------end------
------end------
------end------
------end------
------end------
------end------
------end------
------end------
------end------
------end------
------end------
------end------
------end------
------end------
------end------
------end------
------end------
------end------
------end------
------end------
------end------
------end------
------end------
------end------
------end------
------end------
------end------
------end------
------end------
------end------
------end------
------end------
------end------
------end------
------end------
------end------
------end------
------end------
------end------
------end------
------end------
------end------
------end------
------end------
------end------
------end------
------end------
------end------
------end------
------end------
------end------
------end------
------end------
------end------
------end------
------end------
------end------
------end------
------end------
------end------
------end------
------end------
------end------
------end------
------end------
------end------
------end------
------end------
------end------
------end------
------end------
------end------
------end------
------end------
------end------
------end------
------end------
------end------
------end------
------end------
------end------
------end------
------end------
------end------
------end------
------end------
------end------
------end------
------end------
------end------
------end------
------end------
------end------
------end------
------end------
------end------
------end------
------end------
------end------
------end------
------end------
------end------
------end------
ss: 192.168.0.101 192.168.0.100