C语言文件操作

引入的头文件io_utils/time_utils

文件字符个数

#include<stdio.h>
#include<locale.h>
#include<wchar.h>
#include"../include/io_utils.h"

#define ERROR_ILLEGAL_FILENAME -1
#define ERROR_CANNOT_OPEN_FILE -2
#define ERROR_READ_FILE -3
#define ERROR_UNSUPPORTED_CHARSET -99

#define CHARSET_UTF8 0
#define CHARSET_GBK 1

int CountCharactersInFile(const char*filename,int charset)
{
    if(!filename)return ERROR_ILLEGAL_FILENAME;
    FILE*file;
    switch (charset)
    {
    case CHARSET_GBK:
#ifdef _WIN32
    setlocale(LC_ALL,"chs");
#else
    setlocale(LC_ALL,"zh_CN.gbk");
#endif
    file = fopen(filename,"r");
    break;
    case CHARSET_UTF8: setlocale(LC_ALL,"zh_CN.utf-8");
#ifdef _WIN32
    file = fopen(filename,"r,ccs=utf-8");
#else
    file = fopen(filename,"r");
#endif
    break;
    default: return ERROR_UNSUPPORTED_CHARSET;
    }
    if(!file)return ERROR_CANNOT_OPEN_FILE;
    wchar_t wcs[BUFSIZ];
    int count = 0;
    while(fgetws(wcs,BUFSIZ,file))
    {
        count+=wcslen(wcs);
    }
    if(ferror(file))
    {
        perror("CountCharactersInFile error");
        fclose(file);
        return ERROR_READ_FILE;
    }
    fclose(file);
    return count;
}

int main()
{
    PRINT_INT(CountCharactersInFile("filelist.txt",0));
    
    return 0;
}

重定向标准输入输出

#include<stdio.h>

#if defined(__APPLE__) || defined(__linux__) 
#include<unistd.h>
#elif defined(_WIN32)
#include<io.h>
#endif

void RedirectStdout(const char* filename)
{
    static int saved_stdout_no = -1;
    if(filename){
        if(saved_stdout_no == -1){
            saved_stdout_no = dup(fileno(stdout));
        }
        freopen(filename,"a",stdout);
    }else{
        if(saved_stdout_no !=-1){
            dup2(saved_stdout_no,fileno(stdout));
            close(saved_stdout_no);
        }
    }
}

int main()
{

    puts("1");
    RedirectStdout("output.log");
    puts("2");
    RedirectStdout(NULL);
    puts("3");
    return 0;
}

序列化和反序列化

#include<stdio.h>
#include<time.h>
#include"../include/io_utils.h"
#include"../include/time_utils.h"

#define ERROR 0
#define OK 1

typedef struct 
{
    int vilibility;
    int allow_notification;
    int refresh_rate;
    int region;
    int font_size;
}Setting;

void PrintSetting(Setting*setting)
{
    PRINT_INT(setting->vilibility);
    PRINT_INT(setting->allow_notification);
    PRINT_INT(setting->refresh_rate);
    PRINT_INT(setting->region);
    PRINT_INT(setting->font_size);
}
int SaveSetting(Setting*setting,const char* setting_file)
{
    FILE*file = fopen(setting_file,"wb");
    if(!file){
        perror("Failed to save setting");
        return ERROR;
    }else{
        fwrite(setting,sizeof(Setting),1,file);
        fclose(file);
        return OK;
    }
}

int LoadSetting(Setting*setting,const char* setting_file)
{
    FILE*file = fopen(setting_file,"rb");
    if(!file){
        perror("Failed to load setting");
        return ERROR;
    }else{
        fread(setting,sizeof(Setting),1,file);
        fclose(file);
        return OK; 
    }
}

int main()
{   
    Setting setting;
    LoadSetting(&setting,"setting.txt");
    PrintSetting(&setting);
    setting.font_size = 10000;
    SaveSetting(&setting,"setting.txt");
    
    return 0; 
}

读取指定数目

#include<stdio.h>
#include"../include/io_utils.h"
#include"../include/time_utils.h"

#define BUFF_SIZE 4
int main()
{   
    int buff[BUFF_SIZE];
    while(1)
    {
        size_t bytes_read = fread(buff,sizeof(buff[0]),BUFF_SIZE,stdin);
        if(bytes_read<BUFF_SIZE){
            if(feof(stdin)){
                puts("EOF");
                fwrite(buff,sizeof(char),bytes_read,stdout);
            }else if(ferror(stdout)){
                perror("Error read from stdin");
            }
            break;
        }
        fwrite(buff,sizeof(buff[0]),BUFF_SIZE,stdout);
    }

    return 0; 
}

输入读取

#include<stdio.h>
#include"../include/io_utils.h"
#include"../include/time_utils.h"


int main()
{
    char buff[4];
    while (1)
    {
        if(!fgets(buff,4,stdin))
        {
            break;
        }
        printf_s("s",buff);
    }
    

    return 0; 
}

文件复制

#include<stdio.h>
#include<time.h>
#include"../include/io_utils.h"
#include"../include/time_utils.h"

int CopyFile(const char* src,const char* dest)
{
    if(!src || !dest)
    {
        return -1;
    }
    FILE*src_file = fopen(src,"r");
    if(!src_file)
    {
        fclose(src_file);
        return -2;
    }
    FILE*dest_file = fopen(dest,"w");
    if(!dest_file)
    {
        fclose(dest_file);
        return -2;
    }
    int result = 0;
    while(1)
    {
        int next = fgetc(src_file);
        if(next == EOF)
        {
            if(ferror(src_file)){
                result = -3;
            }else if(feof(src_file)){
                result = -1;
            }else{
                result = -2;
            }
            break;
        }
        if(fputc(next,dest_file) == EOF)
        {
            result = -4;
            break;
        }   
    }
    fclose(src_file);
    fclose(dest_file);
    return result;
}
int main()
{
   CopyFile("../include/io_utils.h","io_utils.h");

    return 0; 
}
posted @ 2021-06-28 20:25  放飞梦想C  阅读(74)  评论(0编辑  收藏  举报