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

 要求: 写一个程序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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值