文件操作

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>

#define defaultPathLen 100

char* typeTurn(char*);                                                                // 大小写转换
void _copyFile(char*, char*);                       // 文件拷贝1. char*->readFilePath 2. char*->writeFilePath
void _crossCombination(char*, char*);                                        // 交叉合并文件                    
void _mnRangle(char*);                                                                // 取m*n列
void _showFileContent(char*);// 显示文件20行内容

int main(int argc, char* argv[]) {
    char _filePath1[defaultPathLen] = "c:/Users/hasee/Desktop/readme.txt";
    char _filePath2[defaultPathLen] = "c:/Users/hasee/Desktop/writeme.txt";

    _copyFile(_filePath1, _filePath2);                                         
    _crossCombination(_filePath1, _filePath2);                         
    _mnRangle(_filePath2);                                                     
    _showFileContent(_filePath1);                                            

    system("pause");
    return 0;
}

char* typeTurn(char* str) {
    if (*str >= 'a' && *str <= 'z') {
        *str -= 32;
    }

    return str;
}

void _copyFile(char* rf, char* wf) {
    FILE* _in;
    FILE* _out;
    char chara = NULL;

    if ((_in = fopen(rf, "r")) == NULL) {
        printf("Disable to open the file... !\n");
        return;
    }
    if ((_out = fopen(wf, "w")) == NULL) {
        printf("Disable to open the file... !\n");
        return;
    }

    chara = fgetc(_in);
    while (chara != EOF) {
        typeTurn(&chara);
        fputc(chara, _out);
        putchar(chara);
        chara = fgetc(_in);
    }
    printf("\n");

    fclose(_in);
    fclose(_out);
}

void _crossCombination(char* rf, char* wf) {
    FILE* _f1;
    FILE* _f2;
    char* chara = (char*)malloc(100*sizeof(char));
    char* _chara = (char*)malloc(100 * sizeof(char));
    int fileBuffer = 100;

    if ((_f1 = fopen(rf, "r")) == NULL) {
        printf("Disable to open the file... !\n");
        return;
    }
    if ((_f2 = fopen(wf, "r")) == NULL) {
        printf("Disable to open the file... !\n");
        return;
    }

    chara = fgets(chara, fileBuffer, _f1);
    _chara = fgets(_chara, fileBuffer, _f2);
    while (true) {
        if (chara != NULL) {
            fputs(chara, stdout);
            chara = fgets(chara, fileBuffer, _f1);
        }
        if (_chara != NULL) {
            fputs(_chara, stdout);
            _chara = fgets(_chara, fileBuffer, _f2);
        }
        else if (chara == NULL && _chara == NULL) {
            break;
        }
    }

    free(chara);
    free(_chara);
    fclose(_f1);
    fclose(_f2);
}

void _mnRangle(char* _fileName) {
    FILE* _fp;
    char chara = NULL;
    int m = 0, n = 0;
    int i = 0;

    if ((_fp = fopen(_fileName, "r")) == NULL) {
        printf("Disable to open the file... !\n");
        return;
    }

    printf("输入 m, n 列 : ");
    scanf("%d%d", &m, &n);
    chara = fgetc(_fp);
    while (chara != EOF) {
        if (i >= m - 1 && i <= n - 1) {
            fputc(chara, stdout);
        }
        chara = fgetc(_fp);
        if (chara == '\n') {
            i = -1;
        } else {
            i++;
        }
    }

    fclose(_fp);
}

void _showFileContent(char* _fileName) {
    FILE* _fp;
    char* chara = (char*)malloc(100 * sizeof(char));
    int ch = 0;
    int i = 0;

    if ((_fp = fopen(_fileName, "r")) == NULL) {
        printf("Disable to open the file... !\n");
        return;
    }

    chara = fgets(chara, 100, _fp);
    while (chara != NULL) {
        if (i < 20) {
            fputs(chara, stdout);
        }
        chara = fgets(chara, 100, _fp);
        if (i == 20) {
            if ((ch = getch()) == 'q') break;
            else i = -1;
        }
        i++;
    }

    free(chara);
    fclose(_fp);
}
挺简单的文件操作练习


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值