从文本文件中删除条目[NOT A HOMEWORK] [英] remove entry from text file [NOT A HOMEWORK]

查看:62
本文介绍了从文本文件中删除条目[NOT A HOMEWORK]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,全部!


我实现了简单的程序来消除具有

后续结构的文件中的条目(实际上它的配置文件是''命名''DNS包用于

那些关心和知道的人):


选项{

目录" / var / named" ;;

listen-on {192.168.11.22; 127.0.0.1; };

货运代理{168.126.63.1; };

pid-file" /var/run/named.pid" ;;

};


控制{

unix" / var / run / ndc" perm 0600 owner 0 group 0;

};


zone"。" {

类型提示;

文件" root.cache";

};


zone" localhost" {

类型主人;

文件" localhost";

};


zone" ; my.local" {

类型主文件;

文件" my.local" ;;

allow-update {127.0.0.1; 192.168.11.0/24; };

};

....


我只打扰区域条目和代码的目标只是删除它们。

这是我完成的源代码。我想听听合理的批评

和有用的建议:-)关于优化,错误等等。


#include< stdio.h>

#include< string.h>

#include< strings.h>


#define BUFSIZE 100


int main(int argc,char ** argv)

{

FILE * f; / *原创* /

FILE * fn; / * new * /

char buf [BUFSIZE] = {0};

注册char * idx;

char sidx [BUFSIZE];

int found = 0; / *设置是否找到区域* /

int i = 0;


if(argc!= 3){

fprintf (stderr,用法:%s< zonename>< configfile> \ n",* argv);

return(0);

}


printf("%s%s \ n",argv [1],argv [2]);


/ * open original * /

if((f = fopen(argv [2]," r"))== NULL)

返回-1;


if((fn = fopen(" named.conf.tmp"," w +"))== NULL)

返回-1;


while(!feof(f)){

fgets(buf,BUFSIZE,f);


/ *跳过领先空格* /

for(idx = buf; * idx!=''\ 0''&&(* idx ==''''|| * idx ==''\\ \\ t''); idx ++)

;


/ *寻找''区域''令牌* /

如果(strncasecmp(idx," zone",sizeof(" zone") - 1)!= 0){

if(!found){

fprintf (fn,buf);

fflus h(fn);

}

继续;

}


idx + = sizeof( " zone") - 1;


/ *跳过令牌内的空格* /

for(; * idx!=''\ 0''&& (* idx ==''''|| * idx ==''\ t''); idx ++)

;


/ *从逗号中提取令牌* /

if(* idx ++!=''"' ')

继续;


/ *将令牌放入缓冲区以比较* /

while(* idx!='' \0''&& * idx!=''"'')

sidx [i ++] = * idx ++;


sidx [我] =''\'''; i = 0;

if(strcasecmp(sidx,argv [1])!= 0){

found = 0;

fprintf( fn,buf);

fflush(fn);

}

else

found = 1;

}


fclose(f);

fclose(fn);


返回0;

}


提前谢谢。


最诚挚的问候,罗马马沙克。电子邮件: mr*@tusur.ru

Hello, All!

I implemented simple program to eliminate entry from the file having the
following structure (actually it''s config file of ''named'' DNS package for
those who care and know):

options {
directory "/var/named";
listen-on { 192.168.11.22; 127.0.0.1; };
forwarders { 168.126.63.1; };
pid-file "/var/run/named.pid";
};

controls {
unix "/var/run/ndc" perm 0600 owner 0 group 0;
};

zone "." {
type hint;
file "root.cache";
};

zone "localhost" {
type master;
file "localhost";
};

zone "my.local" {
type master;
file "my.local";
allow-update { 127.0.0.1; 192.168.11.0/24; };
};
....

I bother only about "zone" entries and the goal of code to remove them only.
Here is the source code I''m done with. I''d like to hear reasonable criticism
and useful advices :-) on optimization, bugs etc.

#include <stdio.h>
#include <string.h>
#include <strings.h>

#define BUFSIZE 100

int main(int argc, char **argv)
{
FILE *f; /* original */
FILE *fn; /* new */
char buf[BUFSIZE] = { 0 };
register char *idx;
char sidx[BUFSIZE];
int found = 0; /* set if zone found */
int i = 0;

if (argc != 3) {
fprintf(stderr, "Usage: %s <zonename> <configfile>\n", *argv);
return (0);
}

printf("%s %s\n", argv[1], argv[2]);

/* open original */
if ( (f = fopen(argv[2], "r")) == NULL )
return -1;

if ( (fn = fopen("named.conf.tmp", "w+")) == NULL )
return -1;

while ( !feof(f) ) {
fgets(buf, BUFSIZE, f);

/* skip leading spaces */
for (idx = buf; *idx != ''\0'' && (*idx == '' '' || *idx == ''\t''); idx++)
;

/* look for ''zone'' token */
if ( strncasecmp(idx, "zone ", sizeof("zone ") - 1) != 0 ) {
if (!found) {
fprintf(fn, buf);
fflush(fn);
}
continue;
}

idx += sizeof("zone ") - 1;

/* skip spaces inside of token */
for (; *idx != ''\0'' && (*idx == '' '' || *idx == ''\t''); idx++)
;

/* extract token from commas */
if (*idx++ != ''"'')
continue;

/* put token into buffer to compare */
while (*idx != ''\0'' && *idx != ''"'')
sidx[i++] = *idx++;

sidx[i] = ''\0''; i = 0;
if (strcasecmp(sidx, argv[1]) != 0) {
found = 0;
fprintf(fn, buf);
fflush(fn);
}
else
found = 1;
}

fclose(f);
fclose(fn);

return 0;
}

Thank you in advance.

With best regards, Roman Mashak. E-mail: mr*@tusur.ru

推荐答案



Roman Mashak写道:

Roman Mashak wrote:
大家好!

我实现了简单的程序,以消除具有以下结构的文件中的条目(实际上它是''命名''DNS包的配置文件,用于关心和了解的人):

options {
目录" / var / named" ;
听取{192.168.11.22; 127.0.0.1;转发器{168.126.63.1; };
pid-file" /var/run/named.pid" ;;
};

控制{
unix" / var / run / ndc" ; perm 0600 owner 0 group 0;
};

zone"。" {
类型提示;
文件" root.cache";
};

区域" localhost" {
类型主文件;
文件" localhost";
};

区域" my.local" {
输入master;
file" my.local";
allow-update {127.0.0.1; 192.168.11.0/24; }
};
...

我只打扰区域条目和代码的目标只是删除它们。
这是我完成的源代码。我想听听合理的批评和有用的建议:-)关于优化,错误等等。

#include< stdio.h>
#include< string .h>
#include< strings.h>

#define BUFSIZE 100
int main(int argc,char ** argv)
{
FILE * f; / *原创* /
FILE * fn; / * new * /
char buf [BUFSIZE] = {0};
注册char * idx;
char sidx [BUFSIZE];
int found = 0; / *设置是否找到区域* /
int i = 0;

if(argc!= 3){
fprintf(stderr,"用法:%s< zonename> ;< configfile> \ n",* argv);
返回(0);
}

printf("%s%s \ n",argv [1],argv [2]);

/ * open original * /
if((f = fopen(argv [2]," r"))== NULL)
返回-1;

if((fn = fopen(" named.conf.tmp"," w +"))== NULL)
返回 - 1;

while(!feof(f)){
fgets(buf,BUFSIZE,f);

/ *跳过前导空格* /
for(idx = buf; * idx!=''\'''&&(* idx ==''''|| * idx ==''\ t''); idx ++)
;

/ *寻找''区域''令牌* /
if(strncasecmp(idx," zone",sizeof(" zone") - 1) != 0){
if(!found){
fprintf(fn,buf);
fflush(fn);
}
继续;
}

idx + = sizeof(" zone") - 1;

/ *跳过令牌内的空格* /
for(; * idx!=''\ 0''&& (* idx ==''''|| * idx ==''\ t''); idx ++)
;

/ *从逗号中提取令牌* /
if(* idx ++!=''"'')
继续;

/ *将令牌放入缓冲区以比较* /
while(* idx!=''\'''&& * idx!=''"'')
sidx [i ++] = * idx ++;

sidx [i] =''\'''; i = 0;
if(strcasecmp(sidx,argv [1])!= 0){
found = 0;
fprintf(fn,buf);
fflush(fn );
}

发现= 1;
}

fclose(f);
fclose(fn);

返回0;
}

提前谢谢。

最诚挚的问候,罗马马沙克。电子邮件: mr*@tusur.ru




查找有关 ;&法QUOT; C源生成器(或者甚至是yacc,如果

你有更复杂的要求,比如这个)



Look for references about "lex" C source generator (or even "yacc" if
you have more complex requirements like this one)


Roman Mashak写道:
Roman Mashak wrote:
你好,全部!

我实现了简单的程序,以消除具有以下结构的文件中的条目(实际上它是''的配置文件''命名''DNS包用于
关心和知道的人):
< snip>
if((fn = fopen(" named.conf.tmp"," w +" ;))== NULL)
返回-1;
Hello, All!

I implemented simple program to eliminate entry from the file having the
following structure (actually it''s config file of ''named'' DNS package for
those who care and know):
<snip>
if ( (fn = fopen("named.conf.tmp", "w+")) == NULL )
return -1;




这里,由于打开文件写入当前目录,它

将限制用户可以运行程序的目录。如果一个

用户没有对当前目录的写权限,那么他将需要更改目录并运行你的程序。


或许您应该在

短段中解释您的程序中使用的算法。



Here, because of opening the file for writing in present directory, it
will restrict the directory from which a user can run your program. If a
user does n''t have write permission for the present directory, then he
will have to change the directory and run your program.

Probably you should have explained algorithm used in your program in
short paragraph.


Roman Mashak写道:
Roman Mashak wrote:
大家好!

我实现了简单的程序,以消除具有以下结构的文件中的条目(实际上它是''命名''的配置文件对于那些关心和知道的人的DNS包:

选项{
目录" / var / named" ;;
listen-on {192.168.11.22; 127.0.0.1;转发器{168.126.63.1; };
pid-file" /var/run/named.pid" ;;
};

控制{
unix" / var / run / ndc" ; perm 0600 owner 0 group 0;
};

zone"。" {
类型提示;
文件" root.cache";
};

区域" localhost" {
类型主文件;
文件" localhost";
};

区域" my.local" {
输入master;
file" my.local";
allow-update {127.0.0.1; 192.168.11.0/24; }
};
...

我只打扰区域条目和代码的目标只是删除它们。
这是我完成的源代码。我想听听合理的批评和有用的建议:-)关于优化,错误等等。

#include< stdio.h>
#include< string .h>
#include< strings.h>


strings.h?
#define BUFSIZE 100
int main(int argc,char ** argv)
{
FILE * f; / *原创* /
FILE * fn; / * new * /
char buf [BUFSIZE] = {0};
注册char * idx;
char sidx [BUFSIZE];
int found = 0; / *设置是否找到区域* /
int i = 0;

if(argc!= 3){
fprintf(stderr,"用法:%s< zonename> ;< configfile> \ n",* argv);
return(0);


也许返回EXIT_FAILURE表示错误?

}

printf("%s%s \ n", argv [1],argv [2]);

/ * open original * /
if((f = fopen(argv [2]," r"))== NULL )
返回-1;


再次,EXIT_FAILURE。

if((fn = fopen(" named.conf.tmp"," w +"))== NULL)


如何使用tmpnam()代替?

返回-1;

while(!feof(f)) {
fgets(buf,BUFSIZE,f);


请参阅 http:// c -faq.com/stdio/feof.html

/ *跳过前导空格* /
for(idx = buf; * idx!=''\'''' &&(* idx ==''''|| * idx ==''\ t''); idx ++)
;

/ *寻找''区域''token * /
if(strncasecmp(idx," zone",sizeof(" zone") - 1)!= 0){


strlen (zone),而不是sizeof()。

if(!found){
fprintf(fn,buf);
fflush(fn);


为什么你需要拨打fflush()?


}
继续;
}

idx + = sizeof(" zone") - 1;

/ *跳过令牌内的空格* /
for(; * idx!=''\ 0''&&(* idx ==''''|| * idx ==''\ t''); idx ++)
;


剥去空白区域的函数怎么样?

/ *从逗号中提取令牌* /
if(* idx ++ !=''"'')
继续;

/ *将令牌放入缓冲区以比较* /
while(* idx!=''\ 0'' && * idx!=''"'')
sidx [i ++] = * idx ++;

sidx [i] =''\'''; i = 0;
if(strcasecmp(sidx,argv [1])!= 0){
found = 0;
fprintf(fn,buf);
fflush(fn );
}

发现= 1;
}

fclose(f);
fclose(fn);

返回0;
}
Hello, All!

I implemented simple program to eliminate entry from the file having the
following structure (actually it''s config file of ''named'' DNS package for
those who care and know):

options {
directory "/var/named";
listen-on { 192.168.11.22; 127.0.0.1; };
forwarders { 168.126.63.1; };
pid-file "/var/run/named.pid";
};

controls {
unix "/var/run/ndc" perm 0600 owner 0 group 0;
};

zone "." {
type hint;
file "root.cache";
};

zone "localhost" {
type master;
file "localhost";
};

zone "my.local" {
type master;
file "my.local";
allow-update { 127.0.0.1; 192.168.11.0/24; };
};
...

I bother only about "zone" entries and the goal of code to remove them only.
Here is the source code I''m done with. I''d like to hear reasonable criticism
and useful advices :-) on optimization, bugs etc.

#include <stdio.h>
#include <string.h>
#include <strings.h>
strings.h?

#define BUFSIZE 100

int main(int argc, char **argv)
{
FILE *f; /* original */
FILE *fn; /* new */
char buf[BUFSIZE] = { 0 };
register char *idx;
char sidx[BUFSIZE];
int found = 0; /* set if zone found */
int i = 0;

if (argc != 3) {
fprintf(stderr, "Usage: %s <zonename> <configfile>\n", *argv);
return (0);
Maybe return EXIT_FAILURE to indicate an error?
}

printf("%s %s\n", argv[1], argv[2]);

/* open original */
if ( (f = fopen(argv[2], "r")) == NULL )
return -1;
Again, EXIT_FAILURE.

if ( (fn = fopen("named.conf.tmp", "w+")) == NULL )
How about using tmpnam() instead?
return -1;

while ( !feof(f) ) {
fgets(buf, BUFSIZE, f);
See http://c-faq.com/stdio/feof.html

/* skip leading spaces */
for (idx = buf; *idx != ''\0'' && (*idx == '' '' || *idx == ''\t''); idx++)
;

/* look for ''zone'' token */
if ( strncasecmp(idx, "zone ", sizeof("zone ") - 1) != 0 ) {
strlen("zone"), not sizeof().
if (!found) {
fprintf(fn, buf);
fflush(fn);
Why do you need to call fflush()?

}
continue;
}

idx += sizeof("zone ") - 1;

/* skip spaces inside of token */
for (; *idx != ''\0'' && (*idx == '' '' || *idx == ''\t''); idx++)
;
How about a function that strips the white space?


/* extract token from commas */
if (*idx++ != ''"'')
continue;

/* put token into buffer to compare */
while (*idx != ''\0'' && *idx != ''"'')
sidx[i++] = *idx++;

sidx[i] = ''\0''; i = 0;
if (strcasecmp(sidx, argv[1]) != 0) {
found = 0;
fprintf(fn, buf);
fflush(fn);
}
else
found = 1;
}

fclose(f);
fclose(fn);

return 0;
}




这是一个开始,HTH。


Boa



That''s a start, HTH.

Boa


这篇关于从文本文件中删除条目[NOT A HOMEWORK]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆