如何从带有注释的文件中读取字符串 [英] How to read strings from a file with comments

查看:81
本文介绍了如何从带有注释的文件中读取字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是关于读取我们的几个
项目中使用的.ini文件。


目前,实际读取每一行的文件是这个声明:


fscanf(文件,%s%s,名称,值); / *从.ini中读取两个字符串

文件* /


这样可行,但它不允许在右边的任何注释

字符串。

我想允许.ini文件在右边有长评论。

更好的是允许额外的评论行也。

计算机应该忽略评论,并且只读每行最初的两个

字符串。


谁能告诉我怎么做?


谢谢,


Mitchell Timin


-

我为 http://ANNEvolve.sourceforge.net 感到自豪。如果你想为ANNEvolve写软件,b $ b或文章,或做测试或研​​究,请告诉我。


人类可能知道我的电子邮件地址是:(但删除3位数字)

zenguy at shaw666 dot ca

This is about reading the .ini files which are used in several of our
projects.

Currently, the actual read of each line of the file is with this statement:

fscanf(file, "%s %s", name, value); /* reads two strings from .ini
file */

This works, but it does not allow any comments to the right of the two
strings.
I would like to allow the .ini file to have long comments on the right.
Even better would be to allow additional lines of commentary also. The
computer should ignore the commentary, and just read the initial two
strings on each line.

Can anyone tell me how to do it?

Thanks,

Mitchell Timin

--
I''m proud of http://ANNEvolve.sourceforge.net. If you want to write software,
or articles, or do testing or research for ANNEvolve, let me know.

Humans may know that my email address is: (but remove the 3 digit number)
zenguy at shaw666 dot ca

推荐答案

我。我自己写道:
这是关于读取我们的几个
项目中使用的.ini文件。

目前,实际读取每一行的文件是这样的声明:

fscanf(文件,"%s%s",名称,值); / *读取.ini
文件中的两个字符串* /

这样可行,但它不允许在两个
字符串右边有任何注释。
我我想允许.ini文件在右边有长篇评论。
更好的是允许额外的评论。
计算机应该忽略评论,只读每行最初的两个字符串。

谁能告诉我怎么做?
This is about reading the .ini files which are used in several of our
projects.

Currently, the actual read of each line of the file is with this statement:

fscanf(file, "%s %s", name, value); /* reads two strings from .ini
file */

This works, but it does not allow any comments to the right of the two
strings.
I would like to allow the .ini file to have long comments on the right.
Even better would be to allow additional lines of commentary also. The
computer should ignore the commentary, and just read the initial two
strings on each line.

Can anyone tell me how to do it?


阅读整行并检查第一个字符(假设您使用

类似''#''作为评论标记)。如果是评论,请忽略

该行,否则用你想要的线上的scanf替换你的fscanf

读。


-

Ian Collins。


Read the full line and check the first character (assuming you use
something like ''#'' for a comment marker). If it is a comment, ignore
the line, else replace your fscanf with scanf on the line you have just
read.

--
Ian Collins.


On Sun,2006年4月9日21:04:40 GMT,在comp.lang.c, "我。我自己

< No ***** @ Spam.none>写道:
On Sun, 09 Apr 2006 21:04:40 GMT, in comp.lang.c , "I. Myself"
<No*****@Spam.none> wrote:
目前,该文件的每一行的实际读取都是这样的声明:

fscanf(文件,%s%s,名称,价值); / *读取.ini
文件中的两个字符串* /

这样可行,但它不允许在两个
字符串右侧发表任何注释。
Currently, the actual read of each line of the file is with this statement:

fscanf(file, "%s %s", name, value); /* reads two strings from .ini
file */

This works, but it does not allow any comments to the right of the two
strings.




大概你打算用某种方式划分评论


SETTING = wibble #this bit is a comment


如果是这样,请使用fgets()读取该行,然后读取sscanf()和

strchr()。

Mark McIntyre

-

"调试是第一次编写代码的两倍。

因此,如果你尽可能巧妙地编写代码,那么你是,

的定义,不够聪明,不能调试它。

--Brian Kernighan



Presumably you plan to delimit the comment somehow

SETTING=wibble #this bit is a comment

if so, use fgets() to read the line, then read up on sscanf() and
strchr().
Mark McIntyre
--
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan


"一世。自己"写道:
"I. Myself" wrote:

这是关于读取我们的几个项目中使用的.ini文件。

目前,实际读取每个该文件的行是这个
声明:

fscanf(文件,%s%s,名称,值); / *从
.ini文件中读取两个字符串* /

这样可行,但它不允许在
两个字符串的右边有任何注释。我想允许.ini文件在右边有很长的评论。更好的是允许额外的评论行。计算机应该忽略
评论,只读每行的两个字符串。

谁能告诉我怎么做?

This is about reading the .ini files which are used in several of
our projects.

Currently, the actual read of each line of the file is with this
statement:

fscanf(file, "%s %s", name, value); /* reads two strings from
.ini file */

This works, but it does not allow any comments to the right of the
two strings. I would like to allow the .ini file to have long
comments on the right. Even better would be to allow additional
lines of commentary also. The computer should ignore the
commentary, and just read the initial two strings on each line.

Can anyone tell me how to do it?



你可以在我的id2id-20中找到一个方法,函数readidpairs。在

本质中它是如下:


do {

ch = getident(leftwd,BUFMAX,idp,NULL);

ch = getident(rightwd,BUFMAX,idp,NULL);

ch = flushln(idp);

/ *代码来处理id对* /

} while(EOF!= ch);


需要getident和flushln的代码(都存在)。您可以获得完整的包裹和来源:


< http://cbfalconer.home.att.net/download/id2id-20。 zip>


-

"如果你想通过groups.google.com发布后续内容,请不要使用

破损的回复链接在文章的底部。点击

" show options"在文章的顶部,然后点击

回复在文章标题的底部。 - Keith Thompson

更多详细信息:< http://cfaj.freeshell.org/google/>

另见< http://www.safalra .com / special / googlegroupsreply />



You can see a method in my id2id-20, function readidpairs. In
essence it is the following:

do {
ch = getident(leftwd, BUFMAX, idp, NULL);
ch = getident(rightwd, BUFMAX, idp, NULL);
ch = flushln(idp);
/* code to process the id pair */
} while (EOF != ch);

which needs the code for getident and flushln (both present). You
can get the complete package and source at:

<http://cbfalconer.home.att.net/download/id2id-20.zip>

--
"If you want to post a followup via groups.google.com, don''t use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>


这篇关于如何从带有注释的文件中读取字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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