How can I ignore a comment line from an input file?


kitcarzon
08-03-2007, 06:18 PM
Hello,

How can I ignore a comment line [which follows a special symbol say, "#"] from an input file for example,

#Matrix size
3
#Number of rotations
10
#Matrix
1 1 1
1 1 1
1 1 1

Thank you in advance,
Kit Carzon.

kitcarzon
08-08-2007, 07:45 AM
Hello,

I am writing some programs in Fortran 77 for educational purposes. For some programs, the input files have a lot of data and I like to mark them with comments using special symbols for ex., "#"

Can any one suggest me a way to make the program ignore the comment line which is followed by the special symbol?

For example,

#Matrix size ---comment line-----
3
#Number of rotations ---comment line-----
10
#Matrix elements ---comment line-----
1 1 1
1 1 1
1 1 1

Thank you in advance,
Kit Carzon.

Loevenmaul
04-08-2008, 03:06 PM
Hello,

below is what I'm using with C. Just call the function once before the "while" or "for" loop or so, and then after every "fscanf()" etc. inside the loop, e.g.:

skip_cmt('#',fp);

The comment character specified by the first argument then may be placed virtually anywhere it makes sense (like TeX's %). Enjoy!

Cheers,

---LvS

#define EAT_WHITE_SPACE \
while ((c=getc(fp))=='\n'||c==' '|| \
c=='\t'||c=='\r'||c=='\v'||c=='\f') ;

void skip_cmt(int z, FILE *fp)
{
int c;
EAT_WHITE_SPACE
if (c==z) {
do {
while ((c=getc(fp))!='\n') ;
EAT_WHITE_SPACE
} while (c==z);
(void)ungetc(c,fp);
}
else
(void)ungetc(c,fp);
}