C语言中的文件操作

2019-03-25  本文已影响0人  simonycshi

C语言中的文件操作

1. 打开文件

FILE * fopen(const char * path, const char * mode);

2. 读写文件

2.1 读写单个字符:getc / putc

getc:

int getc(FILE *infp);

putc:

int putc(int c, FILE *outfp);

2.2 读写字符串:fgets / fputs

fgets:

char * fgets(char *s, int max, FILE *fp)



fputs:

int fputs(char *s, FILE *fp)

2.3 格式化读写:fscanf / fprintf

fscanf:

int fscanf(FILE * stream, const char * format, [argument...]);



fprintf:

int fprintf(FILE *stream, const char *format, [ argument ]...)

2.4 按字节读写:fread / fwrite / fseek

fread:

size_t fread(void *buffer, size_t size, size_t count, FILE *stream) ;



fwrite:

size_t fwrite(const void *buffer, size_t size, size_t count, FILE *stream);



fseek:

int fseek(FILE *stream, long offset, int fromwhere);



示例:

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

int main () 
{
   FILE *fp;
   char c[] = "this is tutorialspoint";
   char buffer[100];

   /* w+模式打开文件 */
   fp = fopen("file.txt", "w+");

   /* 将字符串写入文件 */
   fwrite(c, strlen(c) + 1, 1, fp);

   /* 设置指针至文件开头 */
   fseek(fp, 0, SEEK_SET);

   /* 读取并显示数据 */
   fread(buffer, strlen(c) + 1, 1, fp);
   printf("%s\n", buffer);
   
   fclose(fp);
   return(0);
}

输出:

this is tutorialspoint

3. 关闭文件

int fclose( FILE *myFile );

4. 其他文件方法

int getw(FILE *fp)
int putw(int w, FILE *fp);
long ftell(FILE *stream)
void rewind(FILE *stream)

5. 判断文件状态

注意⚠️:可在fgets等函数返回NULL时用文件状态函数判断时读取发生了错误还是遇到了EOF

int ferror(FILE *fp)
int feof(FILE *fp)

6. 缓存(Buffer)

image

强制刷新缓存:fflush

//显示器
fflush(stdout);
//文件
fflush(outfile);

7. 计算文件大小

#include<stdio.h>

void main(){
    FILE *fp;
    char ch;
    int size = 0;
 
    fp = fopen("MyFile.txt", "r");
    if (fp == NULL){
        printf("文件打开失败");
    }
    
    /* 将指针移至文件末尾 */
    fseek(fp, 0, 2);  
    /* 获取末尾位置的偏移量 */  
    size = ftell(fp);   
    printf("文件大小为: %d\n", size);    
    fclose(fp);
}

8. 拷贝文件内容

#include<stdio.h>

void main(){   
    FILE *fp1, *fp2;
    char ch;
    int pos;
 
    if ((fp1 = fopen("File_1.txt","r")) == NULL){    
        printf("文件打开失败");
        return;
    }
    fp2 = fopen("File_2.txt", "w"); 
    
    //将指向文件fp1的指针指向fp1末尾
    fseek(fp1, 0L, SEEK_END); 
    //计算文件fp1的长度
    pos = ftell(fp1);
    //将指向文件fp1的指针指会fp1开头
    fseek(fp1, 0L, SEEK_SET);
    while (pos--){
        //逐个字符拷贝
        ch = fgetc(fp1);
        fputc(ch, fp2);
    }    
    fcloseall();    
}

9. 逆转文件内容

#include<stdio.h>
#include<errno.h>
 
//计算原文件长度
long count_characters(FILE *);
 
void main(){
    int I;
    long cnt;
    char ch, ch1;
    FILE *fp1, *fp2;
 
    if (fp1 = fopen("File_1.txt", "r")) {
        fp2 = fopen("File_2.txt", "w");
        cnt = count_characters(fp1); 
        
        /* 
        将fp1指针指向文件的最后一位字符
        */
        fseek(fp1, -1L, SEEK_END);     
        printf("拷贝字符长度: %d\n", ftell(fp1));
 
        while (cnt){
            ch = fgetc(fp1);
            fputc(ch, fp2);
            //为什么这里移动的是两个?
            fseek(fp1, -2L, SEEK_CUR);
            cnt--;
        }
        printf("反转拷贝成功");
    }else{
        perror("Error occured\n");
    }
    fclose(fp1);
    fclose(fp2);
}

/* 
计算文件的长度
*/
long count_characters(FILE *f) {
    fseek(f, -1L, 2);
    long last_pos = ftell(f);
    last_pos++;
    return last_pos;
}
上一篇 下一篇

猜你喜欢

热点阅读