-4

I'm trying to make a function to get file size. I tried using GetFileSizeEx but I couldn't get results that I wanted (I want it to be an integer or double) and to compare if it's larger than 1MB or not. Anyways, I made this function but I am getting an error that filenamestr is not declared.

    int getsize(std::string const $filename);
...    
    int getsize(std::string const $filenamestr)
        {
            std::fstream file(filenamestr);
            file.seekg(0, std::ios::end);
            return file.tellg();
        }

What am I doing wrong?

Krešimir Čulina
  • 93
  • 1
  • 7
  • 19

3 Answers3

2

In C++, you do not preface a variable name with a dollar sign. Removing it should fix your problem:

    int getsize(std::string const filename);
...    
    int getsize(std::string const filenamestr)
        {
            std::fstream file(filenamestr);
            file.seekg(0, std::ios::end);
            return file.tellg();
        }
IanPudney
  • 5,941
  • 1
  • 24
  • 39
  • Yeah, I know, I made a typo and spent the last 30 minutes looking for an answer without noticing it... :/ Got used to PHP Also, I've got another question when this one is resolved anyways, when do I use the "&" sign infront of the string in a function? – Krešimir Čulina Jun 24 '13 at 19:41
  • The `&` has the same meaning it does in PHP - it means "pass by reference" instead of "pass by value." Normally, when you pass a parameter to a function, the function receives a *copy* of the parameter. An `&` changes your function makes to its parameters (for example, if you set `filenamestr=""` inside the function) last only for the duration of the function. Once the function returns, the changes are gone. Using an & means that the function operates on the same copy of the parameter as was passed into it, so any changes the function makes persist even after the function is finished. – IanPudney Jun 24 '13 at 19:45
  • 1
    _"Once the function returns, the variable reverts to its original state."_ This is a terrible way to explain it. The function parameter is a different variable, the original one is not altered, and does not need to revert because it never changed. – Jonathan Wakely Jun 24 '13 at 19:47
  • I know, I was trying to put it in a way that I figured a beginner would understand. But I'll edit my comment. – IanPudney Jun 24 '13 at 19:47
1

Now that I know that you're from PHP, I can see where the confusion is coming from. :)

Parameter names do not need to be prepended with a $. The name itself will just do. When you reference filenamestr, it looks for a variable with that exact identifier. But it doesn't see it because $ is also a valid character for an identifier, and it makes $filenamestr a completely different variable in terms of its name.

David G
  • 94,763
  • 41
  • 167
  • 253
1

The dollar sign $ should not be used in variable names. It is not supported by the C++ standard, although Visual C++ supports it. It's best to remove it from your parameters, but the error is a result of the difference in filenamestr and $filenamestr, two different identifiers.

This will fix it.

int getsize(std::string const filename);
...    
int getsize(std::string const filenamestr)
    {
        std::fstream file(filenamestr);
        file.seekg(0, std::ios::end);
        return file.tellg();
    }
ryanpf
  • 136
  • 5