0

我意识到这是一个非常简单的问题,我希望能够通过在互联网上搜索找到答案,但我做不到。我想做的就是读入一个文件,但忽略以散列开头的注释。所以

FILE *fp_in;
if((fp_in = open("file_name.txt","r")) ==NULL) printf("file not opened\n");
const int bsz = 100; char buf[bsz];
fgets(buf, bsz, fp_in);  // to skip ONE line
while((fgets(buf,bsz,fp_in))! ==NULL) {
  double x,y,x;
  sscanf(buf, "%lf %lf %lf\n", &x,&y,&z);
  /////allocate values////
}

所以跳过文件中的 1 行是可以的,但我有几行文件,前面有一个我需要跳过的哈希键。有人可以帮我解决这个问题,我似乎找不到答案。干杯

4

3 回答 3

1

添加if (buf[0] == '#') continue;到循环的开头。

于 2012-10-19T12:32:09.743 回答
0

如果注释标记是该行中的第一个字符,

while((fgets(buf,bsz,fp_in))! ==NULL) {
  if (buf[0] == '#') continue;
  double x,y,x;
  sscanf(buf, "%lf %lf %lf\n", &x,&y,&z);
  /////allocate values////
}

会忽略注释行。如果不是那么简单,就没有办法避免扫描该行以获取评论。

于 2012-10-19T12:32:37.987 回答
0
FILE *fp_in;
int chr;
if((fp_in = fopen("file_name.txt","r")) == NULL){
   printf("File not opened\n");
   exit(1);
}

while((chr = fgetc(fp_in) != EOF){
    if(chr == '#'){
       while((chr = fgetc(fp_in)) != '#' || (chr != EOF)); /*skip*/
    }
  //do the processing.
}
于 2012-10-19T12:33:33.080 回答