将标准输入重定向到文件 [英] redirecting stdin to file

查看:74
本文介绍了将标准输入重定向到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个程序,将我在终端中输入的任何内容写入文件.这是我写的代码.

I want to write a program that takes whatever I type in terminal and writes it to file. Here is the code that I wrote.

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int fd[2];
void pri(){
   char a[10];
   int ad=open("t.txt",O_CREAT | O_APPEND | O_NONBLOCK |  O_RDWR, 0644);
   if(read(fd[0],a,10)>0){
       write(ad,a,10);
   }   
}
int main()
{
   int a;
   char s[10];
   pipe(fd);
   while(read(0,s,10)>0){
      write(fd[1],s,10);
      pri();
   }
   return 0;
}

目前我正在使用数组和管道来实现这一点.有什么方法可以在不使用任何数组的情况下实现相同的目标吗?

Currently I am using arrays and a pipe for achieving this. Is there any way that I can achieve the same without using any arrays?

推荐答案

我想编写一个程序,将我在终端中输入的任何内容写入文件.

I want to write a program that takes whatever I type in terminal and writes it to file.

这实际上非常简单,您不需要为此使用管道.(您的应用程序本身扮演管道的角色.)

That's actually very simple and you don't need a pipe for this. (Your application itself takes the role of the pipe.)

这是我为了证明这一点所做的:mycat.c

This is what I did to demonstrate this: mycat.c

#include <stdio.h>

int main(int argc, char **argv)
{
  if (argc < 2) {
    fprintf(stderr, "ERROR: No output file!\n");
    fprintf(stderr, "Usage: mycat FILE\n");
    return 1;
  }
  FILE *fOut = fopen(argv[1], "w");
  if (!fOut) {
    fprintf(stderr, "ERROR: Cannot open file '%s' for writing!", argv[1]);
  }
  int c;
  while ((c = getc(stdin)) >= 0) {
    if (putc(c, fOut) < 0) {
      fprintf(stderr, "ERROR: Cannot write to file '%s'!", argv[1]);
    }
  }
  if (fclose(fOut)) {
    fprintf(stderr, "ERROR: Cannot write to file '%s'!", argv[1]);
  }
  return 0;
}

它从stdin 中读取一个字符,并将其写入之前由fopen() 打开的文件流fOut.重复此操作,直到 getc() 失败,这可能会发生,例如由于输入结束.

It reads a character from stdin and writes it to the file stream fOut which has been opened by fopen() before. This is repeated until getc() fails which might happen e.g. due to end of input.

Cygwin/Windows 10 上的 bash 示例会话:

Sample session in bash on Cygwin/Windows 10:

$ gcc --version
gcc (GCC) 6.4.0

$ gcc -std=c11 -o mycat mycat.c

$ ./mycat
ERROR: No output file!
Usage: mycat FILE

$ ./mycat mycat.txt
Hello World.
tip tip tip

此时,我输入了 Ctrl+D 以表示 bash 输入结束.

At this point, I typed Ctrl+D to signal bash the end of input.

$ cat mycat.txt
Hello World.
tip tip tip

$

我用cat输出mycat.txt的内容.这是之前输入的内容(如预期).

I used cat to output the contents of mycat.txt. It is what typed in before (as expected).

cat 实际上是我阅读问题时的第一个想法,但后来我想:这是一个标记为 (不是 ).因此我的 C 示例代码.

cat was actually my first idea when I read the question but then I thought: It's a question tagged c (not bash). Hence my C sample code.

为了完整性,与 cat 相同:

For completeness, the same with cat:

$ cat >mycat.txt <<'EOF'
> Hello cat.
> key key key
> EOF

$ cat mycat.txt
Hello cat.
key key key

$

这让我想起了 <<'EOF' 是由 bash 解释的东西.因此,以下也适用:

This remembered me that <<'EOF' is something interpreted by bash. Hence, the following works as well:

$ ./mycat mycat.txt <<'EOF'
Hello World.
tip tip tip
EOF

$ cat mycat.txt 
Hello World.
tip tip tip

$ 

这让我相信 cat 的工作原理非常相似,尽管它将输入文件作为参数并写入 stdout(这反过来可能是重新- 在 shell 中调用它时定向).与我的相反,如果没有提供参数,cat 不会失败 –它从 stdin 读取.

This let me belief that cat works quite similar though it takes input file(s) as argument(s) and writes to stdout (which in turn might be re-directed when calling it in a shell). In opposition to mine, cat doesn't fail if no arguments are provided – it reads from stdin instead.

这篇关于将标准输入重定向到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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