Files In C

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dharanidhar
    New Member
    • Apr 2008
    • 7

    Files In C

    #include <stdio.h>
    void main()
    {
    char n;
    FILE *fp;
    fp = fopen("fileexam ple.c","r");
    while ((n=getchar(fp) )!=EOF)
    putchar(n);

    return;
    }


    whats the problem with above code?
    itsgiving error as wrong arguments to getchar()
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by dharanidhar
    #include <stdio.h>
    void main()
    {
    char n;
    FILE *fp;
    fp = fopen("fileexam ple.c","r");
    while ((n=getchar(fp) )!=EOF)
    putchar(n);

    return;
    }


    whats the problem with above code?
    itsgiving error as wrong arguments to getchar()
    - the return type of function main() is int, not void;
    - getchar() reads a char from stdin and doesn't take an argument; consider fgetc() or getc() instead;
    - check the return value of the fopen() call;
    - EOF is outside the range of possible char values;
    - read the library specification before you attempt to code.

    kind regards,

    Jos

    Comment

    Working...