Thread: Help on argc, argv program

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    13

    Question Help on argc, argv program

    I was wondering if you’ s could help me out with this program that I am unsure how to approach it as im only new to command line arguments. The program should open a file, read a single line of text from the file, count, display number of words in the line of text and print if the line of text is a palindrome or not (palindrome is a string that is spelt the same way forwards as backwards, example = radar). If no file is passed as a CommLineArg, an error will be displayed. Also, im assuming that only type of file passed as an argument is a text file with .txt extension. I have wrote a piece of code attached, unsure of what to do exactly, any help would be great, thanks.

    #include <stdio.h>

    int main (int argc, char *argv[])
    {
    int i;

    printf("Argc is %d\n", argc);

    for(i=0; i<argc; i++)
    printf("Argv[%d] contains %s\n", i, argv[i]);

    if(argc==1)
    printf("NO FILE SPECIFIED...");

    return 0;
    }

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    Well, you're on the right way. I see that you understand that the argv is an array of strings. The first string is the program's name and the second string is the name of the file to open, this string you can use to open the file with fopen.

    You can use strcmp (string compare) to check if the extension of the filename passed as argument is ".txt".

    Hope these hints help you some further.

  3. #3
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    To open the file, you would just use fopen and have type FILE *. Something would be like this:
    Code:
    char line[512];
    FILE *fp;
    
    fp = fopen(argv[1], "r");
    fgets(line, sizeof(line), stdin);
    Now, you have the first line of the file and do whatever you want with it. Like Shiro said, argv[0] is the program name (with the dir included) and the argv[1] starts what you would include with the exe.
    1978 Silver Anniversary Corvette

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    13

    cheers

    k, thanks lads helps

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  2. ARGH! (argc and argv too!) HD Space!!!
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 12-05-2001, 04:05 AM
  3. argh! (argc and argv too!)
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 11-11-2001, 04:01 PM
  4. command line program, something wrong in the argv
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 09-26-2001, 09:36 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM