2018.08.14

利用文件的操作,是否可以实现复制命令呢?

当然是可以的。不停的将文件一中的字节数读出来放到文件2中,这样就能实现复制操作了。

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>

int main(int argc, char *argv[])
{
	int fd_from, fd_to, ret;
	char buf[32] = {0};

	if (argc != 3)    //入参判断
	{
		printf("Input Error!\n");
		exit(1);
	}

	fd_from = open(argv[1], O_RDONLY);  //只读方式打开第一个文件
	if (-1 == fd_from)
	{
		perror("open1");
		exit(1);
	}

	fd_to = open(argv[2], O_WRONLY | O_CREAT | O_EXCL, S_IRWXU);  //创建第二个文件,只写方式打开
	if (-1 == fd_to)
	{
		perror("open2");
		exit(1);
	}

	while ((ret = read(fd_from, buf, sizeof(buf) - 1)) > 0)
	{
		ret = write(fd_to, buf, ret);
		if (-1 == ret)
		{
			perror("write");
			exit(1);
		}
		memset(buf, 0, sizeof(buf));
	}

	close(fd_from);
	close(fd_to);

	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
###function approximation f(x)=sin(x) ###2018.08.14 ###激活函数用的是sigmoid import numpy as np import math import matplotlib.pyplot as plt x = np.linspace(-3, 3, 600) # print(x) # print(x[1]) x_size = x.size y = np.zeros((x_size, 1)) # print(y.size) for i in range(x_size): y[i] = math.sin(2*math.pi*0.4*x[i])+ math.sin(2*math.pi*0.1*x[i]) + math.sin(2*math.pi*0.9*x[i]) # print(y) hidesize = 10 W1 = np.random.random((hidesize, 1)) # 输入层与隐层之间的权重 B1 = np.random.random((hidesize, 1)) # 隐含层神经元的阈值 W2 = np.random.random((1, hidesize)) # 隐含层与输出层之间的权重 B2 = np.random.random((1, 1)) # 输出层神经元的阈值 threshold = 0.005 max_steps = 1001 def sigmoid(x_): y_ = 1 / (1 + math.exp(-x_)) return y_ E = np.zeros((max_steps, 1)) # 误差随迭代次数的变化 Y = np.zeros((x_size, 1)) # 模型的输出结果 for k in range(max_steps): temp = 0 for i in range(x_size): hide_in = np.dot(x[i], W1) - B1 # 隐含层输入数据 # print(x[i]) hide_out = np.zeros((hidesize, 1)) # 隐含层的输出数据 for j in range(hidesize): # print("第{}个的值是{}".format(j,hide_in[j])) # print(j,sigmoid(j)) hide_out[j] = sigmoid(hide_in[j]) # print("第{}个的值是{}".format(j, hide_out[j])) # print(hide_out[3]) y_out = np.dot(W2, hide_out) - B2 # 模型输出 # print(y_out) Y[i] = y_out # print(i,Y[i]) e = y_out - y[i] # 模型输出减去实际结果。得出误差 ##反馈,修改参数 dB2 = -1 * threshold * e dW2 = e * threshold * np.transpose(hide_out) dB1 = np.zeros((hidesize, 1)) for j in range(hidesize): dB1[j] = np.dot(np.dot(W2[0][j], sigmoid(hide_in[j])), (1 - sigmoid(hide_in[j])) * (-1) * e * threshold) dW1 = np.zeros((hidesize, 1)) for j in range(hidesize): dW1[j] = np.dot(np.dot(W2[0][j], sigmoid(hide_in[j])), (1 - sigmoid(hide_in[j])) * x[i] * e * threshold) W1 = W1 - dW1 B1 = B1 - dB1 W2 = W2 - dW2 B2 = B2 - dB2 temp = temp + abs(e) E[k] = temp if k % 100 == 0: print(k) plt.figure() plt.plot(x, Y) plt.plot(x, Y, color='red', linestyle='--') plt.show()这个程序如何每迭代100次就输出一次图片
07-08
要每迭代100次输出一次图片,你需要在代码中添加相应的逻辑。以下是修改后的代码示例: ```python import numpy as np import math import matplotlib.pyplot as plt x = np.linspace(-3, 3, 600) x_size = x.size y = np.zeros((x_size, 1)) for i in range(x_size): y[i] = math.sin(2*math.pi*0.4*x[i]) + math.sin(2*math.pi*0.1*x[i]) + math.sin(2*math.pi*0.9*x[i]) hidesize = 10 W1 = np.random.random((hidesize, 1)) B1 = np.random.random((hidesize, 1)) W2 = np.random.random((1, hidesize)) B2 = np.random.random((1, 1)) threshold = 0.005 max_steps = 1001 def sigmoid(x_): y_ = 1 / (1 + math.exp(-x_)) return y_ E = np.zeros((max_steps, 1)) Y = np.zeros((x_size, 1)) for k in range(max_steps): temp = 0 for i in range(x_size): hide_in = np.dot(x[i], W1) - B1 hide_out = np.zeros((hidesize, 1)) for j in range(hidesize): hide_out[j] = sigmoid(hide_in[j]) y_out = np.dot(W2, hide_out) - B2 Y[i] = y_out e = y_out - y[i] dB2 = -1 * threshold * e dW2 = e * threshold * np.transpose(hide_out) dB1 = np.zeros((hidesize, 1)) for j in range(hidesize): dB1[j] = np.dot(np.dot(W2[0][j], sigmoid(hide_in[j])), (1 - sigmoid(hide_in[j])) * (-1) * e * threshold) dW1 = np.zeros((hidesize, 1)) for j in range(hidesize): dW1[j] = np.dot(np.dot(W2[0][j], sigmoid(hide_in[j])), (1 - sigmoid(hide_in[j])) * x[i] * e * threshold) W1 = W1 - dW1 B1 = B1 - dB1 W2 = W2 - dW2 B2 = B2 - dB2 temp = temp + abs(e) E[k] = temp if k % 100 == 0: plt.figure() plt.plot(x, Y) plt.plot(x, Y, color='red', linestyle='--') plt.savefig(f'iteration_{k}.png') plt.close() plt.figure() plt.plot(x, Y) plt.plot(x, Y, color='red', linestyle='--') plt.show() ``` 在上述示例中,我添加了一个条件判断语句`if k % 100 == 0`来判断是否达到每100次迭代的条件。如果满足条件,则在该迭代结束后绘制并保存图片,文件名为`iteration_k.png`,其中`k`表示当前迭代次数。 请根据你的具体需求和编程环境进行相应的调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值