创建一个文件,写进内容,拷贝新文件
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
实现:
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
相关文章推荐
- Linux 如何用vim命令把编辑文件的几行内容拷贝到一个新文件
- java创建一个文件并写入内容
- vim中如何将一个文件的内容拷贝到另一个文件中
- IOS从.plist文件中读取内容,创建一个图片浏览器
- 字节流方式:输入一个已经存在的文件名称,和一个目标文件名称,实现文件内容的拷贝
- 一个简单的利用ini配置文件以及用户输入的交互进行文件夹内容拷贝的批处理文件脚本的实现
- HTML 5 应用程序缓存(Application Cache)cache manifest 文件使用 html5 中创建manifest缓存以及更新方法 一个manifest文件会创建一份缓存,不同的manifest文件其缓存的内容是互不干扰的
- Linux 如何用vim命令把编辑文件的几行内容拷贝到一个新文件
- 用shell脚本写出检测/tmp/size.log文件,如果存在显示它的内容,不存在则创建一个文件将创建时间写入
- 显示桌面 图标 创建可以新建一个txt文件,把以下内容放入,名字改为显示桌面 .scf
- Linux -->在目录内创建文件、显示文件以及拷贝文件到一个目录都需要什么权限?
- 用java怎么创建一个文件并向该文件写文本内容
- 创建4个线程,4个线程同时对一个文件进行修改。线程1负责向文件写入A,线程2负责向文件写入B,线程3负责写入C,线程4负责写入D。最终文件里的内容应该是ABCDABCDABCD....循环10次;
- 内容输出Linux文件系统的的实现:创建一个文件的过程
- 编写一个文件,读取 src 下面day12包 下面的my.properties 文件,文件内容如下, className=day12.User 结合 IO、反射知识,完成 配置文件中对象的创建和方法调用。并编写方法,列举出 Student 类中所有的属性、
- 创建一个FileputStream类对象读取文件中的内容,并使用循环判断是否到达文件尾
- 用 echo 创建文件内容时,如何避免尾部的多出来的一个空格
- 将目标文件的一部分或者全部内容拷贝到另外一个目标文件中 objcopy
- C#File类常用的文件操作方法(创建、移动、删除、复制等) File类,是一个静态类,主要是来提供一些函数库用的。静态实用类,提供了很多静态的方法,支持对文件的基本操作,包括创建,拷贝,移动,删除和
- 第二周第四天(解析/过滤文件/找d盘里的所有txt文件/FileInputStream/FileOutputStream/拷贝文件/readLine读取一个文本行/在一个文档里写入内容)