c 语言文件操作

//
//  main.c
//  FileOperator
//
//  Created by 千 on 16/7/24.
//  Copyright © 2016年 kodulf. All rights reserved.
//

#include <stdio.h>
#include <strings.h>
/*
 文件操作
 1: 打开文件 FILE fopen()
 FILE *fopen(char *name,char *mode)
 mode 有 “w” “r” 和“a” 追加
 打开文件做两件事情,一,获取一个指向文件的文件指针,二决定如何操作文件,读写还是追加

 2: 重命名 rename
 int rename(const char *oldname,const char *newname)
 
 3:关闭文件
了解就好了,这里不强求,java 中必须要的,其实一起记就是都最好关闭
 fclose(file)
 
 //注意这里删除的是FILE的指针,而不是路径的指针
 fclose(openFile);
 
 4: 删除文件
 char *tobeDelete = "/Users/JiYi2013/Documents/ios/day13/tobe_delete.txt";
 FILE *removeFile =fopen(tobeDelete, mode);
 if(removeFile){
 remove(tobeDelete);//注意了这里的remove 里面是路径的指针和fopen 一样使用的路径的指针。
 }
 注意了删除之前一定要先打开
 
 5: 文本文件的操作
 写文本文件
 int fputc(int c, FILE *pfile);
 读文本文件
 mchar = fgetc(pfile);
 写字符串到文本文件
 int fputs(char *pstr,FILE *pfile);
 从文本文件读取字符串
 char *fgets(char *pstr,int nchars,FILE *pfile);
 
 6://二进制的文件
 size_t wcount = fwrite(pdata,sizeof(long),num_items,pfile);
 size_t wcount = fread(pdata,sizeof(long),num_items,pfile);
 
 7://文件位置操作
 告诉我文件位置
 long ftell(FILE *pfile)
 
 定位到某个位置
 int fseek(FILE *pfile,long offset,int original);
 */
#define MAX_LEN 1024


 //读取一个文件的大小

int getFileSize(FILE *fp){
    fseek(fp,0,SEEK_END);
    int size = ftell(fp);
    fclose(fp);
    return size;
}

int main(int argc, const char * argv[]) {
    // insert code here...
    printf("Hello, World!\n");
    char *filePath ="/Users/JiYi2013/Documents/ios/day13/text.txt";
    char *mode = "r";
    //1.打开文件
    FILE *file =fopen(filePath, mode);
    if(file){//不为空的时候
        printf("打开成功了\n");
    }else{//为NULL的时候执行
        printf("打开失败\n");
    }
    //2 重命名文件
    char *newName ="/Users/JiYi2013/Documents/ios/day13/text_new.txt";
    //char *renameMode ="w";修改文件名只需要r的权限就可以了
    FILE *openFile = fopen(filePath,mode);
    if(openFile){
        //打开成功了,说名文件的名字是旧的
        rename(filePath,newName);
        printf("打开成功了,说名文件的名字是旧的,修改为新的名字成功了\n");
        
    }else{
        //打开失败了,说明文件名字是新的名字
        rename(newName,filePath);
        printf("打开失败了,说明文件名字是新的名字,修改为旧的名字\n");
    }
    //3.关闭文件
    //注意这里删除的是FILE的指针,而不是路径的指针
    fclose(openFile);
    
    
    // 4: 删除文件
    char *tobeDelete = "/Users/JiYi2013/Documents/ios/day13/tobe_delete.txt";
    FILE *removeFile =fopen(tobeDelete, mode);
    if(removeFile){
        remove(tobeDelete);//注意了这里的remove 里面是路径的指针和fopen 一样使用的路径的指针。
    }
    
    //写文件的操作
    char *readWritePaht ="/Users/JiYi2013/Documents/ios/day13/read_write.txt";
    char *addMode = "a";
    FILE *writeFile = fopen(readWritePaht,addMode);
    if(writeFile){
        fputc('C', writeFile);
        fclose(writeFile);
    }
    
    //通过字符一个一个的来写字符串
    FILE *writeStringFile = fopen(readWritePaht,addMode);
    if(writeStringFile){
        char *content = "Hello World\n";
        //int len = sizeof(content)/sizeof(char);
        long len =strlen(content);
        for (int i=0; i<len; i++) {
            fputc(content[i], writeStringFile);
        }
        fclose(writeStringFile);
    }
    
    //读字符
    FILE *readFile = fopen(readWritePaht,mode);
    int get;
    //当读取的不适文件的结尾的时候继续读
    while((get =fgetc(readFile))!=EOF){
        putchar(get);
        //printf("读到了:%c",get);
    }
    
    
    //直接写字符串
    char *contentString = "Content String";
    FILE *writeDirectStringFile = fopen(readWritePaht,addMode);
    fputs(contentString, writeDirectStringFile);
    
    //直接读取文件
    char str[50];
    FILE *readDirectStringFile = fopen(readWritePaht,mode);
    if(readDirectStringFile){
        fgets(str, 30, readDirectStringFile);
        printf("读取string为%s",str);
    }
    
    //6: 二进制的文件
    //文件的复制
    char *copyOldPath ="/Users/JiYi2013/Documents/ios/day13/test.png";
    char *copyNewPath = "/Users/JiYi2013/Documents/ios/day13/test_new.png";
    //rb read byte之前的r相当于rt read text。
    FILE *copyOldFile = fopen(copyOldPath, "rb");
    FILE *copyNewFile = fopen(copyNewPath,"wb");
    
    unsigned char buffer[MAX_LEN];
    
    long c;
    //其实下面的已经读到了buffer缓冲区里面了
    while((c = fread(buffer,1,MAX_LEN,copyOldFile))!=0){
        fwrite(buffer,1,c,copyNewFile);
    }
    
    fclose(copyNewFile);
    fclose(copyOldFile);
    
     //7: 文件位置操作
    printf("\n文件位置操作\n");
    FILE *fileTell = fopen(filePath,"rb");
    if(fileTell){
        long p = ftell(fileTell);
        printf("位置:%lu\n",p);//当前位置为0
        fseek(fileTell,20,0);
        long p2 = ftell(fileTell);
        printf("新的位置%lu\n",p2);//当前位置为0
    }
    
    //读取一个文件的大小
    //注意了这里的mode权限r,rb ,这里好像只能是文本文件呢?
    FILE *readFileSize = fopen(readWritePaht,"rb");
    //获取图片文件的大小好像不可以FILE *readFileSize = fopen(copyOldPath,"rb");

    if(readFileSize){
        printf("文件打开成功");
    int fileSize =getFileSize(readFileSize);
    printf("文件的大小为:%d\n",fileSize);
    }else{
        printf("文件打开失败");
    }
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值