0

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 <stdio.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?

2 Answers 2

6

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Heh, I'm glad I'm not alone in believing it's slightly more efficient (but doesn't matter).
Thanks. It worked. I do eventually mean to pipe it to an awk script, once I get the C program running
3
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

1 Comment

lol...yeah. Just practicing C. I'm so used to programming in C#, C is killing me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.