简体   繁体   English

在C fgets中,continue跳过多于一行

[英]In C fgets and continue are skipping more than one line

I am trying to skip all lines starting with a "#" my current code seems to be working except when it hits the fourth comment, it then skips the first line of coordinates which I need. 我试图跳过以“#”开头的所有行,但当前代码似乎正在工作,除非它遇到第四个注释,然后才跳过我需要的第一行坐标。 The way I have proved this is with two pictures, one with an extra enter added after the comment and the program provides the correct output, and the second with the correct input but incorrect output.(I am required to use the input with no extra line). 我已经证明这是两张图片,其中一张在注释后添加了额外的输入,程序提供了正确的输出,第二张输入了正确的输入但输出不正确(我需要使用输入而没有额外的输入)线)。 I have also attached the section of code where I am encountering the issue. 我还在遇到问题的地方附加了代码部分。 Thanks so much for your help! 非常感谢你的帮助!

在此处输入图片说明

correct output, incorrect added new line to txt file 正确的输出,不正确的将新行添加到txt文件

在此处输入图片说明

code: 码:

a = 0 ;                                                                                     
while(fgets(line, sizeof(line),fp) != NULL)                                                 
{
    if (line[0] == '#')
    {
    continue;
    }
    else if (MAX_X==0  && MAX_Y==0 )
    {
        if ((sscanf(line,"%d %d", &MAX_X, &MAX_Y)==2) && MAX_X>0 && MAX_X <= 1000 && MAX_Y>0 && MAX_Y<=1000){
            continue;
        }
    }else if (NUM_PT==0)
    {
        if ((sscanf(line, "%d", &NUM_PT)==1)&& NUM_PT>0 && NUM_PT<=1000)
            continue;
    }
    else if((fscanf(fp,"%d %d", &X_COORD[a], &Y_COORD[a]))&& X_COORD[a]>=0 && X_COORD[a]<=MAX_X && Y_COORD[a]>=0 && Y_COORD[a]<= MAX_Y)
    {
       a++;
        continue;

    }
    else if(strcmp(line, "")==0)
    {

       return -1;

    }
}

You have an error in line 您在行中有错误

else if((fscanf(fp,"%d %d", &X_COORD[a], &Y_COORD[a]))&& X_COORD[a]>=0 && X_COORD[a]<=MAX_X && Y_COORD[a]>=0 && Y_COORD[a]<= MAX_Y)
{
   a++;
    continue;

}

You just scanned LINE parameter, and then scan a new line instead of scan parameters from this line. 您刚刚扫描了LINE参数,然后扫描了新行,而不是扫描该行中的参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM