C 语言中的文件操作函数

main.c

#include <stdio.h>
#include <stdlib.h>
#include "constant.h"
/* fputc()和fgetc(),用来读写一个字符,读写对象为文件,同putc()和getc()。putchar()和getchar()读写一个字符,读写对象问终端。 
 * fread()和fwrite(),用来读写一个数据块,读写对象为文件。 
 * fprintf()和fscanf()读写对象是磁盘文件,而printf()和scanf()读写对象是终端。在内存与磁盘频繁交换数据时,最好别用,而用fread、fwrite代替。 
 * fgets()和fputs(),用来读写字符串,读写对象为文件。
 * rewind()使得位置指针重新返回文件开头。
 * fseek()可以指定位置读写,即实现随机读写。
 * ftell()得到流式文件中指针的当前位置。
 * feof(),若到文件末尾,函数值为真(非0)。
 * ferror(),若对文件操作出错,函数值为真(非0)。
 * clearerr(),使得feof()和ferror()的值为0。 
 */
int main(int argc, char *argv[]) {
    FILE *fp;//文件指针 
    char patha[50] = "F:/c/demo/file/";//路径固定部分 
    //printf("%s",strcat(path,"a.txt"));//字符串链接函数测试,必须使得第一个字符串的长度足够大,否则有可能报错。
    //printf("%s",F("a.txt"));//用宏定义表示文件路径 
    //下面两种表示文件路径的方式在Windows系统中都可以,不过最好还是用/来表示路径。 
    //原因就是Linux系统中路径表示为/,都用/,代码的移植性更好。 
    //fp = fopen("F:\\c\\demo\\file\\a.txt", "r");
    //fp = fopen("F:/c/demo/file/a.txt", "r");
    // 文件的打开和关闭
    char file_a[] = "a.txt";
    if ((fp = fopen(strcat(patha, file_a), "r")) == NULL) {
        printf("cannot open %s file.\n", file_a);
    } else {
        printf("already open %s file.\n", file_a);
        printf("the content of file %s is :\n", file_a);
        //feof()为判断文件是否真的结束,既可以判断文本文件,也可以判断二进制文件。
        //判断文本文件,也可以用ch!=EOF来判断,但二进制文件则不行。 
        char ch;
        while (!feof(fp)) {
            ch = fgetc(fp);
            putchar(ch);
        }
        printf("\nnow close %s file ...\n", file_a);
        //顺利的执行了关闭操作,则返回值为0,否则返回EOF(-1) 
        if (fclose(fp) == 0) {//fclose(fp) != EOF
            printf("close file %s successfully.\n\n", file_a);
        } else {
            printf("close file %s failed.\n\n", file_a);
        }
    }
    // 文件的读写
    char file_b[] = "b.txt";
    int i;
    char t;
    // C语言中没有专门的字符串变量,下面三种定义字符数组的方式都可以保存字符串。 
    //char tc[] = {'w','w','w','.','m','e','n','g','l','a','n','g','l','a','n','g','.','c','o','m'};//长度20 
    char tc[] = {"localhost"};//长度21
    //char tc[] = "localhost";//长度21
    //以追加的方式打开文件。不过书上说必须存在该文件才能打开,但这里做实验,不存在也可以创建的。
    //而r方式文件必须存在才能打开,w方式会先创建,奇怪的就是a方式,也许是编译器不一样吧。 
    if ((fp = fopen(F("b.txt"), "a")) == NULL) {
        printf("cannot open %s file.\n", file_b);
    } else {
        printf("already open %s file.\n", file_b);
        //printf("============%d",strlen(tc));//三种定义方式的长度都为20,看来strlen()把末尾的'\0'空操作字符已不算在长度内了 
        for (i = 0; i < strlen(tc); i++) {
            if ((t = fputc(tc[i], fp)) != EOF) {
                printf("Characters %c written to file %s successfully.\n", t, file_b);
            } else {
                printf("Characters %c written to file %s failed.\n", t, file_b);
            }
        }
        if (fclose(fp) == 0) {//fclose(fp) != EOF
            printf("close file %s successfully.\n\n", file_b);
        } else {
            printf("close file %s failed.\n\n", file_b);
        }
    }
//  save();
    copy();
    return 0;
}
constant.h
#define PATH "F:/c/demo/file/"
#define F(fnm) PATH fnm
save.c

#include <stdio.h>
#include <stdlib.h>
/**
 * 在键盘上输入字符,并把这些输入保存到磁盘指定的文件中,输入#表示结束 
 */
void save() {
    FILE *fp;
    char ch;
    char path[50] = "F:/c/demo/file/";
    char filename[10];
    printf("Please input file name : ");
    scanf("%s", filename);
    if ((fp = fopen(strcat(path, filename), "w")) == NULL) {
        printf("cannot open file.\n");
        exit(0);
    }
    ch = getchar();
    ch = getchar();
    while (ch != '#') {
        fputc(ch, fp);
        putchar(ch);
        ch = getchar();
    }
    putchar(10);
    fclose(fp);
}
copy.c

#include <stdio.h>
#include <stdlib.h>
/**
 * 文件的复制 
 */
void copy() {
    FILE *in, *out;
    char ch, infilename[10], outfilename[10];
    char path1[50] = "F:/c/demo/file/";
    char path2[50] = "F:/c/demo/file/";
    printf("Enter the infile name : ");
    scanf("%s", infilename);
    printf("Enter the outfile name : ");
    scanf("%s", outfilename);
    if ((in = fopen(strcat(path1, infilename), "r")) == NULL) {
        printf("cannot open infile %s", infilename);
        exit(0);
    }
    if ((out = fopen(strcat(path2, outfilename), "w")) == NULL) {
        printf("cannot open outfile %s", outfilename);
        exit(0);
    }
    while (!feof(in)) {
        fputc(fgetc(in), out);
    }
    fclose(in);
    fclose(out);
}

猜你喜欢

转载自blog.csdn.net/tzhuwb/article/details/77904555