/* a comment here */since they consist of comments only, but include these lines:
/* a multi-line comment
that continues here */
printf("hello\n"); /* a comment */since they contain code in addition to comments.
/* a comment */ printf("hello\n");
Hint: You can do your own parsing for this task, or see CountCLines.java (with sample input file CountCLines.IN) for an example of how to set up a StreamTokenizer to accomplish the desired parsing.
Comments in Fortran source are much easier to recognize: A line that has a C or a c as its first character is a comment line (that is, the whole line is a comment). (For those of you familiar with newer versions of Fortran: Yes, this means we're assuming our Fortran source is in the older fixed format rather the newer free format that allows other forms of comments.)
Suppose we have a directory TestDir containing:
Then the following is a possible execution of our program (user input is shown in boldface), in directory TestDir:
java FilesInfo Test.OUT EmptySub Sub cpgm.c empty.txt otherfile fpgm.f tfile.txt qwertyuiopwhich writes the following to the specified output file Test.OUT:
Adding EmptySub to list of files
Adding Sub to list of files
Adding cpgm.c to list of files
Adding empty.txt to list of files
Adding fpgm.f to list of files
Adding otherfile to list of files
Adding tfile.txt to list of files
qwertyuiop not found or not readable
Options are:
S to get file sizes
L to count lines in files
X to count lines in files excluding whitespace, comments
C to get file contents
Q to end
Enter option: (S, L, X, C, or Q)
S
printing sizes
Enter option: (S, L, X, C, or Q)
C
printing contents
Enter option: (S, L, X, C, or Q)
xx
Invalid option
Options are:
S to get file sizes
L to count lines in files
X to count lines in files excluding whitespace, comments
C to get file contents
Q to end
Enter option: (S, L, X, C, or Q)
S
printing sizes
Enter option: (S, L, X, C, or Q)
L
printing line counts
Enter option: (S, L, X, C, or Q)
X
printing line counts excluding whitespace, comments
Enter option: (S, L, X, C, or Q)
Q
**Sizes** for EmptySub: 512 for Sub: 512 for cpgm.c: 236 for empty.txt: 0 for fpgm.f: 75 for otherfile: 7 for tfile.txt: 141 **done** **Contents** for EmptySub: list of files in directory (empty directory) for Sub: list of files in directory f1 f2 for cpgm.c: text /* here's a comment that starts a line */ /* here's another */ #include "stdio.h" /* here's another comment */ void /* and here's one */ main(int argc, int argv[]) { /* here's one more */ (void) printf("hello, world\n") ; } for empty.txt: text (empty file) for fpgm.f: text c my program program fpgm C another comment print*, "hello, world" end for otherfile: ?? unable to display contents (unknown file type) ?? for tfile.txt: text some text next line is just tabs next line is just blanks next line is mixed tabs and blanks next line is empty more text the end **done** **Sizes** for EmptySub: 512 for Sub: 512 for cpgm.c: 236 for empty.txt: 0 for fpgm.f: 75 for otherfile: 7 for tfile.txt: 141 **done** **Line counts** for EmptySub: ?? (directory) for Sub: ?? (directory) for cpgm.c: 14 for empty.txt: 0 for fpgm.f: 6 for otherfile: ?? (unknown file type) for tfile.txt: 11 **done** **Line counts excluding whitespace, comments** for EmptySub: ?? (directory) for Sub: ?? (directory) for cpgm.c: 5 for empty.txt: 0 for fpgm.f: 3 for otherfile: ?? (unknown file type) for tfile.txt: 7 **done** **End of user input**Note that the above is for execution on a Unix system. Results on a DOS-based system should be identical, except for file and directory sizes. Directory sizes are non-zero multiples of 512 under Unix, 0 (I believe) under Windows; file sizes may depend on what the system uses as a line separator. What you are to print is what's returned by the length() method of the File class. Output may thus look a bit different on different systems, but this shouldn't be a concern -- when I test your program, I'll test it on the same system I use for preparing the expected output, so all should be well. Also, conventions for specifying pathnames may be different on different systems, but again this should not be a concern, for the same reason.
NOTE: For this program, I'm concerned only with what your program writes to its output file. So given the inputs above, your program should produce an output file identical to Test.OUT above. What it writes to standard output, however, is up to you; you can consider the standard-output part of the above dialog a guideline only.