创建一个文件,写进内容,拷贝新文件

本文介绍了一个简单的C语言程序,该程序能够根据用户输入创建指定名称的文件,并将预定义的内容写入文件中。此外,程序还会自动创建该文件的一个备份副本,并命名为原文件名加.bak扩展名。

 要求: 写一个程序creat程序,其使用方法为:creat file_name "file content here" 该程序将创建一个文件名为第1个参数(file_name)的文件,并把第二个参数("file content here")作为文件内容,写入创建的文件中,然后再将该文件拷贝一份命名为file_name.bak;

实现:

main.c

/*********************************************************************************
 *      Copyright:  (C) 2013 Yanshifu<Yanshifu@gmail.com>  
 *                  All rights reserved.
 *
 *       Filename:  apue_test_create.c
 *    Description:  This file 
 *                 
 *        Version:  1.0.0(09/10/2013~)
 *         Author:  Yan Shifu <Yanshifu@gmail.com>
 *      ChangeLog:  1, Release initial version on "09/10/2013 06:35:11 PM"
 *                 
 ********************************************************************************/
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>

#define  pstr       "file content here"

/********************************************************************************
 *  Description:
 *   Input Args:
 *  Output Args:
 * Return Value:
 ********************************************************************************/
int main (int argc, char **argv)
{
    int fd;

    if (argc < 2)
    {
        fprintf(stderr, "Usage :%s filename\n", argv[0]);
        exit(1);
    }

    createfile(argv[1], pstr, sizeof(pstr));

    copy(argv[1], ".bak");

    return 0;
} /* ----- End of main() ----- */

copy.c

/*********************************************************************************
 *      Copyright:  (C) 2013 Yanshifu<Yanshifu@gmail.com>  
 *                  All rights reserved.
 *
 *       Filename:  copy.c
 *    Description:  This file 
 *                 
 *        Version:  1.0.0(09/10/2013~)
 *         Author:  Yan Shifu <Yanshifu@gmail.com>
 *      ChangeLog:  1, Release initial version on "09/10/2013 09:52:07 PM"
 *                 
 ********************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>

#define bufsize     1046

void copy(char *filename, const char *cpyname)
{
    int     newsize, oldsize;
    int     ofd,nfd;
    char    name[bufsize];
    char    buf[bufsize];


    if ((ofd = open(filename, O_RDONLY)) < 0)
    {
        printf("open file error in copy\n");
        exit(1);
    }

    if ((oldsize = read(ofd, buf, bufsize)) < 0)
    {
        printf("read oldfile error\n");
        exit(2);
    }

    strcpy(name, filename);
    strcat(name, cpyname);

    if ((nfd = creat(name, 0666)) < 0)
    {
        printf("create newfile error\n");
        exit(3);
    }


    if ((newsize = write(nfd, buf, oldsize)) < 0)
    {
        printf("write newfile error");
        exit(4);
    }

    close(ofd);
    close(nfd);
    }

creat.c

/*********************************************************************************
 *      Copyright:  (C) 2013 Yanshifu<Yanshifu@gmail.com>  
 *                  All rights reserved.
 *
 *       Filename:  create_file.c
 *    Description:  This file 
 *                 
 *        Version:  1.0.0(09/10/2013~)
 *         Author:  Yan Shifu <Yanshifu@gmail.com>
 *      ChangeLog:  1, Release initial version on "09/10/2013 06:37:11 PM"
 *                 
 ********************************************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>

void createfile(const char *filename, const char *str, int len)
{
    int fd, size;

    if ((fd = creat(filename, 0666)) < 0)
    {
        printf("open error\n");
        exit(1);
    }

    if ((size = write(fd, str, len)) < 0)
    {
        printf("write error\n");
        exit(2);
    }

    close(fd);

}

copy.h

/********************************************************************************
 *      Copyright:  (C) 2013 Yanshifu<Yanshifu@gmail.com>
 *                  All rights reserved.
 *
 *       Filename:  fun.h
 *    Description:  This head file 
 *
 *        Version:  1.0.0(09/10/2013~)
 *         Author:  Yan Shifu <Yanshifu@gmail.com>
 *      ChangeLog:  1, Release initial version on "09/10/2013 09:02:44 PM"
 *                 
 ********************************************************************************/

#ifndef _COPY_H
#define _COPY_H

void    copy(char *filename, const char *cpyname);


#endif
~

creat.h

/********************************************************************************
 *      Copyright:  (C) 2013 Yanshifu<Yanshifu@gmail.com>
 *                  All rights reserved.
 *
 *       Filename:  create.h
 *    Description:  This head file 
 *
 *        Version:  1.0.0(09/11/2013~)
 *         Author:  Yan Shifu <Yanshifu@gmail.com>
 *      ChangeLog:  1, Release initial version on "09/11/2013 10:18:01 AM"
 *                 
 ********************************************************************************/

#ifndef _CREATE_H
#define _CREATE_H


void  createfile(const char *filename, const char *str, int len);

#endif

makefile

bins=main
objs=main.o
srcs=main.c
$(bins):$(objs)
        gcc -o main main.o copy.o create.o

$(objs):$(srcs)
        gcc -c main.c

        gcc -c copy.c copy.h

        gcc -c create.c create.h

clean:
        rm -f $(bins) *.o *.h.gch


<think>好的,我现在需要帮助用户创建一个批处理脚本,将文件a复制到文件夹b中。首先,我得确认用户的操作系统,因为批处理通常是Windows上的。不过用户没提,可能默认是Windows吧。 接下来,用户的需求很明确,但可能需要考虑一些细节。比如,文件a的具体路径是什么?是在当前目录下,还是需要绝对路径?还有文件夹b的位置,是否存在?如果不存在,是否需要自动创建? 首先,基本的拷贝命令是copy,语法是copy 源文件 目标文件夹。但要注意,如果目标文件夹不存在,copy命令可能会报错。所以可能需要先检查文件夹是否存在,如果不存在就用md或mkdir创建。 然后,用户可能希望这个批处理脚本能够灵活处理不同情况,比如文件是否存在,是否覆盖已有文件等。不过用户没有特别说明,所以可能保持简单,不添加太多判断。 另外,路径中如果有空格,需要用引号括起来,避免错误。例如,如果文件a的路径是"C:\My Documents\a.txt",或者文件夹b的名字有空格,这时候引号就很重要。 所以,基本的脚本可能如下: ``` @echo off copy "a" "b\" pause ``` 这里,@echo off是关闭回显,让界面更干净。copy命令将文件a复制到文件夹b中,后面的反斜杠是为了确保b被视为文件夹。pause可以让窗口停留,方便查看结果。 不过,可能还需要添加错误处理。比如,如果文件a不存在,或者文件夹b不存在,脚本应该给出提示。这时候可以加入条件判断: ``` @echo off if not exist "a" ( echo 文件a不存在! pause exit /b ) if not exist "b\" ( echo 文件夹b不存在,正在创建... md "b" ) copy "a" "b\" echo 拷贝完成! pause ``` 这里,首先检查文件a是否存在,不存在就提示并退出。然后检查文件夹b是否存在,不存在则创建。最后执行拷贝,并提示完成。 用户可能需要根据实际情况修改路径,比如如果ab不在当前目录,需要写全路径,如"C:\path\to\a""D:\target\b"。另外,如果希望覆盖已有文件,可以添加/Y参数到copy命令,避免提示。 最后,保存为.bat文件,比如copy_a_to_b.bat,然后双击运行。需要确保用户有权限读写目标文件夹。 可能还需要提醒用户,如果文件名或路径含有特殊字符或空格,一定要用引号包裹,否则会导致命令解析错误。此外,如果是在不同驱动器之间复制,可能需要使用xcopy而不是copy,因为copy可能在某些情况下有限制,但通常同一驱动器没问题。 总结一下,核心步骤是检查文件存在性、目标文件夹存在性,执行拷贝,并处理可能的错误情况。根据用户的需求复杂度,脚本可以简单或更健壮。</think>以下是将文件`a`拷贝到文件夹`b`的批处理脚本的详细步骤: --- ### **步骤 1:编写批处理脚本** 新建一个文本文件,输入以下内容: ```batch @echo off REM 检查文件a是否存在 if not exist "a" ( echo 错误:文件a不存在! pause exit /b ) REM 检查文件夹b是否存在,不存在则创建 if not exist "b\" ( echo 文件夹b不存在,正在创建... md "b" ) REM 执行拷贝操作 copy "a" "b\" echo 文件a已成功拷贝到文件夹b中! pause ``` --- ### **步骤 2:保存为批处理文件** 1. 将文件另存为 `.bat` 格式(例如 `copy_a_to_b.bat`)。 2. 确保编码为 **ANSI**(避免中文乱码)。 --- ### **步骤 3:运行脚本** 双击运行 `copy_a_to_b.bat`,根据提示操作。 --- ### **关键说明** 1. **路径问题**: - 如果文件文件夹路径包含空格,需用引号包裹,例如:`copy "C:\My File.txt" "D:\New Folder\"`。 2. **覆盖文件**: - 默认情况下,若目标文件夹已有同名文件,会提示是否覆盖。如需静默覆盖,添加 `/Y` 参数: ```batch copy /Y "a" "b\" ``` 3. **跨驱动器拷贝**: - 如果需要跨驱动器(如从C盘到D盘),建议使用 `xcopy` 命令(功能更强大): ```batch xcopy "a" "b\" /E /C /H /R /Y ``` --- ### **扩展:脚本优化(可选)** 若需更通用化,可让用户输入路径: ```batch @echo off set /p source="请输入源文件路径:" set /p target="请输入目标文件夹路径:" if not exist "%source%" ( echo 错误:源文件不存在! pause exit /b ) if not exist "%target%\" ( md "%target%" ) copy "%source%" "%target%\" echo 文件已成功拷贝! pause ``` --- 通过以上步骤,您可以灵活地将文件拷贝到指定文件夹,并处理常见异常情况。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值