command line arguments question
Hey guys im just playing around with this program which counts comments from source files such as .h or .c files. How would i pass command Line Arguments when i run the program.
Iv done some passing but it dosnt seem to work im not sure if im passing the arguments correctly when i run it?
Code:
#include <stdlib.h>
#include <stdio.h>
#include "myprogexam.h"
#include <string.h>
#include <ctype.h>
#define TRUE 1
#define FALSE 0
int main(int argc, char **argv)
{
int numComments;
if(argc !=2)
{
printf("not enough arguments passed!\n");
return EXIT_FAILURE;
}
if(argc >3)
{
printf("Too many arguments:\n");
exit(0);
}
numComments = displayComments(argv);
return EXIT_SUCCESS;
}
int displayComments(char **argv)
{
FILE *fp; /* fp point to a file*/
int currentChar, previousChar;
int notYetCommentLine, blanksOnlySoFar;
int numComments, i;
char * suffix[] = {".h", ".c", NULL};
char * filename;
/* allocate dynamic space for the file*/
filename = malloc(sizeof(char) * (strlen(*argv) + 3));
/* check if the filename exists*/
if(!filename)
{
printf("Something went wrong!\n");
return -1;
}
printf("hello\n");
/* count the comments in each file*/
for(i=0; suffix[i]; i++)
{
strcpy(filename, *argv);
strcat(filename, suffix[i]);
fp = fopen(filename, "r");
if(fp)
{
previousChar = '\0';
blanksOnlySoFar = TRUE;
notYetCommentLine = TRUE;
while((currentChar=fgetc(fp)) !=EOF)
{
if(currentChar == '/' && previousChar == '/' &&
blanksOnlySoFar && notYetCommentLine)
{
notYetCommentLine = FALSE;
numComments++;
putchar('/');
}
else if (currentChar == '\n')
{
if(!notYetCommentLine) putchar('\n');
notYetCommentLine = TRUE;
blanksOnlySoFar = TRUE;
}
else if (!isspace(currentChar) && (currentChar != '/'))
{
blanksOnlySoFar = FALSE;
}
if (!notYetCommentLine) putchar(currentChar);
previousChar = currentChar;
}
fclose(fp);
}
}
free(filename);
return numComments;
}