Thread: command line arguments

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    9

    Angry command line arguments

    i want to write a program which will open a file count the no.of words,lines and character in that file taking the delimiter from the command line arguments.
    here is the program
    Code:
    # include<stdio.h>
    # include<conio.h>
    main(int argc,char*argv[])
    {
    FILE*fp;
    char str[80],ch;int i=0,l=0,len=0;
    int words=0;
    
    fp=fopen(argv[1],"r");
    if(argc!=3)
    {
    puts("insuffiecient no. of parameters");
    exit();
    }
    
    if(fp==NULL)
    {
    puts("unable to open the file");
    exit();
    }
    while((ch=getc(fp))!=EOF)
    { i++;
      if(ch=='\n')
      {
      l++;
      }
    
    }
    printf("the number of characters in the file is %d \n",i);
    printf("the number of lines in the file is %d\n",l);
    rewind(fp);
    while(fgets(str,79,fp)!=NULL)
    {
    len=strlen(str)-2;
    for(i=0;i<len;i++)
    {
    	if(str[i]==*argv[2]&&str[i+1]!=*argv[2])
    	{
    	words++;
    	}
    
    }
    words++;
    }
    fclose(fp);
    printf("the number of words %d",words);
    getch();
    }
    giving the delimiter as "," it gives no. of words=0 and other than , it is giving insufficient no. of arguments.
    here is the text file which i am using

    saurav is great.
    He is the greatest of all,he is good,surely he is.

    i have two commas so it should have shown the no. of words something differend,could anyone please figure out the defect in the programe.Thanks for seeing this thread.
    Last edited by Salem; 07-08-2004 at 06:49 AM. Reason: tagging - but just this once since you're new here

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Why not do all the parsing in one pass?
    Also use [code] tags as they greatly enhance the readibility of the code

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. GradeInfo
    By kirksson in forum C Programming
    Replies: 23
    Last Post: 07-16-2008, 03:27 PM
  3. command line arguments
    By vurentjie in forum C Programming
    Replies: 3
    Last Post: 06-22-2008, 06:46 AM
  4. Replies: 10
    Last Post: 09-27-2005, 12:49 PM
  5. NULL arguments in a shell program
    By gregulator in forum C Programming
    Replies: 4
    Last Post: 04-15-2004, 10:48 AM