fread fgets feof 读文件到buf里面

从dhcp.leases读文件,每次读一行,fgets,写到dst_dhcp_list指向的buf里面。

feof(fp)有两个返回值:如果遇到文件结束,函数feof(fp)的值为非零值,否则为0。

int conf_get_dhcp_list1_new(char* dst_dhcp_list)
{
    FILE *f=NULL;
    char line_buffer[256]={0};
    my_printf(LOG_MODE_LEVEL_1, "%s(%d) sizeof(line_buffer):%d\n",__FUNCTION__,__LINE__,sizeof(line_buffer));

    if ((f = fopen("/tmp/dhcp.leases", "r")) != NULL)
    {

        while (!feof(f)) // If the file ends, the value of the function feof(fp) is non-zero, otherwise 0.
        {
           fgets(line_buffer,sizeof(line_buffer),f);
           strcat(dst_dhcp_list, line_buffer);
           my_printf(LOG_MODE_LEVEL_1, "%s(%d) conf_get_dhcp_list1 accessed and dst_dhcp_list is:\n%s\n",__FUNCTION__,__LINE__,dst_dhcp_list);
        }

    }

    fclose(f);
    return 0;
}

    while(fgets(wifimac, sizeof(wifimac), fd) != NULL)
    {
        if(strncmp(uppermac,wifimac,17) == 0)
        {

            my_printf(1, "%s(%d) COMPARE SUCCESED wifiMAC is %s uppermac is %s \n",__FUNCTION__,__LINE__,wifimac,uppermac);
            return 1;
        }   
        my_printf(1, "%s(%d) wifiMAC is %s mac is %s \n",__FUNCTION__,__LINE__,wifimac,uppermac);

    }

#include <stdio.h>
#include <string.h>
#include <unistd.h> 

/*****************************************************************
*Function: get_system_output
*Description:call the system command,gets the return value of the command.
*Input:cmd
*Input:size
*Output:output
*****************************************************************/
static int get_system_output(char *cmd, char *output, int size)
{
    FILE *fp=NULL;  
    fp = popen(cmd, "r");   
    if (fp)
    {       
        if(fgets(output, size, fp) != NULL)
        {       
            if(output[strlen(output)-1] == '\n')            
                output[strlen(output)-1] = '\0';    
        }   
        pclose(fp); 
    }
    return 0;
}


int main(int argc, char **argv)
{

    char *p_file="./data.txt";  
    char *p_file_bck="./data.bck";  
    FILE *f=NULL;
    char str[1024]={0};
    char line_buffer[1024]={0};
    char *p_line_buffer = NULL;
    char *pcmd = NULL;
    char cmd[1024]={0};
    char file_info[64][1024]={'0'};
    char *p=NULL;
    int i=0,j=0;

    if (access(p_file,R_OK) !=0 )
    {
        printf(">>>>>>>>>>./data.txt ERROR! \n");
        return;
    }

    system("awk '{print($2)}' ./data.txt > ./data.bck");


    if ((f = fopen(p_file_bck, "r")) != NULL)
    {
        while (fgets(line_buffer,sizeof(line_buffer),f),!feof(f)) // If the file ends, the value of the function feof(fp) is non-zero, otherwise not file end 0.
        {
          // fgets(line_buffer,sizeof(line_buffer),f);  //fread(line_buffer, 1024, 1, f);

           memset(str, 0x0, sizeof(str));
           strcpy(str, line_buffer);

           if ( (str[strlen(str)-2] == '\n')  || (str[strlen(str)-2] == '\r') )
               str[strlen(str)-2]='\0'; 

           if ( (str[strlen(str)-1] == '\n') || (str[strlen(str)-1] == '\r') )
               str[strlen(str)-1]='\0'; 

           //copy to ~/lin/push_code/
           memset(&cmd, 0x0, sizeof(cmd));
           strcat(cmd, "cp ");
           strcat(cmd, str);
           strcat(cmd, "   ~/lin/push_code/");
           printf("###copy to:%s\n",cmd);
           //system(cmd);


#if 1      
           //svn di to ~/lin/push_code/
           memset(&cmd, 0x0, sizeof(cmd));
           strcat(cmd, "svn di ");
           strcat(cmd, str);
           strcat(cmd, "  >  ~/lin/push_code/diff");

           p = strrchr(str, '/');
           //printf("###p:\n%s\n",p);
           strcat(cmd, p);
          // system(cmd);
           printf("###svn di to:%s\n",cmd);
#endif   

        }

    }
    else
    {
        printf(">>>>>>>>>>./data.txt ERROR!! \n");
    }


    return 0;
}

猜你喜欢

转载自blog.csdn.net/linbounconstraint/article/details/80253700