2022年5月15日 星期日

[C/C++] 一行一行讀檔

說明:
一行一行讀檔

Code:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>

#define BOOL                    int
#define TRUE                    0
#define FALSE                   -1
#define MAX_LONG_LENGTH         1024
#define MAX_SHORT_LENGTH        128

BOOL load_file_fun(char *filename, char **p_content)
{
    int fd = -1;    
    int totalsize = 0;
    int readsize = 0;
    char buffer[MAX_SHORT_LENGTH];
    BOOL ret = FALSE;
    
    if ( (fd = open((char *)filename, O_RDONLY)) > 0 )
    {
        totalsize = 0;
        while( (readsize = read(fd, buffer, MAX_SHORT_LENGTH)) > 0)
            totalsize += readsize;

        if (totalsize <= 0)
        {
            printf("[ERROR] %s file size is wrong!\n", filename);
            return ret;
        }    
        
        if (lseek(fd, 0, SEEK_SET) == -1)
        {
            printf("[ERROR] %s can't seek!\n", filename);
            return ret;
        }
        
        *p_content = (char*)malloc( ( totalsize + 1) * sizeof(char));
        memset(*p_content, '\0', totalsize + 1);

        if (*p_content)
        {
            if(read(fd, *p_content, totalsize) > 0 )
            {
                ret = TRUE;
            }
            else
            {
                printf("[ERROR] %s can't read!\n", filename);
                free(*p_content);
            }

            close(fd);
        }
    }
    else
        printf("[ERROR] Open %s fail!\n", filename);

    return ret;    
}

BOOL load_file_by_line_fun(char *file)
{
    char *p_content = NULL;
    char *p_tmp = NULL;
    char *p_feed = NULL;
    char *p_line = NULL;
    int len = 0;
    BOOL ret = FALSE;

    if (load_file_fun(file, &p_content) == TRUE)
    {
        p_tmp = p_content;

        while((p_feed = strpbrk(p_tmp, "\n")))
        {
            len = p_feed - p_tmp;
            p_line = (char*)malloc(len + 1);
            memset(p_line, '\0', len + 1);
            strncpy(p_line, p_tmp, len);
            printf("p_line : %s\n", p_line);
            free(p_line);
            p_tmp = p_feed + 1;
        }
        ret = TRUE;
    }

    if (p_content)
        free(p_content);
 
    return ret;
}

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

    load_file_by_line_fun("/etc/exports");
    
    return 0;
}

Result:
p_line : # /etc/exports: the access control list for filesystems which may be exported
p_line : #              to NFS clients.  See exports(5).
p_line : #
p_line : # Example for NFSv2 and NFSv3:
p_line : # /srv/homes       hostname1(rw,sync,no_subtree_check) hostname2(ro,sync,no_subtree_check)
p_line : #
p_line : # Example for NFSv4:
p_line : # /srv/nfs4        gss/krb5i(rw,sync,fsid=0,crossmnt,no_subtree_check)
p_line : # /srv/nfs4/homes  gss/krb5i(rw,sync,no_subtree_check)
p_line : #
p_line : /tftpboot       *(insecure,rw,no_subtree_check,no_root_squash,no_all_squash)
p_line : /SOURCE         *(insecure,rw,no_subtree_check,no_root_squash,no_all_squash)


沒有留言:

張貼留言