views:

257

answers:

3
+5  Q: 

Beginner Doing K&R

Hi, I'm just starting programming and going through K&R to try and learn C. I've gotten to the section on command line arguments (5.10) but now I'm stumped. Every time I try and open a program I've written with command line arguments I'm told that file X, X being the argument, doesn't exist.

`gcc -o find find.c

open find test

The file /Documents/Learning_C/test does not exist.`

Any suggestions? Thanks

+3  A: 

What system are you on? In Unix/Linux you compile & run your executable via:

gcc -o find find.c
./find test

As others have noted, when you prefix your binary with "./", there wont be any naming conflicts. However, if you made find accessible in your $PATH, you might have some conflicts with find and test--standard programs with most *nix distributions... Maybe you could choose more specific names (i.e. ./myFind testArg)

Pete
The prefix "./" *is* important, at least on Linux, as the current folder "." is usually not part of the PATH variable, thus the system won't find the binary. If the executable you produced has the same name as a standard program on you system (like "test") leaving out "./" will use the system program, not the one you compiled.
bluebrother
Pete
@Pete, thanks for slurping in the answers that others provided. First with the name clash with the system command find and then with the use of ./ to dereference. Check the edits ladies and gents...
Rob Wells
actually by "others" I really meant my answer and my edits to clarify!
Rob Wells
+1  A: 

Try giving your output executable a different name.

I suspect your executing the system find command which is looking for a directory called 'test'.

Or try forcing it by executing

./find toto

Edit: Prepending the ./ to the command is important because it tells the shell to execute the find in the current directory as opposed to the first 'find' that exists in your PATH. It is normally recommended that you don't have . (the current directory) in your PATH for security reasons.

HTH

P.S. Forgot to say good one for working through K&R. I just finished doing the same after working in C for thirty years and it was good to get back and refresh the mind!

Rob Wells
A: 

Instead of making us all individually guess what exactly you're doing wrong, perhaps you should paste the program you're using for the illustration mentioned ?

blacky
this should probably be a comment..
Neeraj
Thanks for the tip.
blacky