| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 733 人关注过本帖
标题:使用fgets按行读取文件,为何读完一行,下行读不出来了
只看楼主 加入收藏
xuyejin
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2023-6-7
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:9 
使用fgets按行读取文件,为何读完一行,下行读不出来了
各位大侠好,
 我使用fgets函数按行读取文件,为何读取一行后,第二行读取不出来了?还请各位大侠指教下
文件如下:
fd_door_cfg = fopen("./doorconfig.txt","r+");
    printf("file open!\n");
   
    i = 0;
    fc = ftell(fd_door_cfg);
    printf("%d\n",fc);
    fgets(u8Buff,100,fd_door_cfg);
    fc = ftell(fd_door_cfg);
    printf("%d LINE = %s\n",fc,u8Buff);
    fgets(u8Buff1,100,fd_door_cfg);
    fc = ftell(fd_door_cfg);
    printf("%d LINE = %s\n",fc,u8Buff1);
运行结果,
file open!
0
26 LINE = door open mode = password
42 LINE =
第二行应该是 off delay = 15 为何读取不出来了?
搜索更多相关主题的帖子: fgets 读取 LINE 文件 一行 
2023-06-07 14:00
xuyejin
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2023-6-7
收藏
得分:0 
char u8Buff[100],u8Buff1[100] 定义
2023-06-07 14:02
xuyejin
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2023-6-7
收藏
得分:0 
文件内容是:

$ cat doorconfig.txt
door open mode = password
Off delay = 15


2023-06-07 14:04
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9025
专家分:54030
注 册:2011-1-18
收藏
得分:0 
你贴个完整代码出来别人才可以调试,比如

程序代码:
#include <stdio.h>

int main( void )
{
    FILE* fd_door_cfg = fopen( "./doorconfig.txt", "r+" );
    if( fd_door_cfg )
    {
        printf("file open!\n");
   
        long fc = ftell( fd_door_cfg );
        printf( "%ld\n", fc );

        char u8Buff[100];
        fgets( u8Buff, 100, fd_door_cfg );
        fc = ftell( fd_door_cfg );
        printf("%ld LINE = %s\n", fc, u8Buff );

        char u8Buff1[100];
        fgets( u8Buff1, 100, fd_door_cfg );
        fc = ftell( fd_door_cfg );
        printf( "%d LINE = %s\n", fc, u8Buff1 );

        fclose( fd_door_cfg );
    }
}
2023-06-07 16:05
xuyejin
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2023-6-7
收藏
得分:0 
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

FILE *fd_door_cfg;
unsigned short door_open_mode,door_off_dly;

int main(int argc, char *argv[])
{
    int res,i;
    char u8Buff[100],u8Buff1[100],s,*ptr;
    long int fc;
   
    // ------------打开开门授权方式配置文件,如果不存在创建文件,并写入缺省密码开门授权
    printf("begin! \n");
    fd_door_cfg = fopen("./doorconfig.txt","r+");
    if(fd_door_cfg == NULL)
    {//如果打不开文件,那么创建文件,并写入缺省值 1 (密码)
        printf("doorconfig.txt not exist! \n");   
        fd_door_cfg = fopen("./doorconfig.txt","w+");
        if(fwrite("door open mode = password\n", sizeof("door open mode = password\n"),1, fd_door_cfg) != 1)
        printf("write doorconfig.txt error! \n");
        if(fwrite("Off delay = 15\n", sizeof("Off delay = 15\n"),1 ,fd_door_cfg) != 1)
        printf("write doorconfig.txt error! \n");
        printf("doorconfig.txt creat! \n");   
    }
    else
    printf("doorconfig.txt exist! \n");     

    fclose(fd_door_cfg);

   

    fd_door_cfg = fopen("./doorconfig.txt","r+");
    printf("file open!\n");
   
    i = 0;
    fc = ftell(fd_door_cfg);
    printf("%d\n",fc);
    fgets(u8Buff,100,fd_door_cfg);
    fc = ftell(fd_door_cfg);
    printf("%d LINE = %s\n",fc,u8Buff);
    fgets(u8Buff1,100,fd_door_cfg);
    fc = ftell(fd_door_cfg);
    printf("%d LINE = %s\n",fc,u8Buff1);
   
    //{
    //    printf("LINE = %s\n",u8Buff);
    //}
    //printf("read first line = %.s\n",u8Buff);
    //fgets(&u8Buff[0],100,fd_door_cfg);
    //printf("read second line = %s\n",u8Buff);
   
    if(strcmp(u8Buff,"door open mode = password") == 0)
    door_open_mode = 1;   
    else if(strcmp(u8Buff,"door open mode = card") == 0)
    door_open_mode = 0;   
    else if(strcmp(u8Buff,"door open mode = card | password") == 0)   
    door_open_mode = 2;        
    else if(strcmp(u8Buff,"door open mode = card & password") == 0)
    door_open_mode = 3;
    else
    door_open_mode = 1;        

    //fgets(&u8Buff[0],200,fd_door_cfg);
    //printf("read second line = %s\n",u8Buff);
   
    //door_off_dly = atoi(ptr);
    //printf("open mode = %x, off dly = %d\n",door_open_mode,door_off_dly);
   
   

    fclose(fd_door_cfg);
   
   
   
    return 0;
}

2023-06-07 17:48
xuyejin
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2023-6-7
收藏
得分:0 
运行结果如下:

begin!
doorconfig.txt exist!
file open!
0
26 LINE = door open mode = password

42 LINE =

第二行就没有输出
2023-06-07 17:50
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9025
专家分:54030
注 册:2011-1-18
收藏
得分:20 
if(fwrite("door open mode = password\n", sizeof("door open mode = password\n"),1, fd_door_cfg) != 1)
if(fwrite("Off delay = 15\n", sizeof("Off delay = 15\n"),1 ,fd_door_cfg) != 1)
改为
if(fwrite("door open mode = password\n", sizeof("door open mode = password\n")-1,1, fd_door_cfg) != 1)
if(fwrite("Off delay = 15\n", sizeof("Off delay = 15\n")-1,1 ,fd_door_cfg) != 1)

字符串 "abc\n" 一共包含 5 个字符,分别是 'a', 'b', 'c', '\n', '\0'
2023-06-07 20:46
xuyejin
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2023-6-7
收藏
得分:0 
谢谢版主,我修改了下,好了,谢谢啦!
2023-06-08 08:13
xuyejin
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2023-6-7
收藏
得分:0 
编程论坛对于我这种新手来说,又多了个老师啊,
2023-06-08 08:20
hunterhuang
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2023-7-14
收藏
得分:0 
可以学习有意思的案例
2023-08-06 23:01
快速回复:使用fgets按行读取文件,为何读完一行,下行读不出来了
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014386 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved