gpt4 book ai didi

c - 从文件中读取并存储在结构中

转载 作者:太空宇宙 更新时间:2023-11-04 03:05:24 25 4
gpt4 key购买 nike

我想复制这个数据@822!172.28.6.137!172.28.6.110!5000!6000|形成文件 input_data 到此结构,将 822 从文件复制到 input.key 并将 172.28.6.137 复制到 src_ip !遇到它应该将数据从文件复制到结构的下一个成员怎么办?

struct input_par
{
char key[5];
char src_ip[15];
char dst_ip[15];
char src_port[5];
char dst_port[5];
};

main()
{
int i;
char ch;
FILE *fp;
struct input_par input;
fp = fopen("input_data","r");
if(fp == NULL)
printf("file open failed \n");
else
{
ch = fgetc(fp);
if(ch=='@')
printf("data is valid\n");
fseek(fp,1,1);
while(ch!='|')
{
input.key =
input.src_ip =
input.dst_ip =
input.src_port =
input.dst_port =
}
}

最佳答案

您可以使用正则表达式,请参阅 libstd 中的 regexp.h

如果你只需要在这里使用这种东西,你可以通过你的 char[] 并计算 !并根据您之前看到的内容以及您在正确部分中阅读过的字符添加多少。

(fscanf 也更容易)

关于c - 从文件中读取并存储在结构中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5975991/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com