c 标准io <stdio.h>

读文件到文件尾

    FILE *file;
    if((file = fopen("D://a.txt","r")) == NULL)
    {
        printf("cant open file!");
    }
    char ch;
    while((ch = fgetc(file))!=EOF) putchar(ch);
    if(fclose(file)) printf("file close error!");

从键盘输入字符存入文件

    FILE *file;
    if((file = fopen("D://a.txt","w")) == NULL)
    {
        printf("cant open file!");
    }
    char ch;
    while((ch = fgetchar()!='\n')) fputc(ch,file);
    if(fclose(file)) printf("file close error!");

读写字符串

char * fgets(char *str,int num,FILE *file)
char * fputs(char *str,FILE *file)
#include <stdio.h>
#include <string.h>
main(){
    FILE * fp1, *fp2;
    char str[128];
    if((fp1 = fopen("text1.txt","r"))==NULL)
    {
        /*以只读方式打开文件1*/
        printf("can not open file \n")
        exit(0);
    }
    if((fp2 = fopen("test2.txt","w")) == NULL){
        /*以只写方式打开文件2*/
        printf("can not open file\n");
        exit(0);        
    }
    while((strlen(fgets(str,128,fp1)))>0)
    /*从文件中读回字符串长如大于0*/
    printf("%s",str);
    /*在屏幕显示*/
    fclose(fp1);
    fclose(fp2);
}

获取文件长度

    fseek (file, 0, SEEK_END);   // non-portable
    int size=ftell (file);
    rewind (file);

成块读写文件

fread
fwrite
原文地址:https://www.cnblogs.com/wangjixianyun/p/2983629.html