免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 5382 | 回复: 17
打印 上一主题 下一主题

[C] 为什么fgets读不出数据 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-01-22 20:06 |只看该作者 |倒序浏览
这是我的函数:
int main(void)
{
        int i = 0;
        char pbuf[256] = "1970-01-01 02:24:51 [Error] WEB ALARM: test_str:[Log utility testing...], test_val:[12, 0xc], abcd[123], bbbl[234]";
        FILE* fp = NULL;
        int length = 0;
        int num = 0;
        char buf[256] = {0};
        FILE* fp_tmp = NULL;
       
    fp = fopen("/***/size.txt", "wr+"); //这个就是文件路径;
    fp_tmp = fp;       
        while(i < 14)
        {
                        i++;
                                               
                        if(fp == NULL)
                        {
                                printf("open file error\r\n");
                                return;
                        }
                       
                        fgets(buf, 300, fp_tmp);
            printf("in %s[%d]:buf:%s\n", __FUNCTION__, __LINE__, buf);
                       
                        fprintf(fp, "%s\r\n", pbuf);                                       
        }       
        fclose(fp);         
}

运行结果:
**********:~$ ./for_test
in main[28]:buf:
in main[28]:buf:
in main[28]:buf:
in main[28]:buf:
in main[28]:buf:
in main[28]:buf:
in main[28]:buf:
in main[28]:buf:
in main[28]:buf:
in main[28]:buf:
in main[28]:buf:
in main[28]:buf:
in main[28]:buf:
in main[28]:buf:


这是执行后size.txt的文件内容:
1970-01-01 02:24:51 [Error] WEB ALARM: test_str:[Log utility testing...], test_val:[12, 0xc], abcd[123], bbbl[234]
1970-01-01 02:24:51 [Error] WEB ALARM: test_str:[Log utility testing...], test_val:[12, 0xc], abcd[123], bbbl[234]
1970-01-01 02:24:51 [Error] WEB ALARM: test_str:[Log utility testing...], test_val:[12, 0xc], abcd[123], bbbl[234]
1970-01-01 02:24:51 [Error] WEB ALARM: test_str:[Log utility testing...], test_val:[12, 0xc], abcd[123], bbbl[234]
1970-01-01 02:24:51 [Error] WEB ALARM: test_str:[Log utility testing...], test_val:[12, 0xc], abcd[123], bbbl[234]
1970-01-01 02:24:51 [Error] WEB ALARM: test_str:[Log utility testing...], test_val:[12, 0xc], abcd[123], bbbl[234]
1970-01-01 02:24:51 [Error] WEB ALARM: test_str:[Log utility testing...], test_val:[12, 0xc], abcd[123], bbbl[234]
1970-01-01 02:24:51 [Error] WEB ALARM: test_str:[Log utility testing...], test_val:[12, 0xc], abcd[123], bbbl[234]
1970-01-01 02:24:51 [Error] WEB ALARM: test_str:[Log utility testing...], test_val:[12, 0xc], abcd[123], bbbl[234]
1970-01-01 02:24:51 [Error] WEB ALARM: test_str:[Log utility testing...], test_val:[12, 0xc], abcd[123], bbbl[234]
1970-01-01 02:24:51 [Error] WEB ALARM: test_str:[Log utility testing...], test_val:[12, 0xc], abcd[123], bbbl[234]
1970-01-01 02:24:51 [Error] WEB ALARM: test_str:[Log utility testing...], test_val:[12, 0xc], abcd[123], bbbl[234]
1970-01-01 02:24:51 [Error] WEB ALARM: test_str:[Log utility testing...], test_val:[12, 0xc], abcd[123], bbbl[234]
1970-01-01 02:24:51 [Error] WEB ALARM: test_str:[Log utility testing...], test_val:[12, 0xc], abcd[123], bbbl[234]


很纳闷为什么fgets()读不出文件的内容;
请指教:

论坛徽章:
0
2 [报告]
发表于 2015-01-22 20:23 |只看该作者
有人回答吗,自己顶一下

论坛徽章:
0
3 [报告]
发表于 2015-01-22 20:32 |只看该作者
是因为不能再写文件的时候又去读文件吗

论坛徽章:
0
4 [报告]
发表于 2015-01-22 20:49 |只看该作者
我猜测可能已经将文件上锁的原因有关,得通过多搞几个临时文件来解决了

论坛徽章:
0
5 [报告]
发表于 2015-01-22 21:03 |只看该作者
本帖最后由 xuanwoxingxi 于 2015-01-22 21:03 编辑

reading和writting的交替的时候,需要fseek或者fflush一下!         


MSDN:    When the "r+", "w+", or "a+" access type is specified, both reading and writing are allowed (the file is said to be open for "update"). However, when you switch from reading to writing, the input operation must encounter an EOF marker. If there is no EOF, you must use an intervening call to a file positioning function. The file positioning functions are [fsetpos], [fseek], and [rewind]. When you switch from writing to reading, you must use an intervening call to either [fflush] or to a file positioning function.

论坛徽章:
324
射手座
日期:2013-08-23 12:04:38射手座
日期:2013-08-23 16:18:12未羊
日期:2013-08-30 14:33:15水瓶座
日期:2013-09-02 16:44:31摩羯座
日期:2013-09-25 09:33:52双子座
日期:2013-09-26 12:21:10金牛座
日期:2013-10-14 09:08:49申猴
日期:2013-10-16 13:09:43子鼠
日期:2013-10-17 23:23:19射手座
日期:2013-10-18 13:00:27金牛座
日期:2013-10-18 15:47:57午马
日期:2013-10-18 21:43:38
6 [报告]
发表于 2015-01-22 21:05 |只看该作者
读、写分别各自fopen

论坛徽章:
0
7 [报告]
发表于 2015-01-22 21:22 |只看该作者
又看了下你代码,感觉其实没那么复杂
首先,你用的是wr+的方式,当你fopen的时候,你的txt一定会被先清空内容。

你的读取是在写入的前面
你写入以后文件流指针往后偏移,所以你什么都没读到。

你试试fprintf后面加上, fseek(fp,(long)sizeof(buf),SEEK_CUR);

还有,感觉你的fp和fp_tmp,在使用上是没有区别的,不知道你为何要多弄一个fp_tmp


论坛徽章:
0
8 [报告]
发表于 2015-01-23 10:22 |只看该作者
原来wr打开文件的时候文件就会被清空了,也对,因为w方式写文件的时候是从头开始写,不是追加写的;
我其实是要实现一个程序一直持续的往一个文件中写数据,所以这个文件一直处于打开不关闭(因为打开方式是w重新打开会将文件清空)的情况下,然后我是在每次写入数据之前要判断一下文件的大小,不能让一直持续的写,持续写的话会让文件无休止的变大,所以当文件到一定大小的时候,将文件的比如前500行删除,之前是在文件w打开方式的时候去读文件中的数据的,所以一直读不出数据,兄台真是一语点破了呀,我还一直搞了那么久没弄明白,多谢多谢;
我现在的实现方式是这样的,请问还有什么高效的方式吗?
#include <stdlib.h>
#include <stdio.h>
#include <string.h>


#define MAX_TMPLOG_LINE 10
#define LOG_MEM_FILE "/home/shaqianqing/size1.txt"
#define FALSE 0
#define TRUE 1
FILE* fp_head = NULL;

int judge_log_add(void)
{
    char buf[300] = {0};
    int line_num = 0;
    FILE* fp = NULL;
    //printf("in %s[%d]:fp:%d\n\n\n", __FUNCTION__, __LINE__, fp);
   
    fp = fopen(LOG_MEM_FILE, "r");
        if(fp == NULL)
        {
                printf("open file error\r\n");
                return;
        }
       
        /*printf("long=%ld\n", ftell(fp));
        if(ftell(fp) <= 0)
        {
                printf("return\n");
                return TRUE;
        }*/
   
    fgets(buf, 300, fp);
    printf("in %s[%d]:buf:%s\n", __FUNCTION__, __LINE__, buf);
   
    while(!feof(fp))
    {
            printf("in %s[%d]:\n", __FUNCTION__, __LINE__);
        line_num++;
        fgets(buf, 300, fp);
    }
    fclose(fp);
    fp = NULL;

    printf("in %s[%d]:line_num:%d\n", __FUNCTION__, __LINE__, line_num);
    if(line_num>MAX_TMPLOG_LINE)
    {
        return FALSE;
    }
    else
    {
        return TRUE;
    }
}

void log_remove(void)
{
    FILE *fp_tmp = NULL;
    char file_name[32] = "/home/shaqianqing/size.tmp";
    char buf[500] = {0};
    int line_num = 0;
    FILE *fp = NULL;
   
     puts("in log_remove!");
     
    fp = fopen(LOG_MEM_FILE, "r");
        if(fp == NULL)
        {
                printf("open file error\r\n");
                return;
        }

    fp_tmp = fopen(file_name, "w");

    fgets(buf, 300, fp);
    while(!feof(fp))
    {
        line_num++;
        if(line_num > (MAX_TMPLOG_LINE/2))
        {
            fputs(buf,fp_tmp);
        }
        fgets(buf, 300, fp);
    }

    fclose(fp);
     fp = NULL;
    fclose(fp_tmp);
     fp_tmp = NULL;

    remove(LOG_MEM_FILE);
    rename(file_name,LOG_MEM_FILE);
}
int main(void)
{
        int i = 0;
        char pbuf[256] = "1970-01-01 02:24:51 [Error] WEB ALARM: test_str:[Log utility testing...], test_val:[12, 0xc], abcd[123], bbbl[234]";
        int length = 0;
        int num = 0;
        FILE* fp = NULL;
       
        fp = fopen(LOG_MEM_FILE, "w+");  /*第一这样打开是因为实际应用的时候会将文件关闭,然后重新打开后这里就会将文件自动清空了*/
        if(fp == NULL)
        {
                printf("open file error\r\n");
                return;
        }
       
        while(i < MAX_TMPLOG_LINE+14)
        {
                    printf("\n\n\n");
                        i++;
                        if(fp != NULL)
                        {
                                 fclose(fp);
                                  fp = NULL;
                        }
                       
                        if((judge_log_add() == FALSE))
                         {
                                     printf("false!\n");
                                       log_remove();
                         }
                       
                        printf("in %s[%d]:\n", __FUNCTION__, __LINE__);
                        if(fp == NULL)
                        {
                                printf("in %s[%d]:\n", __FUNCTION__, __LINE__);
                                fp = fopen("/home/shaqianqing/size1.txt", "a+");
                                if(fp == NULL)
                                {
                                        printf("open file error\r\n");
                                        return;
                                }
                        }
                       
                        printf("in %s[%d]:\n", __FUNCTION__, __LINE__);
                       
                        fprintf(fp, "%s\r\n", pbuf);
                       
                        printf("in %s[%d]:fp:%d\n", __FUNCTION__, __LINE__, fp);
                        fclose(fp);
                        fp = NULL;
        }
       
        if(fp == NULL)
        {
                fp = fopen("/home/shaqianqing/size1.txt", "a+"); /*这样是想追加的写到文件中*/
                if(fp == NULL)
                {
                        printf("open file error\r\n");
                        return;
                }
        }
       
        fflush(fp);
        length = ftell(fp);
        printf("the file size is %d \r\n", length);
        fclose(fp);
         fp = NULL;
       
}
回复 7# xuanwoxingxi


   

论坛徽章:
0
9 [报告]
发表于 2015-01-23 10:29 |只看该作者
您有什么高招吗?可以看下我上一个回复
hellioncu 发表于 2015-01-22 21:05
读、写分别各自fopen

论坛徽章:
324
射手座
日期:2013-08-23 12:04:38射手座
日期:2013-08-23 16:18:12未羊
日期:2013-08-30 14:33:15水瓶座
日期:2013-09-02 16:44:31摩羯座
日期:2013-09-25 09:33:52双子座
日期:2013-09-26 12:21:10金牛座
日期:2013-10-14 09:08:49申猴
日期:2013-10-16 13:09:43子鼠
日期:2013-10-17 23:23:19射手座
日期:2013-10-18 13:00:27金牛座
日期:2013-10-18 15:47:57午马
日期:2013-10-18 21:43:38
10 [报告]
发表于 2015-01-23 10:38 |只看该作者
shaqianqing 发表于 2015-01-23 10:29
您有什么高招吗?可以看下我上一个回复


你的需求有点怪,一般做法是日志写到一定量就新建一个文件,老的日志文件保留或者删除均可
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP