File Size Problem

Jul 23, 2016 at 1:34pm
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <fstream>

using namespace std;

int main(int argc, char *argv[]){
streampos begin, end;
if (argc != 2) {cout << "Error. File not found." << endl; return 1;}
else
{
ifstream file_one (argv[1], ios::binary); // It's a filename to open
    if ( !file_one.is_open() ) // Check to see if file opening succeeded
    {
    cout << "Could not open the file" << endl; return 1;
    }
    else
    {
    begin = file_one.tellg();
    file_one.seekg (0, ios::end);
    end = file_one.tellg();
    file_one.close();
    cout << "Size of the file " << argv[1] << " is: " << (end-begin) << " bytes.\n";
    return 0;
    }
}
}
Last edited on Jul 23, 2016 at 2:13pm
Jul 23, 2016 at 2:24pm
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?
Jul 24, 2016 at 3:21am
No, I haven't. I simply compile the code by g++ and then ask for the output in the terminal with ./a.out.
Jul 24, 2016 at 4:51am
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.

./a.out "filename.txt"

Jul 24, 2016 at 5:36am
closed account (48T7M4Gy)
http://www.cplusplus.com/forum/beginner/194622/2/
Jul 24, 2016 at 8:05am
@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?
Last edited on Jul 24, 2016 at 8:12am
Jul 24, 2016 at 10:14am
closed account (48T7M4Gy)
hi aurimas

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 :(
Jul 24, 2016 at 11:30am
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.
Jul 24, 2016 at 11:37am
@jlb

I sent you a PM :+)
Jul 24, 2016 at 12:07pm
@TheIdeasMan
I sent you a PM :+)
Jul 24, 2016 at 12:54pm
closed account (48T7M4Gy)
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 ...

http://stackoverflow.com/questions/15080835/running-an-executable-in-mac-terminal

Jul 24, 2016 at 12:55pm
closed account (48T7M4Gy)
./count filename1 filename2 might just be the answer?
Last edited on Jul 24, 2016 at 1:48pm
Jul 24, 2016 at 4:18pm
@jib

thanks that worked.

@kemort

Thank you for the detailed answer, but it is confusing me. Copy the file across to the root directory? Haven't done that before :)
Last edited on Jul 24, 2016 at 4:23pm
Jul 24, 2016 at 4:23pm
closed account (48T7M4Gy)
Great stuf - another green tick purely for reducing unnecessary revisits if appropriate of course. :)
Topic archived. No new replies allowed.