读取C()标准输入? [英] read() stdin in c?

查看:204
本文介绍了读取C()标准输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有涉及的read()命令快速小问题。

I have a quick little question involving the read() command.

我用C真的生锈,不幸的是,我现在的任务有我在编程C.
我们需要利用读()和不fgets和等

I am really rusty in C, and unfortunately, my current assignment has me programming in C. We need to read stdin using the read() and not fgets and the like.

所以我有一个简单的while循环:

So I have a simple while loop:

int n, i;
char buffer[257], *input;

while((n=read(fileno(stdin), buffer, 256)) > 0)
                {
                        buffer[n] ='\0';
                        if(buffer[n] = '\n') break;
                        write(input, buffer, strlen(buffer));
                }

所以,我不知道如何在进入的preSS(行尾)这个循环停止所以这就是为什么我有突破code,但我如果这是正确完成不太了解。

So I am not sure how to make this loop stop at the press of enter (end of line) so that's why I have the break code, though I don't know if that's done correctly.

我想实现的总体目标是把从标准到指针输入

The whole objective I am trying to accomplish is to put the input from stdin into the pointer 'input'

(我真的不善于理解指针所以我:)承担)

(I am really bad at understanding pointers so bear with me :) )

我的问题是,我得到分段错误,当我preSS进入。
我希望我能使用与fgets因为那时这一切都将用一个简单的解决

My problem is that I am getting segmentation faults when I press enter. I wish I could use fgets because then all this would be solved with a simple

input = fgets(buffer, 256, stdin);

混账功课:(
不管怎么说,如果你们可以点我在正确的方向,我真的AP preciate它。谢谢!

Darn homework :( Anyways, if any of you could point me in the right direction, I would really appreciate it. Thank you!

推荐答案

您要去了解这一切是错误的。首先,是的用来写入打开文件描述符系统调用。你的输入是不一样的 - 文件描述符是通过首先调用打开系统调用或引用一个获得在始终打开的文件,如: 标准输入标准输出标准错误。更何况,一个文件描述符是 INT 在Linux上。

You're going about it all wrong. Firstly, write is a system call that's used to write to a open file descriptor. Your input is nothing like that - a file descriptor is gained by calling the open syscall first, or referring to one of the always-open files, e.g. stdin, stdout or stderr. Not to mention that a file descriptor is an int on Linux.

此外,你应该记住,你的输入对标准输入以换行符结束的假设并不一定是正确的所有的时间。您的程序可能会在标准输入一些内容,不包含任何新行,对一些文件的内容,例如(重定向到标准输入),或者从键盘输入的可能只是结束以按Ctrl-D (EOF),而不是返回键。

Also, you should remember that the assumption of your input on stdin ending with a newline doesn't have to be right all the time. Your program may receive some content on standard input that doesn't contain any newlines, for example contents of some file (redirected to stdin), or the input from keyboard may simply end with a Ctrl-D (EOF) instead of the "return" key.

在最严重的是,未初始化的指针没有指向任何有效的内存。声明的char *输入不给你写的任何权利/由输入除非读提到的记忆你分配一些内存,这个指针将指向第一。

On top of all that, an uninitialized pointer doesn't refer to any valid memory. The declaration of char *input doesn't give you any right to write/read the memory referred to by input unless you allocate some memory that this pointer will point to first.

用C复制串是用在&LT声明的函数实现;文件string.h> 。他们在C-字符串,例如操作通过终止\\ 0字符序列。在这种情况下,阅读()不通过 \\ 0 ,在这种情况下,你会终止其输出要使用的memcpy 函数。你为什么不干脆尝试直接读入输入有关系吗?它没有多大意义,读缓存首先,简单地将数据复制到内存的另一块以后。

Copying strings in C is achieved by using functions declared in <string.h>. They operate on C-strings, e.g. sequences of characters terminated by \0. In this case, read() doesn't terminate its output by a \0, in which case you'll want to use the memcpy function instead. Why don't you simply try reading directly into input, though? It doesn't make much sense to read to buffer first, simply to copy the data to another chunk of memory later on.

此外,的规格读指出,它读取的最多的计数从文件描述符字节。这意味着,如果你的缓存声明为的char [257] ,那么你应该叫阅读(标准输入,缓冲,257),而不是256的话,大概也更适合于简单地声明缓冲区为256个元素的数组。

Also, the specification of read states that it reads up to count bytes from the file descriptor. Which means that if your buffer is declared as char[257], then you should call read(stdin,buffer,257), not 256. It would probably also be more suitable to simply declare the buffer as a 256-element array.

我希望我已经理顺了一些东西给你。

I hope that I've straightened out some stuff for you.

这篇关于读取C()标准输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆