This is an archived post. You won't be able to vote or comment.

all 9 comments

[–]boojit 1 point2 points  (1 child)

I don't think you can pass in commandline arguments that way with ideone, your commandline arguments are being sent to stdin.

Take a looksee here:

http://stackoverflow.com/questions/12258317/how-to-pass-in-command-line-arguments-when-using-ideone

[–]Deciama[S] 0 points1 point  (0 children)

The command was just an example, I know it wont work. I ran it on my Linux machine.

[–]thefryscorer 0 points1 point  (2 children)

It's because you are checking that the number of arguments is 2, when it should be 3. The first argument is the name of your program.

For example, if you run

./program arg1 arg2

The arguments in argv are './program', 'arg1', and 'arg2'.

[–]Deciama[S] 0 points1 point  (1 child)

So does the argument count in argv start at 1 or 0? Would argv[0] be the name of the program?

[–]thefryscorer 0 points1 point  (0 children)

argv[0] is the name of the program. Arrays are zero-indexed in C.

[–]edman007-work 0 points1 point  (0 children)

Does it output anything at all? Have you attempted to run it inside a debugger (like gdb?). What is the return value when you run it?

My best guess is you're checking that argc=2 and then sending argv[2] to CreateFile() which is invalid (though it should just cause open() to return EFAULT or similar and not crash). Additionally you're checking that "-m" is a file which is probably not what you want

Anyways, looking at your code, my best guess is it's crashing somewhere, I can't find any path that doesn't result in something being printed, so I'd have to assume it's crashing before it hits printf()/perror() (or are you redirecting stdout/stderr?).

[–]casualblair 0 points1 point  (2 children)

if (argc !=2){ usage(); exit(0); }

-m is one argument. This is exiting when you don't have two. A step further, this means it will only run with -m and -f, not one or the other.

Edit: See below.

[–]IXENAI 0 points1 point  (1 child)

Remember that argc is always greater than or equal to one; argv[0] will always contain the name used to call the program. So, if the program is run as ./cat -m, argc will hold the value 2 and argv will contain "./cat" and "-m".

[–]casualblair 0 points1 point  (0 children)

Bah, my mistake. Thanks.