Thread: Invalid Page Faults with Command Line Args

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    42

    Invalid Page Faults with Command Line Args

    Hello. I was making a command line xor encryption tool (mainly for fun), and I did most of it pretty good. But when I compile it (without any errors or warnings), and then run it with the nessesary arguments, it throws a invalid page fault at me. :(

    Well here is a snippet of the code (a bit large to post the entire thing...):

    Code:
    void fSyntax()
    {
    	cout << "\nThe correct syntax is: <filename> <password> [--decrypt]\n";
    	exit(1);
    }
    
    int main(int argc, char *argv[])
    {
    	int i;
    	if((argc < 2) || (argc > 3))
    		fSyntax();
    	while( i < argc )
    	{
    		if(!strcmp(argv[argc],"--decrypt"))
    		{
    			fstream FileD(argv[1], ios::in | ios::out | ios::nocreate);
    			if(!FileD)
    				exit(1);
    			fDecrypt(FileD, argv[2]);
    			FileD.close();
    			exit(1);
    		}
    		i++;
    	}
    	fstream FileE(argv[1], ios::in | ios::out | ios::nocreate);
    	if(!FileE)
    		exit(1);
    	fEncrypt(FileE, argv[2]);
    	FileE.close();
    	return 0;
    }
    Now what do I do? :p

    I'm thinking it has something to do with the while() loop, but I can't be sure on that.

    Well, thanks for your help.
    Sigh, nothing ever works the first try.

    Register Linux User #314127

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    if(!strcmp(argv[argc]
    argv is an array that it argc elements long -- argv[0] to argv[argc-1].

    argv[argc] is an out of bounds index.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>argv[argc] is an out of bounds index.
    In normal array terms it would be out of bounds, but for command line args, argv[argc] is a NULL pointer.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    42
    Aha, i fixed it.

    Code:
    int i;
    was supposed to be

    Code:
    int i = 0;
    Last edited by KneeLess; 09-28-2003 at 07:32 PM.
    Sigh, nothing ever works the first try.

    Register Linux User #314127

  5. #5
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Originally posted by Hammer
    >>argv[argc] is an out of bounds index.
    In normal array terms it would be out of bounds, but for command line args, argv[argc] is a NULL pointer.
    Good catch, I forgot that one.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by KneeLess
    Aha, i fixed it.

    Code:
    int i;
    was supposed to be

    Code:
    int i = 0;
    ... which is why a for loop would have been a better choice, imo. It makes mistakes like that easier to see.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 04:35 PM
  2. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Whats the deal with page faults?
    By Deo in forum Game Programming
    Replies: 5
    Last Post: 06-10-2005, 10:13 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM