View Single Post
Old 08-04-2006, 10:45 PM   #7 (permalink)
cn880
Registered User
 
Join Date: Aug 2006
Posts: 5
cn880 is on a distinguished road
Quote:
Originally Posted by redhead
This is not a correction or anything, just a small examination to see if you know what the main function does...
  • Please describe to me, how a valid call of the program would be in order to catch the different checks for:
    • if(optind == 3)
    • if (optind == argc || optind > argc)
    • if (optind > 1 && optind < argc)
    • And the last else section.
To be honest, I don't really understand your question. ): I believe optind is the argv[] index of the first command line argument that isn't a switch option?

Unfortunately, there is a lot about that main function I do not understand. It has become obvious to me that there is a lot more reading and studying to be done before I attempt to write any more programs with any real complexity. C is much more difficult then I thought it would be. I was able to learn HTML, PHP, and CSS with very little effort and thought C would be the same way. Apparently not....

Quote:
Originally Posted by redhead
Now to the correction part...

I see you've removed nearly half the searching through argv[] for the case with optind pointing in to the mittle of argv[]. If you explain the questions I've put up for you, then I know for sure, because from what I see, it seems like you're not entirely aware of what getopt()/optind/optarg does..
Which might be why you've changed my
Code:
if(optind > 1 && optind < argc){
part to
Code:
if (optind == argc && optind < argc) {
So for now I wont comment on that part of your program.
That was merely sloppy typing. Hoping that would memorize the program better, I retyped all of it by hand. I thought that be better then simply cutting and pasting, but it appears as though I have made a few mistakes along they way. ):

Quote:
Originally Posted by redhead
In order to make nice with the OS, you need to close the filepointers you've opened, else they wont be closed untill your app terminates, so if at some point it fails misserably youd end up with alot of unusable files, because they all have unclosed filepointers.
So you need to do somethign like:
Code:
...
		else {
			FILE *success = fopen  (buff, "w");

			if (!success)
				printf ("%s: Error creating file.", buff);
			else {
				printf ("Created file: %s\n", buff);
                                fclose(success);
                        }
		}
...
Oops! I remember that from my first program. I don't know why I forgot it this time around. Next time, I will be sure to remember to fclose() any pointers I fopen()ed. (;

Quote:
Originally Posted by redhead
Another thing, I know this isn't exactly ANSI/ISO compliant since it uses alot of POSIX relevant extensions like getopt(), access(), snprintf(), etc.
But to make nice with the compiler in a ANSI/ISO C -way, you have to declare the variables befor assignment, which is why, for instance, I declare the type of i befor I make any reference to it, or perform any other actions for that matter..
It would be nice to declare your FILE* at the top of your create_file() function. So it would be something like:
Code:
...
int create_file (char* filename, int count) {
        FILE* success;
	char* buff;			/* our filename with dynamic memory 
					   allocation */
...
		else {
			if(!(success = fopen  (buff, "w")))
                                printf ("%s: Error creating file.", buff);
...
If you want to figure out thesse sort of ANSI/ISO compliances, then you can tell gcc, to a strict ANSI/ISO type check with the flag -ansi, note that I did mention this isn't following the ANSI/ISO completely, so it wont compile with that flag... Mostly due to the use of getopt().

And one last thing, the description for char opt; in main() should be a refference to getopt(), not optind.. Sorry that one was partialy my mistake, I had my mind in another part of the program, when I wrote that description...

Else a nice little proggy for a 3.rd day student So welcome to the world of C...
Thank you. (:

Before any examination though, I believe more learning is necessary. What would you do if you were my position and couldn't tell a optind from a getopt()? (; I am a beginning computer science student (have taken an introductory Java class so far) and will most assuredly take a C class if it is offered (especially if it is required for my major, though I understand my university is mostly geared toward C++).

So far, I have read this paper from a university professor and read most of this tutorial. Are there any other links you would recommend for a beginner?
cn880 is offline   Reply With Quote