Thread: This compiles but gives error

  1. #1
    Registered User
    Join Date
    May 2017
    Posts
    31

    This compiles but gives error

    This is a program i coded which parses command line arguments which are expected to be text files and in turn send contents to STDOUT which is screen.

    it compiles but doesn't work. although it compiles. sinces its run from the commandline, i dont know how to debug such programs. can anyone tell me how to do this? i mean how to debug command line arguments.

    this is the source code for the program;

    Code:
    #include <stdio.h>
    
    
    void filecopy(FILE *, FILE *);
    int main(int argc, char *argv[]){
        FILE *fp;
        if (argc == 1){
            filecopy(stdin, stdout);
        }
        else
           while (--argc > 0)
                if ( (fopen(*++argv, "r")) == NULL ){
                    printf("could not open file: %s", *argv);
                    continue;
                }
                else {
                    filecopy(fp, stdout);
                    fclose(fp);
                }
    
    
        return 0;
    
    }
    
    
    void filecopy(FILE *ifp, FILE *ofp){
        int c;
        if (ifp == NULL || ofp == NULL)
            return;
        else
            while ( (c = getc(ifp)) != EOF)
            putc(c, ofp);
    }

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,068
    I have never seen good code that decremented argc.
    Or incremented argv.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    Quote Originally Posted by stahta01 View Post
    I have never seen good code that decremented argc.
    Or incremented argv.
    You need to get out more! It's extremely common, and perfectly legal, of course.

    Anyway, OP, you forgot to assign the file pointer to fp when opening the files.

  4. #4
    Registered User
    Join Date
    May 2017
    Posts
    31
    Quote Originally Posted by algorism View Post
    You need to get out more! It's extremely common, and perfectly legal, of course.

    Anyway, OP, you forgot to assign the file pointer to fp when opening the files.
    except i don't understand what you mean, but i believe this line assigns the file pointer to fp

    Code:
    if ((fp = fopen(*++argv, "r")) == NULL) {

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    4,068
    Quote Originally Posted by thagulf2017 View Post
    except i don't understand what you mean, but i believe this line assigns the file pointer to fp

    Code:
    if ((fp = fopen(*++argv, "r")) == NULL) {
    That line was NOT in the code you posted.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  6. #6
    Registered User
    Join Date
    May 2017
    Posts
    31
    Code:
    #include <stdio.h>
    
    
    void filecopy(FILE *, FILE *);
    int main(int argc, char *argv[]){
        FILE *fp;
        if (argc == 1){
            filecopy(stdin, stdout);
        }
        else
          while(--argc > 0)
            if ((fp = fopen(*++argv, "r")) == NULL) {
                printf("cat: can't open %s\n", *argv);
                return 1;
            } else {
                filecopy(fp, stdout);
                fclose(fp);
            }
            return 0;
    }
    
    
    void filecopy(FILE *ifp, FILE *ofp){
        int c;
        if (ifp == NULL || ofp == NULL)
            return;
        else
            while ( (c = getc(ifp)) != EOF)
            putc(c, ofp);
    }
    this is complete code and still doesn't wokr

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    4,608
    It seems to work for me.

    How exactly are you calling the function?

    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. why this warning's? but compiles
    By c_lady in forum C Programming
    Replies: 3
    Last Post: 03-14-2010, 01:58 PM
  2. Compiles on gcc 3.3 but not on gcc 4.0.3
    By cunnus88 in forum C++ Programming
    Replies: 5
    Last Post: 03-29-2007, 12:24 PM
  3. Compiles okay, but doesn't run.
    By karlawarla in forum C++ Programming
    Replies: 20
    Last Post: 11-23-2006, 04:03 PM
  4. Replies: 4
    Last Post: 09-16-2006, 07:11 PM
  5. code compiles without error, but
    By kashifk in forum C Programming
    Replies: 9
    Last Post: 04-11-2003, 08:07 AM

Tags for this Thread