C语言 fgets 跳过空行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26052014/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 11:24:29  来源:igfitidea点击:

fgets skip the blank line

cfgets

提问by user4075830

I am writing a C program that use fgets to read in each line from a file. The problem is that if the file have a blank line, how to skip it to get the next line ? This is what I had try so far but it did not work.

我正在编写一个 C 程序,它使用 fgets 从文件中读取每一行。问题是,如果文件有一个空行,如何跳过它以获取下一行?这是我迄今为止尝试过的,但没有奏效。

char line[100];
FILE *filePtr = fopen(filename, "r");
    while(fgets(line, sizeof(line), filePtr) != NULL)       //read each line of the file
        {
            if (line != "\n")
            { 
                //do something
            }
            else
            {
                continue;
            }
        }

回答by Ashish_Deshpande

You can also use the strcmp function to check for newline

您还可以使用 strcmp 函数检查换行符

//Check for dos and unix EOL format
if(strcmp(line,"\n") || strcmp(line,"\r\n"))
{
   //do something 
}
else 
{
    continue;
}

Also, answering to your comment, fgets increments the file pointer after reading the line from the file. If you are running the code on a Linux system, try doing man fgetsfor more details.

此外,回答您的评论, fgets 在从文件中读取行后增加文件指针。如果您在 Linux 系统上运行代码,请尝试执行man fgets以获取更多详细信息。

回答by timrau

Change

改变

if (line != "\n")

into

进入

if (line[0] != '\n')

回答by J.M.I. MADISON

if(strcmp(line,"\n") || strcmp(line,"\r\n")){...} is wrong.

if(strcmp(line,"\n") || strcmp(line,"\r\n")){...} 是错误的。

strcmp returns non-zero if not equal to.

如果不等于,strcmp 返回非零值。

line=="\n"

行=="\n"

line=="\r\n"

行=="\r\n"

line=="A"

行==“A”

Would all evaluate to true for this logic.

对于这个逻辑,所有评估结果都为真。

It has the right idea in mindand was helpful though.

它有正确的想法,但很有帮助。

Here is a full working program re-write:

这是一个完整的工作程序重写:

//:for: uint32_t
#include <stdint.h> 

//:for: fopen, fgets, feof, fflush
#include <stdio.h>  

int main(){
    printf("[BEG:main]\n");fflush(stdout);

    size_t num_non_empty_lines_found = 0;
    FILE* file_pointer = NULL;
    const char* file_name = "RFYT.TXT";
    file_pointer = fopen( file_name, "r" );

    //: Init to null character because fgets
    //: will not change string if file is empty.
    //: Leading to reporting that an empty file
    //: contains exactly 1 non-blank line.
    //:
    //: Macro contains todays date, as a paranoid
    //: measure to ensure no collisions with
    //: other people's code.
    #define JOHN_MARKS_MAX_LINE_2019_03_03 256
    char single_line[ 
        JOHN_MARKS_MAX_LINE_2019_03_03 
    ] = "##代码##";
    int max_line = JOHN_MARKS_MAX_LINE_2019_03_03;
    #undef  JOHN_MARKS_MAX_LINE_2019_03_03

    //: This could happen if you accidentially
    //: spelled the filename wrong:
    if(NULL==file_pointer){
        printf("[ERROR:CheckFileNameSpelling]\n");
        return( 1 );
    };;

    //# DONT DO THIS! If you spelled the file  #//
    //# name wrong, this condition will lead   #//
    //# to an infinite loop.                   #//
    //- while( !feof(file_pointer )){ ... }    -//  
    while(fgets( 
    /**/single_line 
    ,   max_line
    ,   file_pointer 
    )){

        //: Check for empty lines:
        if( strcmp(single_line,"\n"  ) != 0 &&
            strcmp(single_line,"\r\n") != 0 &&
            strcmp(single_line,"##代码##"  ) != 0 &&
        1){
            printf("[LINE_HAS_CONTENT]\n");
            num_non_empty_lines_found++;
        }else{
            printf("[LINE_IS_EMPTY]\n");
            continue;
        };;

        //: Do stuff with non empty line:
        printf( "[Content]:%s\n", single_line );
    };;

    if(num_non_empty_lines_found<1){
        printf("[WARNING:FileWasEmpty]\n");
        printf("[EmptyFileName]:%s\n", file_name);
        fflush(stdout);
    };;

    printf("[END:main]\n");fflush(stdout);
    return( 0 );

};;