与fgets跳过空白行 [英] fgets skip the blank line

查看:2213
本文介绍了与fgets跳过空白行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写使用与fgets从一个文件中的每一行改为C程序。的问题是,如果该文件有一个空行,如何跳过它,以获得下一行?这是我有什么到目前为止尝试,但没有奏效。

 字符行[100];
FILE * filePtr = FOPEN(文件名,R);
    而(与fgets(行,sizeof的(线),filePtr)!= NULL)//读取文件的每一行
        {
            如果(行!=\\ n)
            {
                //做一点事
            }
            其他
            {
                继续;
            }
        }


解决方案

修改

 如果(行!=\\ n)

 如果(行[0]!='\\ n')

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.

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;
            }
        }

解决方案

Change

if (line != "\n")

into

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

这篇关于与fgets跳过空白行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆