用rewind函数使位置指针指向文件头

来源:互联网 发布:淘宝家具安装工挣钱 编辑:程序博客网 时间:2024/06/04 19:28

本段代码实现了将一个文件内容复制到另外一个文件中的功能

#include<stdio.h>#include<stdlib.h>#include<string.h>int main(){    FILE *fp1,*fp2;    if((fp1=fopen("file1.c","r"))==NULL)    {        printf("cannot open file1\n");        exit(0);    }    if((fp2=fopen("file2.c","w"))==NULL)    {        printf("cannot open file2\n");        exit(0);    }    while(!feof(fp1))        putchar(getc(fp1));    rewind(fp1);/*将文件1的指针重新定位到文件的开头处*/    while(!feof(fp1))        putc(getc(fp1),fp2);    fclose(fp1);    fclose(fp2);    printf("copy compony\n");    return 0;}


0 0
原创粉丝点击