您的位置:首页 > 其它

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

2013-09-11 12:40 399 查看
 要求: 写一个程序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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐