Hi,so I wrote the code as given below, but can't get it to work because it gives me an Error. File not found meaning that argc != 2 . Thereof I have a question. What is wrong in my code and why argc != 2 is evaluated to other value and not 2? If that was the case I would have solved the problem of the exercise.
The problem for the exercise is this: In this challenge, given the name of a file, print out the size of the file, in bytes.
Have you tried printing out the value of argc? What value does it have? Have you tried printing out the elements of the argv[][] array? How are you calling the function?
I simply compile the code by g++ and then ask for the output in the terminal with ./a.out.
Then since you didn't supply any command line arguments when you ran the program you will not have two arguments passed into your program. Try supplying a command line argument when you run the program.
@jib
I supplied the command line argument, but still get the error from argc != 2 .
When I run ./a.out filename.txt I get "Could not open the file". I still can't get the program to calculate the size of the file in bytes!
@kemort
How can I run the program via terminal, shell, console? I'm running it on Mac. How to put exe file and test text in the same directory?
Yeah I have a new Mac and I thought it would be simple but those were famous last words. On a pc it is as simple as I've said before and that is consistent with the website setting the problem.
With a Mac using the terminal the best I can get so far is open count 7, but the trouble is the argument isn't read. argc is always 1 and argv[0] is the only argument used. open count --args 7 makes no difference.
Sorry it wasn't as simple as I thought. Compare notes later maybe :(
I supplied the command line argument, but still get the error from argc != 2 .
When I run ./a.out filename.txt I get "Could not open the file".
This is not the error from argc != 2, it is from the second if statement the file doesn't open. If filename.txt is not in the same directory as a.out the file won't open.
Major breakthrough - I think. At least it works perfectly as planned/programmed on my system. I have a small factorial program prepared using C++ via XCode under OSX. factorial takes one argument. (0 and 1 so, argc = 2)
1. Compile and run the program as normal in XCode (no parameters involved)
2. Click on the executable in XCode (btm LHS) and select 'Show in Finder'
3. Copy the file across to the root directory. (Purely a convenience to avoid path name crap)
4. Open Terminal go to root directory, type ls and make sure factorial is there.
5. Now, type ./factorial 5 followed by a return
6. Bingo, I get argc = 2 (tick), then a few intermediate numbers I included for diagnostic purposes and the number 120 (tick)
I've yet to fathom UNIX stuff as it's been a long time since last visit so ./ is a bit of a mystery at the moment. But it worked! To show it wasn't a random walk ...