# filesystems

This topic is 4730 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have problems with _findfirst ( actually I believe its _findnext) but the problem is that it don't show all the files. How do you guys create filesystems, do you use windows FindFirst, or opendir, or the way i'm doing it? FS.cpp
#include <direct.h>
#include <stdlib.h>
#include <stdio.h>

#include "FS.h"

FS::FS()
{
char tmp[_MAX_PATH];
_getcwd(tmp,_MAX_PATH);

struct _finddata_t tmpdata;
long hfile;
FileEntry tmpfiles;

if( (hfile = _findfirst("*.txt",&tmpdata) != -1L))
{
cout<<"getting files..."<<endl;
do
{
tmpfiles.name = tmpdata.name;
tmpfiles.path = tmp;
tmpfiles.size = tmpdata.size;
tmpfiles.flag = 0;
FileList.push_back(tmpfiles);
}while( 0 == _findnext(hfile,&tmpdata)  );

_findclose(hfile);
}
}
{
}
void FS::close(void *handle)
{
}
void *FS::create(char *filename, int flags)
{
return((int)0);
}
int FS::FileCount()
{
return ((int)FileList.size());
}
bool FS::FileExist(char *name)
{
return 0;
}
const char* FS::FileName(int index)
{
if( index<FileCount())
{
//return ("hello this works");
return (FileList[index].name.c_str());
}
return ((char*)0);
}
char *FS::FileNameEx(int index)
{
return((char*)0);
}
DWORD FS::Flags(char *name)
{
return 0;
}
DWORD FS::Flags(int index)
{
return 0;
}
char* FS::FlagsEx(int index)
{
return 0;
}
char* FS::FlagsEx(char *name)
{
return 0;
}
void* FS::open(char *filename, int flags)
{
return 0;
}
FS::~FS()
{
}


FS.h
#pragma once
#include <windows.h>
#include <io.h>
#include <vector>
#include <string>
#include <iostream>
using namespace std;

class FS
{
private:

struct FileEntry
{
string name;
string path;
int size;
DWORD flag;
};
vector<FileEntry> FileList;

typedef struct _FILTER
{
string name;
string ext;
int flags;
int unused;
} Filter;

vector<Filter> FilterList;

public:

FS();

virtual ~FS();

int   FileCount();
const char* FileName(int index);
char* FileNameEx(int index);

DWORD Flags(char* name);
DWORD Flags(int index);
char* FlagsEx(char* name);
char* FlagsEx(int index);

void* open(char* filename,int flags=0);
void* create(char*filename,int flags=0);
void  close(void* handle);

bool  FileExist(char* name);

};



##### Share on other sites
does any body know whats wrong?

##### Share on other sites
Are you setting the directory you want to look in?
_chdir

##### Share on other sites
I'm betting either you learned C before you learned C++ or you learned C++ from somebody who learned C before they learned C++. So I'm going to be annoying and make you read through all my rants about C++ being used as C in order to find the answer to your problem (and it's so simple you're going to kick yourself) [smile]
• #include <stdlib.h>. The C standard libraries are available in two forms in C++, the deprecated form which you are using and the newfangled form which changes the header names from X.h to cX and puts all symbols in namespace std. Since you don't need C source compatibility (and can't get it as you're using classes) you should almost certainly be using #include <cstdlib>. The same applies for stdio.h/cstdio. Of course, since you don't actually appear to be using anything from either header you may as well just drop them entirely.

• struct _finddata_t tmpdata;. In C++ the struct keyword is unneccessary here. You may argue that it is clearer, but I would consider this a spurious argument since even in C it's idiomatic to typedef struct names and skip the struct qualifier on variable declarations.

• if( (hfile = _findfirst("*.txt",&tmpdata) != -1L)). This is your error. It's a case of operator precedence. != has higher precedence than =, so this line is parsed as: if((hfile = (_findfirst("*.txt",&tmpdata) != -1L))), which takes the return value of _findfirst and compares it to -1L. The boolean result is then converted to a long and assigned to hfile, leaving hfile with a value of either 1 or 0. A good compiler with a decent warning level enabled will warn about assignments in conditional statements. Now you know one more reasons why.

• void FS::close(void *handle). void *? Are you sure? Since it's just a stub function at the moment it's impossible to know what a 'handle' is, but void * is rarely a good choice.

• void *FS::create(char *filename, int flags). Why char *? If you leave the function as is then clients can pass raw strings automatically but must tack on a c_str() call to pass std::string objects. If you used std::string const & instead then clients could pass raw strings or std::strings naturally.

• const char* FS::FileName(int index). Again, why char * instead of std::string? You may have a case to make if you have a valid reason not to use exceptions for failure cases and have no suitable string to use as an error case. On the otherhand, this function is vulnerable to clients grabbing a filename and then modifying the directory in some manner which invalidates the const char * they hold (if such a function exists - it appears not to so far in your implementation), i.e.:
const char * f = fs.FileName(1);
// remove a the first file from the list held in fs
std::cout << f << '\n'; // dangling pointer access.

• #pragma once. Prefer to use the more portable standard inclusion guards either instead of or in addition to #pragma once. My current first-choice compiler does not support #pragma once so in situations where you have more complex errors that would require people to compile multiple source files to check your code I would have additional work to do to be able to compile your source, making me more likely to spend my time on somebody else's problem instead of yours.

• using namespace std;. Never put a using namespace declaration in a header file. This single directive means then every single header file included after FS.h in a translation unit is automatically also using namespace std;, whether it wants to or not.

• typedef struct _FILTER{/*...*/} Filter;. Such typedefs are unneccessary in C++. A simple struct Filter{/*...*/}; is exactly equivalent (except for not defining the additional name _FILTER).
Enigma

##### Share on other sites
thanks Enigma

i did some cleaning up but the problem for some reason was that i wasn't linking my files...so it didn't seem to be working. I didn't notice that untill i tried searching for certain file types.

you was right that was simple

1. 1
2. 2
Rutin
15
3. 3
4. 4
5. 5

• 13
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633725
• Total Posts
3013559
×