标题:关于文件复制问题代码
只看楼主
泪水清痕
Rank: 1
等 级:新手上路
帖 子:17
专家分:1
注 册:2011-4-22
结帖率:100%
 问题点数:0 回复次数:2 
关于文件复制问题代码
// l10_1.2.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#define SIZE 10
int _tmain(int argc, _TCHAR* argv[])
{
    FILE*fp;
    char ch1[SIZE];
    char ch2[SIZE];
    char ch3[SIZE];
    printf("请输入三个字符串:\n");
    gets(ch1);
    gets(ch2);
    gets(ch3);
    if((fp=fopen("string.txt","w"))==NULL)
    {
        printf("Cannot open file strike any key exit!");
        exit(-1);
    }
    fputs(ch1,fp);
    fputs(ch2,fp);
    fputs(ch3,fp);
    fclose(fp);
    char ch4[SIZE*3]={0};
    if((fp=fopen("string.txt","r"))!=NULL)
    {
        
        fgets(ch4,SIZE*3,fp);
        printf("您输入的字符串为:");
        puts(ch4);
        fclose(fp);
    }
    char ch5[SIZE*3];
    if((fp=fopen("string2.txt","w"))==NULL)
    {
        printf("Cannot open file strike any key exit!");
        exit(-1);
    }
    strcpy(ch5,ch4);
    fputs(ch5,fp);
    fclose(fp);
    if((fp=fopen("string2.txt","r"))==NULL)
    {
        printf("Cannot open file strike any key exit!");
        exit(-1);
    }
    fgets(ch5,SIZE*3,fp);
    fclose(fp);
    _getch();
    return 0;
}

红色的字体是复制已有的文件内容
搜索更多相关主题的帖子: include 字符串 file 
2012-08-06 17:27
血祭幻岚
Rank: 2
等 级:论坛游民
帖 子:29
专家分:72
注 册:2012-3-21
得分:0 
什么问题啊?我可以运行诶
2012-08-06 17:51
long0042
Rank: 2
等 级:论坛游民
帖 子:38
专家分:50
注 册:2008-3-5
得分:0 
还是用二进制复制比较好,你那个只能复制,二进制的话可以复制任何文件的。

程序代码:
#include <stdio.h>

int main(int argc, char **argv)
{
    char buffer[1024];
    char *srcFile = "src";
    char *desFile = "des";

    FILE *srcfp = NULL;
    FILE *desfp = NULL;

    if (argc == 3)
    {
        srcfp = fopen(argv[1], "rb");
        desfp = fopen(argv[2], "wb");
    }
    else
    {
        srcfp = fopen(srcFile, "rb");
        desfp = fopen(desFile, "wb");
    }

    if ((srcfp == NULL) || (desfp == NULL))
        return -1;

    for (;;)
    {
        if (feof(srcfp))
            break;

        int readSize = fread(buffer, 1, 1024, srcfp);
        fwrite(buffer, 1, readSize, desfp);
    }

    fclose(srcfp);
    fclose(desfp);

    return 0;
}
2012-08-06 20:24



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-374803-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018857 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved