stdin_to_stdout.c

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

#define BUFFSIZE 4096

int main(void)
{
	int n,m;
	char buf[BUFFSIZE] = {0};
	
	while((n = read(STDIN_FILENO, buf, BUFFSIZE)) > 0){
		if(m = write(STDOUT_FILENO, buf, BUFFSIZE) <  0){
			printf("write error\n");
			exit(1);
		}
		
		if(m < n)
		  	break;
	}
		
	if (n < 0)
	{
		printf("read error\n");
		exit (1);
	}
	
	return 0;
}


两个常量STDIN_FILENO 和 STDOUT_FILENO 定义在 <unistd.h> 头文件中,他们指定了标准输入和标准输出的文件描述符。在POSIX标准中,它们的值分别是 0 和 1 。

read 函数

ssize_t read(int fd, void *buf, size_t count);   //读取文件,将读取的字符放入某一个预存的缓冲区内。成功执行返回所读取数据的数量,失败返回-1

fd :这是一个已经打开的文件句柄(文件描述符),表示从这个文件句柄所代表的文件读取数据。
buf : 指缓冲区。读取的数据会放到这里。
count : 表示每次所读取的字符数量


write函数

ssize_t write(int fd, const void *buf, size_t count);  //想打开的设备或文件中些数据。成功返回写入的字节数,出错返回-1

fd :要进行写操作的文件描述符
buf : 需要输出的缓冲区
count : 最大输出字节计数

把buf缓冲区中的数据写入到fd所代表的文件中,最大写入字节数为count

另外需要注意的是,read函数中count的值,可以认为给定(一般要比你实际可能读到的字符数要大),而write函数中却不能这样,他必须是实际应该写入的字符数(缓冲区中读取到的字节数)。否则可能会出现越界错误。




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Traceback (most recent call last): File "C:\Users\Administrator\PycharmProjects\pythonProject4\venv\lib\site-packages\pytesseract\pytesseract.py", line 392, in get_tesseract_version stdin=subprocess.DEVNULL, File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\subprocess.py", line 411, in check_output **kwargs).stdout File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\subprocess.py", line 488, in run with Popen(*popenargs, **kwargs) as process: File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\subprocess.py", line 800, in __init__ restore_signals, start_new_session) File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\subprocess.py", line 1207, in _execute_child startupinfo) File "D:\PyCharm Community Edition 2022.1.3\plugins\python-ce\helpers\pydev\_pydev_bundle\pydev_monkey.py", line 575, in new_CreateProcess return getattr(_subprocess, original_name)(app_name, patch_arg_str_win(cmd_line), *args) FileNotFoundError: [WinError 2] 系统找不到指定的文件。 During handling of the above exception, another exception occurred: Traceback (most recent call last): File "D:\PyCharm Community Edition 2022.1.3\plugins\python-ce\helpers\pydev\pydevd.py", line 1491, in _exec pydev_imports.execfile(file, globals, locals) # execute the script File "D:\PyCharm Community Edition 2022.1.3\plugins\python-ce\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile exec(compile(contents+"\n", file, 'exec'), glob, loc) File "C:/Users/Administrator/PycharmProjects/pythonProject4/main.py", line 3, in <module> print(pytesseract.get_tesseract_version()) File "C:\Users\Administrator\PycharmProjects\pythonProject4\venv\lib\site-packages\pytesseract\pytesseract.py", line 146, in wrapper wrapper._result = func(*args, **kwargs) File "C:\Users\Administrator\PycharmProjects\pythonProject4\venv\lib\site-packages\pytesseract\pytesseract.py", line 395, in get_tesseract_version raise TesseractNotFoundError() pytesseract.pytesseract.TesseractNotFoundError: tesseract is not installed or it's not in your PATH. See README file for more information. Process finished with exit code 1该如何处理
最新发布
07-20

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值