版權(quán)聲明:本文為小斑馬偉原創(chuàng)文章,轉(zhuǎn)載請注明出處!
一、大文件拷貝
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>
#define SIZE 1024
#include<sys/types.h>
#include<sys/stat.h>
int main(int argc, char* argv[]) {
for (int i = 0; i < argc; i++) {
printf("%s\n", argv[i]);
}
//用戶輸入?yún)?shù)缺少
if (argc < 3) {
printf("缺少參數(shù)");
}
//D:/copy.exe D:/test.avi D:/Code/test.avi
//argv[0] 程序名稱 文件大小是50M
FILE* fp1 = fopen(argv[1], "rb");
FILE* fp2 = fopen(argv[2], "wb");
if (!fp1 || !fp2) {
printf("復制文件出錯\n");
return -2;
}
char* temp = NULL;
int size = 0;
struct stat st;
stat(argv[1], &st);
//根據(jù)文件實際大小開辟空間
if (st.st_size > SIZE) {
temp = (char*)malloc(sizeof(char)* SIZE);
size = SIZE;
} else {
temp = (char*)malloc(sizeof(char)* st.st_size +10);
size = st.st_size + 10;
}
int count = 0;
while (!feof(fp1)) {
mement(temp, 0, size);
count = fread(temp, sizeof(char), size, fp1);
fwrite(temp, sizeof(char), count, fp2);
}
free(temp);
fclose(fp2);
fclose(fp1);
return EXIT_SUCCESS;
}
二、獲取文件狀態(tài)

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>
#include<sys/types.h>
#include<sys/stat.h>
int main(void) {
//文件狀態(tài)結(jié)構(gòu)體變量
struct stat st;
stat("D:/copy.exe", &st);
printf("文件大小:%d\n", st.st_size);
printf("獲取文件最后一次訪問的時間:%d\n", st.st_atime);
printf("獲取文件最后一次修改的時間:%d\n", st.st_mtime);
system("pause");
return EXIT_SUCCESS;
}
三、文件的隨機讀寫
#include<stdio.h>
int fseek(FILE* stream,long offset,int whence);
功能:移動文件(文件光標)的讀寫位置.
參數(shù): stream 已經(jīng)打開的文件指針 offset:根據(jù)whence來移動的位移數(shù)(偏移量),可以是正數(shù),也可以負數(shù),如果正數(shù),則相對于whence往后移動,如果是負數(shù),則相對于whence往左移動。如果向前移動的字節(jié)數(shù)超過文件開頭則出錯返回,如果向后移動的字節(jié)數(shù)超過了文件末尾,再次寫入時將增大文件尺寸。whence:其取值如下:
SEEK_SET:從文件開頭移動offset個字節(jié)
SEEK_CUR:從當前位置移動offset個字節(jié)
SEEK_END:從文件末尾移動offset個字節(jié)
返回值: 成功 0 失敗 -1
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>
int main(void) {
FILE* fp = fopen("D:/a.txt", "r");
if (!fp)
return -1;
char arr[100];
memset(arr, 0, 100);
fgets(arr, 100, fp);
printf("%s", arr);
memset(arr, 0, 100);
fgets(arr, 100, fp);
printf("%s", arr);
//文件隨機讀寫
fseek(fp, -8, SEEK_CUR);
memset(arr, 0, 100);
fgets(arr, 100, fp);
printf("%s", arr);
//從文件開始位置
fseek(fp, 11, SEEK_SET);
memset(arr, 0, 100);
fgets(arr, 100, fp);
printf("%s", arr);
//從文件末尾位置
fseek(fp, -17, SEEK_END);
memset(arr, 0, 100);
fgets(arr, 100, fp);
printf("%s", arr);
fclose(fp);
return EXIT_SUCCESS;
}
int main(void) {
FILE* fp = fopen("D:/a.txt", "r+");
if (!fp)
return -1;
//獲取光標的位置
long pos = ftell(fp);
printf("%ld\n", pos);
fseek(fp, -17, SEEK_END);
fputs("你好 哈哈\n", fp);
fclose(fp);
return EXIT_SUCCESS;
}
int main(void) {
FILE* fp = fopen("D:/a.txt", "w");
if (!fp)
return -1;
char ch1[] = "hello 哈哈哈\n";
char ch2[] = "world";
fputs(ch1, fp);
fseek(fp, -14, SEEK_CUR);
fputs(ch2, fp);
fclose(fp);
return EXIT_SUCCESS;
}
四、獲取文件光標位置
#include<stdio.h>
long ftell(FILE* stream);
功能:獲取文件流(文件光標)的讀寫位置。
參數(shù):stream :已經(jīng)打開的文件指針
返回值:成功:當前文件流(光標光標)的讀寫位置 失敗 -1
int main(void) {
FILE* fp = fopen("D:/a.txt", "r");
if (!fp)
return -1;
char arr[100];
//獲取文件光標流所在位置
long pos = ftell(fp);
printf("%ld\n", pos);
fgets(arr, 100, fp);
fseek(fp, 8, SEEK_CUR);
pos = ftell(fp);
printf("%ld\n", pos);
//重置文件光標在起始位置
rewind(fp);
memset(arr, 0, 100);
fgets(arr, 100, fp);
printf("%s\n", arr);
return EXIT_SUCCESS;
}
五、刪除文件、重命名文件名
#include<stdio.h>
int remove(const char* pathname);
功能:刪除文件
參數(shù):pathname:文件名
返回值:成功 0 失敗 -1
#include<stdio.h>
int rename(const char* oldpath,const char* newpath);
功能:把oldpath的文件名改為newpath
參數(shù):oldpath:舊文件名 newpath:新文件名
返回值:成功 0 失敗 -1
int main() {
int value = remove("D:/b.txt");
if (value == 0) {
printf("刪除文件成功\n");
}
else {
printf("刪除文件失敗\n");
}
return EXIT_SUCCESS;
}
int main(void) {
//重命名
int value = rename("D:/a.txt", "D:/abc.txt");
//移動文件 剪切
int value = rename("D:/a.txt", "D:/Code/abc.txt");
if (value == 0) {
printf("成功\n");
}
else {
printf("失敗\n");
}
return EXIT_SUCCESS;
}
六、文件緩沖區(qū)
文件緩沖區(qū):ANSI C標準采用"緩沖文件系統(tǒng)"處理數(shù)據(jù)文件。
所謂緩沖文件系統(tǒng)是指系統(tǒng)自動地在內(nèi)存區(qū)為程序中每一個正在使用的文件開辟一個文件緩沖區(qū)從內(nèi)存向磁盤輸出數(shù)據(jù)必須先送到內(nèi)存中的緩沖區(qū),裝滿緩沖區(qū)后才一起送到磁盤區(qū)。
如果從磁盤向計算機讀入數(shù)據(jù),則一次從磁盤文件將一批數(shù)據(jù)輸入到內(nèi)存緩沖區(qū)(充滿緩沖區(qū))。然后再從緩沖區(qū)逐個地將數(shù)據(jù)送到程序數(shù)據(jù)區(qū)(給程序變量)
int fflush(FILE* stream)
功能:更新緩沖區(qū),讓緩沖區(qū)的數(shù)據(jù)立馬寫到文件中。
參數(shù):stream 文件指針
返回值:成功 0 失敗 -1

int main(void) {
FILE* fp = fopen("D:/a.txt", "r");
if (!fp)
return -1;
char ch;
while (1) {
scanf("%c", &ch);
if (ch == '@') {
break;
}
//頻繁的和硬盤交互 損傷硬盤
fflush(fp);
fputc(ch, fp);
}
fclose(fp);
return EXIT_SUCCESS;
}