C++ Directory Get Contents

Description

C++ Directory Get Contents

#include <iostream>
#include <io.h>
#include <time.h>
#include <string>

using namespace std;

string Chop(string &str)//from w w  w  .ja  va 2 s  .  c  o m
{
    string res = str;
    int len = str.length();
    if (str[len - 1] == '\r')
    {
        res.replace(len - 1, 1, "");
    }
    len = str.length();
    if (str[len - 1] == '\n')
    {
        res.replace(len - 1, 1, "");
    }
    return res;
}

void DumpEntry(_finddata_t &data)
{
    string createtime(ctime(&data.time_create));
    cout << Chop(createtime) << "\t";
    cout << data.size << "\t";
    if ((data.attrib & _A_SUBDIR) == _A_SUBDIR)
    {
        cout << "[" << data.name << "]" << endl;
    }
    else
    {
        cout << data.name << endl;
    }
}

int main()
{
    _finddata_t data;

    int ff = _findfirst ("../*.*", &data);

    if (ff != -1)
    {
        int res = 0;
        while (res != -1)
        {
            DumpEntry(data);
            res = _findnext(ff, &data);
        }
        _findclose(ff);
    }
    return 0;
}



PreviousNext

Related