Numerical Recipes Forum  

Go Back   Numerical Recipes Forum > Obsolete Editions Forum > General Hints, Tips, and Tricks for Using NR

Reply
 
Thread Tools Display Modes
  #1  
Old 08-03-2007, 06:18 PM
kitcarzon kitcarzon is offline
Registered User
 
Join Date: Jul 2007
Posts: 4
How can I ignore a comment line from an input file?

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.
Reply With Quote
  #2  
Old 08-08-2007, 07:45 AM
kitcarzon kitcarzon is offline
Registered User
 
Join Date: Jul 2007
Posts: 4
Unhappy Ignoring comment lines from input file

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.
Reply With Quote
  #3  
Old 04-08-2008, 03:06 PM
Loevenmaul Loevenmaul is offline
Registered User
 
Join Date: Apr 2008
Posts: 1
Ignoring comments

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);
}
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 09:04 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.