Frage

I want to retrive files with extension of .jpg from directory in vc++ 08. I'm not much comfortable with pointers so I got error when I try to get length of string in following code. Can anyone help me out??

Here is my code..

#include<iostream>
#include <string>
#pragma warning( disable : 4996 )
#include "dirent.h"
#pragma warning( default : 4996 )
using namespace std;

int main(int argc, char *argv[])
{
if(argc != 2)
{
  cout<<"Usage: "<<argv[0]<<" <Directory name>"<<endl;
  return -1;
}

DIR *dir;
dir = opendir (argv[1]);
if (dir != NULL)
{
    string name; string ext = ".jpg"; int len;

  cout<<"Directory Listing for "<<argv[1]<<" : "<<endl;
  struct dirent *ent;
  while ((ent = readdir (dir)) != NULL)
  {
    name = ent->d_name; 
    len = name.length;                  // here i got error...
    name = name.substr(0, len-4);

    if(_stricmp(name.c_str(), ext.c_str()) == 0)
    {
        cout<<ent->d_name<<endl;
    }
    else
    {
        cout<<"No JPG file"<<endl;
    }
  }
}
else
{
  cout<<"Invalid Directory name"<<endl;
  return -1;
}

return 0;
}

And error is

example.cpp(29) : error C3867: 'std::basic_string<_Elem,_Traits,_Ax>::length': function call missing argument list; use '&std::basic_string<_Elem,_Traits,_Ax>::length' to create a pointer to member
1>        with
1>        [
1>            _Elem=char,
1>            _Traits=std::char_traits<char>,
1>            _Ax=std::allocator<char>
1>        ]
1>\example.cpp(29) : error C2440: '=' : cannot convert from 'unsigned int (__thiscall std::basic_string<_Elem,_Traits,_Ax>::* )(void) const' to 'int'
1>        with
1>        [
1>            _Elem=char,
1>            _Traits=std::char_traits<char>,
1>            _Ax=std::allocator<char>
1>        ]
1>        There is no context in which this conversion is possible

Thanks in advance...

War es hilfreich?

Lösung

len = name.length();
             //  ^^

length is a member function, not a member variable. You need to call that function.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top