command line arguments using argc and argv.

This is a discussion on command line arguments using argc and argv. within the C Programming forums, part of the General Programming Boards category; hello all, I am trying to write a program where I can read info from a data file into my ...

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    3

    command line arguments using argc and argv.

    hello all,
    I am trying to write a program where I can read info from a data file into my program and write info to another data file using argc and argv functions.
    this is what i have so far and am not sure it is correct.

    main( int argc, char* argv[] )
    {
    char record [37];
    int i;
    FILE *input;
    FILE *output;
    input=fopen(argv[1],"r");
    output=fopen(argv[2],"w");

    printf("Number of files to be printed: %i\n", argc -1);

    for (i=1; i<argc; i++)
    {
    printf("\n\tFile:\t%s\n",argv[i]);
    input=fopen(argv[i],"r");

    while (fgets(record,37,input) != NULL)
    printf("%s",record);

    fclose(input);
    fclose(output);
    }
    return(0);
    }

    (ps my input file has about 30 characters in it on 4 seperate lines).

    Any input from any of you guys would be much appreciated, as I dont think this code I wrote is completely correct.
    thank you
    Last edited by Cudarich; 12-05-2004 at 01:45 AM.

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >input=fopen(argv[1],"r");
    >output=fopen(argv[2],"w");
    This really doesn't do too much. The first thing you do is reassign input to another return from fopen, and you never use output except to break the program by fclosing it multiple times.

    It looks like you want to write your own version of cat:
    Code:
    #include <stdio.h>
    
    int main(int argc, char *argv[])
    {
      char buffer[BUFSIZ];
    
      if (argc < 2) {
        /* No input files, take input from stdin */
        while (fgets(buffer, sizeof buffer, stdin) != NULL)
          fputs(buffer, stdout);
      }
      else {
        FILE *in;
        int i;
    
        for (i = 1; i < argc; i++) {
          if ((in = fopen(argv[i], "r"))== NULL)
            continue;
    
          while (fgets(buffer, sizeof buffer, in) != NULL)
            fputs(buffer, stdout);
        }
      }
    
      return 0;
    }
    Of course, getting the basic functionality to work isn't the fun part. The fun part is adding all of the switch handling and nifty options that cat supports.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help getting a grib on argc and argv
    By elwad in forum C Programming
    Replies: 10
    Last Post: 05-01-2009, 10:13 AM
  2. Using argc and argv data
    By Rad_Turnip in forum C Programming
    Replies: 4
    Last Post: 03-31-2006, 05:09 AM
  3. Converting a argc / argv construct into a va_list
    By chambece in forum C Programming
    Replies: 6
    Last Post: 07-03-2005, 04:47 PM
  4. argc & argv
    By miryellis in forum C Programming
    Replies: 11
    Last Post: 09-19-2004, 11:59 PM
  5. how do i? re: argc - argv
    By luigi40 in forum C Programming
    Replies: 2
    Last Post: 06-11-2004, 10:17 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21