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

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


好的,下面是一个简单的Java代码示例,实现了文件拷贝的GUI界面。 ```java import java.awt.*; import java.awt.event.*; import java.io.*; import javax.swing.*; public class FileCopyGUI extends JFrame implements ActionListener { private JLabel srcLabel, destLabel; private JTextField srcField, destField; private JButton copyButton; public FileCopyGUI() { // 设置窗口标题和大小 setTitle("文件拷贝"); setSize(400, 200); // 创建标签和文本框 srcLabel = new JLabel("源文件"); srcField = new JTextField(); destLabel = new JLabel("目标文件"); destField = new JTextField(); // 创建拷贝按钮 copyButton = new JButton("拷贝"); copyButton.addActionListener(this); // 创建面板并添加组件 JPanel panel = new JPanel(new GridLayout(3, 2)); panel.add(srcLabel); panel.add(srcField); panel.add(destLabel); panel.add(destField); panel.add(copyButton); // 将面板添加到窗口中 add(panel); // 设置窗口可见性和关闭操作 setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } @Override public void actionPerformed(ActionEvent e) { if (e.getSource() == copyButton) { String srcPath = srcField.getText(); String destPath = destField.getText(); File srcFile = new File(srcPath); File destFile = new File(destPath); try { copyFile(srcFile, destFile); JOptionPane.showMessageDialog(this, "拷贝成功!"); } catch (IOException ex) { JOptionPane.showMessageDialog(this, "拷贝失败:" + ex.getMessage()); } } } private void copyFile(File srcFile, File destFile) throws IOException { InputStream in = null; OutputStream out = null; try { in = new FileInputStream(srcFile); out = new FileOutputStream(destFile); byte[] buffer = new byte[1024]; int length; while ((length = in.read(buffer)) > 0) { out.write(buffer, 0, length); } } finally { if (in != null) { in.close(); } if (out != null) { out.close(); } } } public static void main(String[] args) { new FileCopyGUI(); } } ``` 这个程序使用了Java Swing框架来实现GUI界面,包括标签、文本框和按钮等。当用户点击拷贝按钮时,程序会调用copyFile()方法来进行文件拷贝操作。如果拷贝成功,会弹出一个消息框提示用户,否则会显示错误信息。 您可以根据自己的需求和喜好进行修改和完善。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值