开发者

Piping output of C program to file (bash)

开发者 https://www.devze.com 2023-01-19 09:39 出处:网络
I\'m just trying my hand in bash, so I wrote a simple program in C to count the number of characters in a file.

I'm just trying my hand in bash, so I wrote a simple program in C to count the number of characters in a file.

This is my C program:

#include <stdi开发者_开发技巧o.h>

int main()
{
  int i, nc;
  nc = 0;
  i = getchar();
  while (i!=EOF){
    nc = nc + 1;
    i = getchar();
  }
  printf("%d\n",nc);

  return 0;
}

This is the bash command I'm using to compile and execute:

gcc sign.c < bright_side_of_life > output

My output file is completely empty though. Where am I going wrong?


You have to compile first, then run:

gcc -o sign sign.c
./sign < bright_side_of_life > output

Also, technically this is not piping the output of the program to a file; it is simply redirecting it. If you really wanted a pipe involved, you'd probably go in for some 'feline abuse' (meaning, use the 'cat' command):

./sign < bright_side_of_life | cat > output

However, the I/O redirection is more normal and (though it really doesn't matter in this context) more efficient.


gcc sign.c -o output
./output < bright_side_of_life > size.txt

and i hope you are actually practising your C language. otherwise, just use the wc tool

0

精彩评论

暂无评论...
验证码 换一张
取 消