I am new to C. I won't to understand how this program works. I found it and don't understand how to run it. When I do it says:
Missing name for redirect. Here is the code.

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

int main(int argc, char *argv[])
{
  FILE *from, *to;
  char ch;


  if(argc!=3) {
    printf("Usage: copy <source> <destination>\n");
    exit(1);
  }

  /* open source file */
  if((from = fopen(argv[1], "rb"))==NULL) {
    printf("Cannot open source file.\n");
    exit(1);
  }

  /* open destination file */
  if((to = fopen(argv[2], "wb"))==NULL) {
    printf("Cannot open destination file.\n");
    exit(1);
  }

  /* copy the file */
  while(!feof(from)) {
    ch = fgetc(from);
    if(ferror(from)) {
      printf("Error reading source file.\n");
      exit(1);
    }
    if(!feof(from)) fputc(ch, to);
    if(ferror(to)) {
      printf("Error writing destination file.\n");
      exit(1);
    }
  }

  if(fclose(from)==EOF) {
    printf("Error closing source file.\n");
    exit(1);
  }

  if(fclose(to)==EOF) {
    printf("Error closing destination file.\n");
    exit(1);
  }

  return 0;
}

Well, first of all if you are new to C that's not a good way to start. You should learn the basics first and study a bit until you reach that level. Me myself, I havent reached that level yet, and assuming you didnt write the program since you are new to C, then I dont see the point for you to know how that works or what's wrong with the code. Unless you arent learning and you need to have that working for whatever reason.

Good luck.

Comments
I find myself believing him.

>I found it and don't understand how to run it
It runs in the command line and expects three arguments: The name of the program <space>the file name to be copy<space>the file name to be created as a copy.

Mind that this is not a particular well written program.

when we give values at run time, the compailer take it as a file. here the argc is the no of words in the input given and argv is a pointer. the pointer takes each word as 'while loop increments'.

>I found it and don't understand how to run it. When I do it says...

OK, this program can't print this message so I suspect that you have tried to print the 1st line of this text as a command string ;)

You don't understand how to run it but you do that...
How do you do that? ;)

I am new to C. I won't to understand how this program works. I found it and don't understand how to run it. When I do it says:
Missing name for redirect. Here is the code.

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

int main(int argc, char *argv[])
{
  FILE *from, *to;
  char ch;


  if(argc!=3) {
    printf("Usage: copy <source> <destination>\n");
    exit(1);
  }

  /* open source file */
  if((from = fopen(argv[1], "rb"))==NULL) {
    printf("Cannot open source file.\n");
    exit(1);
  }

  /* open destination file */
  if((to = fopen(argv[2], "wb"))==NULL) {
    printf("Cannot open destination file.\n");
    exit(1);
  }

  /* copy the file */
  while(!feof(from)) {
    ch = fgetc(from);
    if(ferror(from)) {
      printf("Error reading source file.\n");
      exit(1);
    }
    if(!feof(from)) fputc(ch, to);
    if(ferror(to)) {
      printf("Error writing destination file.\n");
      exit(1);
    }
  }

  if(fclose(from)==EOF) {
    printf("Error closing source file.\n");
    exit(1);
  }

  if(fclose(to)==EOF) {
    printf("Error closing destination file.\n");
    exit(1);
  }

  return 0;
}

if you are a beginner then how could you bring such a big code...
and if you are to able to brought that then there is no question of "how it works"...
i think you should start with complete reference in C by herbert schildt .......

This article has been dead for over six months. Start a new discussion instead.