C 中处理文本文件的异常行为
我正在尝试模拟 DNS 服务器的行为,我有一个名为 hosts.txt
的数据库,其中包含 machine_names/IP_addresses
,例如:
equipo_00/169.0.1.169
sala_oeste_01/200.1.2.200
sala_oeste_02/200.2.3.200
sala_oeste_03/200.3.4.200
MEMFIS_04/201.4.5.201
ICARO_05/200.5.6.200
equipo_06/169.6.7.169
sala_este_07/201.7.8.201
sala_este_08/201.8.9.201
CEC_09/189.9.10.189
这是我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main(int argc, char** argv)
{
char* machine, *ip_add;
FILE* db;
char* flag;
char tmp[256];
char par[256];
printf("> ");
scanf("%s", par);
db = fopen("hosts.txt", "r");
while(db != NULL && fgets(tmp, sizeof(tmp), db) != NULL)
{
if(strstr(tmp, par))
{
flag = "1"; // flag setting to 1 means find the line
machine = strtok(tmp, "/");//here's supposed to get the value of the machine
ip_add = strtok(NULL, "/");//here's supposed to get the value of the ip adress
}
}//while
if(strcmp(flag, "1") == 0) //
{
printf("here\n");
}
else
{
flag = "0"; //flag setting to 0
printf("there\n");
}
printf("flag= %s pc=%s server=%s\n", flag, machine, ip_add);
return 0;
}
我的代码从标准输入读取机器的值并指示在服务器上分配给该机器的 IP 地址。问题是该程序不是正常行为,我不知道如何将我的代码修改为预期输出,例如,
input: equipo_00
output: flag = 1 pc = equipo_00 server=169.0.1.169
抱歉,如果问题相当愚蠢,我是这种语言的新手..谢谢大家的帮助和请原谅我的英语
I'm trying to simulate the behavior of a DNS server, which I have a DB named hosts.txt
containing machine_names/IP_addresses
, for example:
equipo_00/169.0.1.169
sala_oeste_01/200.1.2.200
sala_oeste_02/200.2.3.200
sala_oeste_03/200.3.4.200
MEMFIS_04/201.4.5.201
ICARO_05/200.5.6.200
equipo_06/169.6.7.169
sala_este_07/201.7.8.201
sala_este_08/201.8.9.201
CEC_09/189.9.10.189
Here's my code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main(int argc, char** argv)
{
char* machine, *ip_add;
FILE* db;
char* flag;
char tmp[256];
char par[256];
printf("> ");
scanf("%s", par);
db = fopen("hosts.txt", "r");
while(db != NULL && fgets(tmp, sizeof(tmp), db) != NULL)
{
if(strstr(tmp, par))
{
flag = "1"; // flag setting to 1 means find the line
machine = strtok(tmp, "/");//here's supposed to get the value of the machine
ip_add = strtok(NULL, "/");//here's supposed to get the value of the ip adress
}
}//while
if(strcmp(flag, "1") == 0) //
{
printf("here\n");
}
else
{
flag = "0"; //flag setting to 0
printf("there\n");
}
printf("flag= %s pc=%s server=%s\n", flag, machine, ip_add);
return 0;
}
My code reads from standard input, the value of the machine and indicate the IP address that is assigned to that machine on the server. The problem is that the program is not normal behavior, I do not know how to modify my code to an expected output, eg
input: equipo_00
output: flag = 1 pc = equipo_00 server=169.0.1.169
Sorry if the question is rather silly, I'm new to this language .. thank you all for your help and excuse my English
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您要做的就是:
将文件中的字符串读入
tmp
。检查用户输入的字符串
par
是否存在于tmp
中。如果是,您继续在
/
上标记tmp
并使指针machine
指向第一块和ip_add
指向下一块。请注意,
machine
和ip_add
只是指针。它们指向tmp
的各种索引。因此,稍后当您继续循环时,您将新字符串再次读取到tmp
中,并覆盖它。这会导致问题,并且您的指针现在指向已更改的字符串。为了避免这种情况,只需在成功匹配后添加一个中断:
此外,您的最终
printf
:应该是您的
if
正文的一部分,以便仅在找到时才打印它们比赛。目前,即使未找到匹配项,您也会进行打印。在这种情况下,您将打印垃圾,因为您的指针
server
和ip_add
尚未初始化。This is what you do:
Read a string from the file into
tmp
.Check if user entered string
par
is present intmp
.If yes you go ahead and tokenize
tmp
on/
and make the pointermachine
point to the first piece andip_add
point to the next piece.Note that
machine
andip_add
are just pointers. And they point at various indices oftmp
. So later when you continue with the loop you read the new string again intotmp
, overwriting it. This causes the problem and your pointer are now pointing to a changed string.To avoid this just add a break after a successful match:
Also your final
printf
:should be part of your
if
body, so that you print them only if you've found the match.Currently you are printing even if a match is not found. In which case you'll be printing junk as your pointers
server
andip_add
have not been initialized.看来您的问题可能出在
flag
变量上。使用字符串作为标志值似乎很不寻常。试试这个:在上面的代码中,
flag
变量在函数开始时未初始化,这不会给出意外(且未定义)的结果。It looks like your problem might be with the
flag
variable. Using a string for a flag value seems very unusual. Try this:In your code above, the
flag
variable is uninitialised at the start of the function which won't may give unexpected (and undefined) results.下面是固定代码。有几个小错误,主要是缺少 init。想想如果找不到机器会发生什么。
主要的一点是,循环的每次迭代都使用相同的缓冲区,从而清除以前找到的服务器。我通过退出循环解决了这个问题。另一种方法是将找到的结果复制到另一个缓冲区中。
Below is the fixed code. There is several minor errors, mostly missing inits. Think what would happen if machine is not found.
The main one is that you use the same buffer for each iteration of the loop, thus clearing up the previously found server. I fixed that by exiting loop. Another method could be to copy the found result in another buffer.
你不能检查这个,因为如果标志没有初始化,它是未定义的行为
strcmp() 在两个参数中搜索 '\0' 字符
you can't check this because if flag wasn't initializied it's undefined behavior
strcmp() searchs for '\0' character in both arguments