473,386 Members | 1,804 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

How to read strings from a file with comments

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
Apr 9 '06 #1
15 5439
I. Myself wrote:
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?

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.
Apr 9 '06 #2
On Sun, 09 Apr 2006 21:04:40 GMT, in comp.lang.c , "I. Myself"
<No*****@Spam.none> wrote:
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.


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
Apr 9 '06 #3
"I. Myself" wrote:

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?


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/>
Apr 9 '06 #4
I. Myself wrote:
This is about reading the .ini files which are used in several of our
projects.
There are many pre-written utilities written in c which conform to the c
standards. Try a search on google, or if you prefer to use a home-rolled
version see below.

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 */
fscanf wouldn't be my first choice for this.

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.
This is rather trivial to accomplish. Read in one line of text (using
fgets perhaps). Pass a pointer to the buffer containing that line of
text to a new function you write (perhaps called remove_comments). Below
is an untested example. This assumes that your comments begin with the
'#' character and continue until EOL.

/**
* Remove comments denoted with a preceding '#' in a given string
*
* @param buffer Pointer to a string in which to remove comments from
* @return New length of string
*/
size_t remove_comments(char **buffer)
{
char *p;

if(strchr(p, '#') == NULL)
return(0); /* no comment found */

for(p = *buffer; p != '\0' && p != '#'; p++) ;

/* at this point, *p should be either '#' or '\0' */
*p = '\0';

return(strlen(p));
}
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.
Ignoring multiple lines of comments would be a little more tricky, but
it certainly could be done. I'll leave you with finding a solution for
this task. The only thing you will need to do now is check for an empty
string after calling the above function. Additionally you can also
implement some sort of a trim function which removes all leading and
trailing whitespace from a given string.

Can anyone tell me how to do it?

Thanks,

Mitchell Timin


You're welcome and I hope that points you in the right direction. Again,
note that the above code is untested (however it should work).

Joe
Apr 9 '06 #5
Ian Collins <ia******@hotmail.com> writes:
[...]
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.


I think you mean sscanf, not scanf.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Apr 10 '06 #6
Keith Thompson wrote:
Ian Collins <ia******@hotmail.com> writes:
[...]
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.

I think you mean sscanf, not scanf.

Well spotted.

--
Ian Collins.
Apr 10 '06 #7

Ο/Η Joe Estock *γραψε:
I. Myself wrote:
This is about reading the .ini files which are used in several of our
projects.


There are many pre-written utilities written in c which conform to the c
standards. Try a search on google, or if you prefer to use a home-rolled
version see below.

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 */


fscanf wouldn't be my first choice for this.

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.


This is rather trivial to accomplish. Read in one line of text (using
fgets perhaps). Pass a pointer to the buffer containing that line of
text to a new function you write (perhaps called remove_comments). Below
is an untested example. This assumes that your comments begin with the
'#' character and continue until EOL.

/**
* Remove comments denoted with a preceding '#' in a given string
*
* @param buffer Pointer to a string in which to remove comments from
* @return New length of string
*/
size_t remove_comments(char **buffer)
{
char *p;

if(strchr(p, '#') == NULL)
return(0); /* no comment found */

for(p = *buffer; p != '\0' && p != '#'; p++) ;

/* at this point, *p should be either '#' or '\0' */
*p = '\0';

return(strlen(p));
}


Maybe a better (and working) version would be:

size_t remove_comments(char *buffer)
{
char *p;

if(strchr(buffer, '#') == NULL)
return strlen(buffer); /* no comment found */

for(p = buffer; *p != '\0' && *p != '#'; p++) ;

/* at this point, *p should be either '#' or '\0' */
*p = '\0';

return strlen(p);
}

And one could even avoid the final strlen() call by calculating the
string length in the previous loop.

Apr 10 '06 #8
On Sun, 09 Apr 2006 23:25:10 GMT, Joe Estock wrote:
/**
* Remove comments denoted with a preceding '#' in a given string
*
* @param buffer Pointer to a string in which to remove comments from
* @return New length of string
*/
size_t remove_comments(char **buffer)
{
char *p;

if(strchr(p, '#') == NULL)
return(0); /* no comment found */

for(p = *buffer; p != '\0' && p != '#'; p++) ;

/* at this point, *p should be either '#' or '\0' */
*p = '\0';

return(strlen(p));
}


if *p==0 then strlen(p)==0

Apr 10 '06 #9
<st***********@hotmail.com> wrote in message
news:11**********************@g10g2000cwb.googlegr oups.com...

/ Joe Estock :
I. Myself wrote:
This is about reading the .ini files which are used in several of our
projects.


There are many pre-written utilities written in c which conform to the c
standards. Try a search on google, or if you prefer to use a home-rolled
version see below.

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 */


fscanf wouldn't be my first choice for this.

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.


This is rather trivial to accomplish. Read in one line of text (using
fgets perhaps). Pass a pointer to the buffer containing that line of
text to a new function you write (perhaps called remove_comments). Below
is an untested example. This assumes that your comments begin with the
'#' character and continue until EOL.

/**
* Remove comments denoted with a preceding '#' in a given string
*
* @param buffer Pointer to a string in which to remove comments from
* @return New length of string
*/
size_t remove_comments(char **buffer)
{
char *p;

if(strchr(p, '#') == NULL)
return(0); /* no comment found */

for(p = *buffer; p != '\0' && p != '#'; p++) ;

/* at this point, *p should be either '#' or '\0' */
*p = '\0';

return(strlen(p));
}


Maybe a better (and working) version would be:

size_t remove_comments(char *buffer)
{
char *p;

if(strchr(buffer, '#') == NULL)
return strlen(buffer); /* no comment found */

for(p = buffer; *p != '\0' && *p != '#'; p++) ;

/* at this point, *p should be either '#' or '\0' */
*p = '\0';

return strlen(buffer);
}
Apr 10 '06 #10
On 2006-04-10, stathis gotsis <st***********@hotmail.com> wrote:
<st***********@hotmail.com> wrote in message
news:11**********************@g10g2000cwb.googlegr oups.com...
/**
* Remove comments denoted with a preceding '#' in a given string
*
* @param buffer Pointer to a string in which to remove comments from
* @return New length of string
*/ [...]
Maybe a better (and working) version would be:

size_t remove_comments(char *buffer)
{
char *p;

if(strchr(buffer, '#') == NULL)
return strlen(buffer); /* no comment found */

for(p = buffer; *p != '\0' && *p != '#'; p++) ;

/* at this point, *p should be either '#' or '\0' */
*p = '\0';

return strlen(buffer);
}


Or instead of "return strlen(buffer)", just

return p - buffer;

which is a bit more efficient (strlen will scan all the way from start
to end to find the NUL).
Apr 10 '06 #11
On 2006-04-10, stathis gotsis <st***********@hotmail.com> wrote:
<st***********@hotmail.com> wrote in message
news:11**********************@g10g2000cwb.googlegr oups.com...
/**
* Remove comments denoted with a preceding '#' in a given string
*
* @param buffer Pointer to a string in which to remove comments from
* @return New length of string
*/ [..]
Maybe a better (and working) version would be:

size_t remove_comments(char *buffer)
{
char *p;

if(strchr(buffer, '#') == NULL)
return strlen(buffer); /* no comment found */

for(p = buffer; *p != '\0' && *p != '#'; p++) ;

/* at this point, *p should be either '#' or '\0' */
*p = '\0';

return strlen(buffer);
}


Well, or just do it like this:

size_t remove_comments(char *buffer)
{
char *p = strchr(buffer, '#');
if (p != NULL) *p = '\0';

return strlen(buffer);
}
Apr 10 '06 #12
"Ben C" <sp******@spam.eggs> wrote in message
news:sl*********************@bowser.marioworld...
On 2006-04-10, stathis gotsis <st***********@hotmail.com> wrote:
<st***********@hotmail.com> wrote in message
news:11**********************@g10g2000cwb.googlegr oups.com...
/**
* Remove comments denoted with a preceding '#' in a given string
*
* @param buffer Pointer to a string in which to remove comments from
* @return New length of string
*/ [..]

Maybe a better (and working) version would be:

size_t remove_comments(char *buffer)
{
char *p;

if(strchr(buffer, '#') == NULL)
return strlen(buffer); /* no comment found */

for(p = buffer; *p != '\0' && *p != '#'; p++) ;

/* at this point, *p should be either '#' or '\0' */
*p = '\0';

return strlen(buffer);
}


Well, or just do it like this:

size_t remove_comments(char *buffer)
{
char *p = strchr(buffer, '#');
if (p != NULL) *p = '\0';

return strlen(buffer);
}


Thanks for the suggestion, i got stuck on correcting the initial code, and
as a result i made one more mistake and missed the obvious. Too bad.
Apr 10 '06 #13
On Mon, 10 Apr 2006 20:57:06 +0300,
stathis gotsis <st***********@hotmail.com> wrote
in Msg. <e1***********@ulysses.noc.ntua.gr>
Maybe a better (and working) version would be:

size_t remove_comments(char *buffer)
{
char *p;

if(strchr(buffer, '#') == NULL)
return strlen(buffer); /* no comment found */

for(p = buffer; *p != '\0' && *p != '#'; p++) ;

/* at this point, *p should be either '#' or '\0' */
*p = '\0';

return strlen(buffer);
}


How about just:

strtok(buffer, "#");

robert
Apr 11 '06 #14
"Robert Latest" <bo*******@yahoo.com> wrote in message
news:4a************@individual.net...
On Mon, 10 Apr 2006 20:57:06 +0300,
stathis gotsis <st***********@hotmail.com> wrote
in Msg. <e1***********@ulysses.noc.ntua.gr>
Maybe a better (and working) version would be:

size_t remove_comments(char *buffer)
{
char *p;

if(strchr(buffer, '#') == NULL)
return strlen(buffer); /* no comment found */

for(p = buffer; *p != '\0' && *p != '#'; p++) ;

/* at this point, *p should be either '#' or '\0' */
*p = '\0';

return strlen(buffer);
}


How about just:

strtok(buffer, "#");


I tend to avoid strtok() in general, but it can work in simple cases like
this one.
Apr 11 '06 #15

I. Myself 写道:
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


Those suggestions are all good in simple situations. But what if there
are comment indicator such as "#" inside the "Key=Value" pair? For
example: Val1="ab#3". Then to simply search the "#" symbol and discard
the right part won't work. So I intend to put comment in a separate
line. This makes things simple.

Apr 12 '06 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Konan | last post by:
Pardon the simple question, but I have just begun to learn PHP. So far so good - all the examples in my books actually work. One thing that none of them address is how to read a file of strings...
7
by: Davy | last post by:
Hi all, A read-only data file is read in a C/C++ program. And now I use stdio function such as fopen() to fread() to operate the file. The content of the data file is constant. How to build...
15
by: ruca | last post by:
Hi, Can I read a .TXT File to a DataSet? How can I do that? I want to read his lines to a DropDownList. This lines are the names of employees that I export from an application that I have. I...
5
by: Zenek | last post by:
Hello, I have: - server MS SQL MSDE (2000) - database 'COLLBASE' - table 'MAIN' - row: column 'NAME' value 'version' and column 'VALUE' value '003' I make backup files by SQL query.
5
by: Denis Petronenko | last post by:
Hello, how can i read into strings from ifstream? file contains values in following format: value11; val ue12; value 13; valu e21;value22; value23; etc. i need to read like file >string,...
10
by: Arquitecto | last post by:
Hi , I have a question about a file operation i want to do . I have a data file lets say X bytes . I want to read the file and delete a byte every 2nd byte . I am a little comfused ,my approach...
6
by: portCo | last post by:
Hello there, I am creating a vb application which is some like like a questionare. Application read a text file which contains many questions and display one question and the input is needed...
3
by: slomo | last post by:
How to read strings cantaining escape character from a file and use it as escape sequences? for example, a file 'unicodes.txt' has contents: \u0050\u0079\u0074\u0068\u006f\u006e Now, ...
0
by: J-L | last post by:
Hi, I need to read a file like this: # Messages franais pour GNU gettext. # Copyright 2006 Yoyodyne, Inc. # Michel Robitaille <robitail@IRO.UMontreal.CA>, 2006. # Christophe Combelles...
3
by: Laura | last post by:
Hi: How can I read a file on the client side without loading it to the server?
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.