您好,欢迎访问一九零五行业门户网

C程序将一个文件的内容复制到另一个文件中

c文件i/o − 创建、打开、读取、写入和关闭文件
c文件管理文件可用于存储大量持久数据。像许多其他语言一样,'c'提供以下文件管理函数:
创建文件打开文件读取文件向文件写入关闭文件以下是'c'中最重要的文件管理函数:
函数目的
fopen () 创建文件或打开现有文件
fclose () 关闭文件
fprintf () 向文件写入数据块
fscanf () 从文件中读取数据块
getc () 从文件中读取单个字符
putc () 向文件中写入单个字符
getw () 从文件中读取整数
putw () 向文件中写入整数
fseek () 将文件指针位置设置为指定位置
ftell () 返回文件指针的当前位置
rewind () 将文件指针设置为文件开头
input:sourcefile = x1.txttargefile = x2.txtoutput: file copied successfully.
说明在这个程序中,我们将一个文件复制到另一个文件,首先您将指定要复制的文件。我们将打开文件,然后以“读”模式读取要复制的文件,以“写”模式读取目标文件。
示例#include <iostream>#include <stdlib.h>using namespace std;int main() { char ch;// source_file[20], target_file[20]; file *source, *target; char source_file[]="x1.txt"; char target_file[]="x2.txt"; source = fopen(source_file, "r"); if (source == null) { printf("press any key to exit...
"); exit(exit_failure); } target = fopen(target_file, "w"); if (target == null) { fclose(source); printf("press any key to exit...
"); exit(exit_failure); } while ((ch = fgetc(source)) != eof) fputc(ch, target); printf("file copied successfully.
"); fclose(source); fclose(target); return 0;}
以上就是c程序将一个文件的内容复制到另一个文件中的详细内容。
其它类似信息

推荐信息