View Single Post
Old 08-04-2006, 09:26 PM   #6 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
redhead is on a distinguished road
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.
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.

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);
                        }
		}
...
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...
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001

Last edited by redhead; 08-04-2006 at 09:47 PM.
redhead is offline   Reply With Quote