C语言如何将2.txt文件的内容追加到1.txt文件末尾,并写入3.txt?

Python09

C语言如何将2.txt文件的内容追加到1.txt文件末尾,并写入3.txt?,第1张

int main(){

//以读的方式打开文件

FILE * fp1 = fopen("1.txt","r")

if(fp1 == NULL)

{

perror("fopen1 error")

exit(-1)

}

//以读的方式打开文件

FILE * fp2 = fopen("2.txt","r")

if(fp2 == NULL)

{

perror("fopen2 error")

exit(-1)

}

//以写的方式打开文件

FILE * fp3 = fopen("3.txt","w")

if(fp3 == NULL)

{

perror("fopen3 error")

exit(-1)

}

//初始化3个字符

char ch1 = '0'

while((ch1 = getc(fp1)) != EOF)

{

putc(ch1,fp3)

}

while((ch1 = getc(fp2)) != EOF)

{

putc(ch1,fp3)

}

//关闭文件1,2,3

fclose(fp1)

fclose(fp2)

fclose(fp3)

return 0

}

使用fopen函数打开文件,用fseek函数将文件位置调整到文件末尾,然后用fwrite函数写入数据即可。下面的示例代码,向1.txt的文件中追加hello world的字符串。

#include <stdio.h>

#include <string.h>

int main()

{

FILE *fp = fopen("1.txt", "a+")

if (fp==0) { printf("can't open file\n") return 0}

fseek(fp, 0, SEEK_END)

char sz_add[] = "hello world\n"

fwrite(sz_add, strlen(sz_add), 1, fp)

fclose(fp)

return 0

}

怎么用C语言在一个文件后面添加内容

使用fopen函数打开文件,用fseek函数将文件位置调整到文件末尾,然后用fwrite函数写入数据即可。下面的示例代码,向1.txt的文件中追加hello world的字符串。

#include <stdio.h>

#include <string.h>

int main()

{

FILE *fp = fopen("1.txt", "a+")

if (fp==0) { printf("can't open file\n")return 0}

fseek(fp, 0, SEEK_END)

char sz_add[] = "hello world\n"

fwrite(sz_add, strlen(sz_add), 1, fp)

fclose(fp)

return 0

}