#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);
}
挺简单的文件操作练习
文件操作
最新推荐文章于 2022-10-18 11:51:49 发布