• C,C++,windows api, linux api 操作文件总结


    背景:

    操作文件是编程必不可少的内容,工作中经常看到各种操作文件的方式,倍感困惑,所以总计了C/C++在windows和linux平台下文件的基本操作


    ================
    C
    ================

    -------- 头文件
    <stdio.h>

    -------- 相关函数和接口
    FILE *fopen( const char * filename, const char * mode );
    int fclose( FILE *fp );
    int fputc( int c, FILE *fp );
    int fputs( const char *s, FILE *fp );
    int fgetc( FILE * fp );
    char *fgets( char *buf, int n, FILE *fp );

    -------- 例子

    #include <stdio.h>
    
    main()
    {
    FILE *fp;
    
    fp = fopen("/tmp/test.txt", "w+");
    fprintf(fp, "This is testing for fprintf...
    ");
    fputs("This is testing for fputs...
    ", fp);
    fclose(fp);
    }

      

    ================
    C++
    ================

    -------- 头文件
    <fstream>
    <ifstream>
    <ofstream>
    -------- 相关函数和接口
    void open(const char *filename, ios::openmode mode);
    void close();
    流插入运算符( << )向文件写入信息
    流提取运算符( >> )从文件读取信息

    -------- 例子

    #include <fstream>
    #include <iostream>
    using namespace std;
    
    int main ()
    {
    
    char data[100];
    
    // 以写模式打开文件
    ofstream outfile;
    outfile.open("afile.dat");
    
    cout << "Writing to the file" << endl;
    cout << "Enter your name: "; 
    cin.getline(data, 100);
    
    // 向文件写入用户输入的数据
    outfile << data << endl;
    
    cout << "Enter your age: "; 
    cin >> data;
    cin.ignore();
    
    // 再次向文件写入用户输入的数据
    outfile << data << endl;
    
    // 关闭打开的文件
    outfile.close();
    
    // 以读模式打开文件
    ifstream infile; 
    infile.open("afile.dat"); 
    
    cout << "Reading from the file" << endl; 
    infile >> data;
    
    // 在屏幕上写入数据
    cout << data << endl;
    
    // 再次从文件读取数据,并显示它
    infile >> data; 
    cout << data << endl;
    
    // 关闭打开的文件
    infile.close();
    
    return 0;
    }

    ================
    windows api
    ================

    -------- 头文件
    <windows.h>
    -------- 相关函数和接口

    CreateFile 创建、打开文件
    ReadFile 读取文件内容
    WriteFile 写入文件内容
    SetFilePointer 移动文件指针
    SetEndOfFile 设置文件结尾标志
    CopyFile 文件拷贝
    DeleteFile 文件删除
    MoveFile 文件移动
    CreateDirectory 创建一个目录
    RemoveDirectory 删除一个目录
    GetCurrentDirectory 获取当前程序所在目录
    SetCurrentDirectory 设置当前程序所在目录

    -------- 例子

    #include <windows.h>
    #include <stdio.h>
    #include <string.h> 
    int isDirectory(char *path);
    void help();
    
    int main(int argc, char const *argv[])
    {
    char file_src[MAX_PATH]={0};
    char file_dest[MAX_PATH]={0};
    
    // 只输入程序名 和一个参数则调用help
    if (argc <= 2)
    {
    help();
    return 0;
    }
    
    memmove(file_src, argv[1], strlen(argv[1]));
    memmove(file_dest, argv[2], strlen(argv[2]));
    
    if( isDirectory(file_dest) )
    { // 如果第二个参数是目录, 则拼装新的文件路径
    sprintf(file_dest, "%s\%s", file_dest, file_src);
    }
    
    if( CopyFile(file_src, file_dest, 0) == 0)
    printf("文件复制失败!");
    
    return 0;
    }
    
    // 判断是否为目录
    BOOL isDirectory(char *path)
    {
    WIN32_FIND_DATA fd;
    BOOL rel = FALSE;
    char *p = path;
    
    // 查找到第一个文件的句柄
    HANDLE hFind = FindFirstFile(path, &fd);
    
    while(*p != '') p++;
    
    // 如果结尾是这两种符号就肯定是目录
    if( *(--p) == '\' || *(p) == '/' ) {
    *p = '';
    return TRUE;
    }
    
    // 判断是否获取错误
    if(hFind != INVALID_HANDLE_VALUE)
    {
    // 文件信息按位与上目录属性, 非目录则全部置零
    if( fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
    {
    rel = TRUE; 
    }
    // 关闭查找句柄
    FindClose(hFind);
    }
    return rel; 
    }
    
    int touchfile(int argc, char const *argv[])
    {
    int i;
    
    // 参数数目为1 即用户只输入程序名,程序仅收到一个参数
    if (argc == 1)
    {
    help();
    return 0;
    }
    
    // 遍历指针数组所指向的参数
    for (i = 1; i < argc; i++)
    {
    CreateFile(
    argv[i], // 文件名
    GENERIC_WRITE, // 写入权限
    0, // 阻止其他进程访问
    NULL, // 子进程不可继承本句柄
    CREATE_NEW, // 仅不存在时创建新文件
    FILE_ATTRIBUTE_NORMAL, // 普通文件
    NULL); // 不适用模板文件
    }
    
    
    return 0;
    }
    
    void help()
    {
    printf("复制文件:
    ");
    printf("cp <文件名> <新路径>
    ");
    printf("cp <文件名> <新文件名>
    ");
    printf("cp <文件名> <新路径\新文件名>");
    }

    ================
    linux api
    ================
    -------- 头文件
    <unistd.h>
    <fcntl.h>
    -------- 相关函数和接口

    int creat(const char *filename, mode_t mode);

    int open(const char *pathname, int flags);
    int open(const char *pathname, int flags, mode_t mode);

    int read(int fd, const void *buf, size_t length);
    int write(int fd, const void *buf, size_t length);

    int close(int fd);

    -------- 例子

    #include <stdlib.h>
    #include <stdio.h>
    #include <fcntl.h>
    #include <unistd.h>
    
    #define BUFFER 4096
    
    int main(int argc,char *argv[])
    {
    int n;
    char buf[BUFFER];
    int fd1 = open(argv[1],O_RDONLY);
    int fd2 = open(argv[2],O_WRONLY);
    while( (n=read(fd1,buf,BUFFER))>0 )
    {
    if( (write(fd2,buf,n))!=n )
    {
    printf("error
    ");
    exit(1);
    }
    }
    close(fd1);
    close(fd2);
    exit(0);
    }
  • 相关阅读:
    HTML5学习笔记-2.HTML基础
    HTML5学习笔记-1.网页入门
    Java学习笔记-8.多线程编程
    Java学习笔记-7.Java IO流
    Java学习笔记-6.Java异常处理
    Java学习笔记-5.常用数据结构
    Java学习笔记-4.继承与多态
    Java学习笔记-3.类与对象
    C语言学习笔记-10.文件
    .net core 中threadpool用法
  • 原文地址:https://www.cnblogs.com/fyzjhh/p/6626658.html
Copyright © 2020-2023  润新知