ifstream([directory])

D

Dave Johnston

Hi,

I'm currently trying to create a wrapper that uses C functions but
behaves like ifstream (from fstream.h) - this is because the platform
I'm using (WinCE) doesn't support streams and this is the easiest way
to take a huge project across onto it.

Basically, I've hit a problem. I have no idea how the ifstream class
handles directories. In the code I have (which I didn't write), there
are several places whereby an ifstream stream is created with a
directory name. My wrapper simply refuses to accept a directory as the
filename - but when I do this, the project breaks.

I've tried to work it out by myself, but I'm stuck! Anyone have an
idea about the internals of fstream and how it works (I'm using VC,
but this is .c++... I'm assuming (ha!) MS didn't do anything
ridiculous)? When I do a get() on an ifstream dir it just seems to
return an empty ('space') character... and getline() does nothing, it
seems. Surely ifstream is doing something with the directory if I can
do a while([ifstream object]) loop!

Any advice would be hugely appreciated.
Dave.
 
V

Victor Bazarov

Dave Johnston said:
I'm currently trying to create a wrapper that uses C functions but
behaves like ifstream (from fstream.h) - this is because the platform
I'm using (WinCE) doesn't support streams and this is the easiest way
to take a huge project across onto it.

Basically, I've hit a problem. I have no idea how the ifstream class
handles directories. In the code I have (which I didn't write), there
are several places whereby an ifstream stream is created with a
directory name. My wrapper simply refuses to accept a directory as the
filename - but when I do this, the project breaks.

I've tried to work it out by myself, but I'm stuck! Anyone have an
idea about the internals of fstream and how it works (I'm using VC,
but this is .c++... I'm assuming (ha!) MS didn't do anything
ridiculous)?

You don't have to assume. You could simply ask in a Visual C++
newsgroup (microsoft.public.vc.language).
When I do a get() on an ifstream dir it just seems to
return an empty ('space') character... and getline() does nothing, it
seems. Surely ifstream is doing something with the directory if I can
do a while([ifstream object]) loop!

ifstream does not usually handle directories (not on Windows, anyway).
If you try to open a directory using an ifstream, you won't go far. It
just doesn't have 'goodbit' set in that case, making the whole attempt
in vain. Try calling 'good()' on your stream and see what you get.

Also, C++ does NOT define how ifstream associates itself with a file
on the hosting system. It's an implementation detail. Take the source
of the run-time library and see what they did. It wouldn't surprise me
if fstreams were just wrappers of FILE*-based functions, which are, in
turn, wrappers of some OS-specific functionality.

Victor
 
M

MiniDisc_2k2

Dave Johnston said:
Hi,

I'm currently trying to create a wrapper that uses C functions but
behaves like ifstream (from fstream.h) - this is because the platform
I'm using (WinCE) doesn't support streams and this is the easiest way
to take a huge project across onto it.

Basically, I've hit a problem. I have no idea how the ifstream class
handles directories. In the code I have (which I didn't write), there
are several places whereby an ifstream stream is created with a
directory name. My wrapper simply refuses to accept a directory as the
filename - but when I do this, the project breaks.

I've tried to work it out by myself, but I'm stuck! Anyone have an
idea about the internals of fstream and how it works (I'm using VC,
but this is .c++... I'm assuming (ha!) MS didn't do anything
ridiculous)? When I do a get() on an ifstream dir it just seems to
return an empty ('space') character... and getline() does nothing, it
seems. Surely ifstream is doing something with the directory if I can
do a while([ifstream object]) loop!

Any advice would be hugely appreciated.
Dave.

I don't know the internals about how fstream works, but here's how I'd
handle the directories in your situation. I'm not sure how to handle the
char*s which I would recommend here, as I'm more accustomed to std::string,
so I used std::string here, hoping you'd be able to figure out how to
convert the code to a char*:

std::string ExtractDir(std::string&);

/* Note this code assumes that the drive is left out, and that \ is not the
first character. Such as:

Dir\Dir2\file.txt (works)
ADir\afile.exe (works)
\Dir\file.txt (does not work)
C:\file.txt (does not work)
file.txt (works)

This coding can easily be added with a few if statements and using the same
logic as I have used to extract the path. */



int main()
{
string pathname; // the path (directory+file) you want to
access
std::cout << "Please input the pathname: ";
std::cin >> pathname;

string directory; // stores a single directory

do
{
directory = ExtractDir(pathname);
if (directory == "") break;
/* add code here to change the working directory to the indicated
directory. Something such as system("pushd()") should work well,
it's a good idea to leave the system back in the state it was before
you modified the working directory.*/

} while (true);

/* Now that the working directory has been set, the file would be in the
right directory. Add code here to access the file. */

/* Add code here to put the system back to the original working
directory, such as system ("popd()") */
}

std::string ExtractDir(std::string &path)
{
bool gotOneDir = false;
/* false until entire directory name has been stored in returnvalue */
std::string returnvalue, newpath;
/* newpath will replace path when done */
for (int x = 0; x < path.length(); x++)
{
char buffer = path[x]; // added only for efficiency
if (gotOneDir)
newpath += buffer;
else
{
if (buffer == '\\') // don't forget double backslash
gotOneDir = true; // end of directory name
else
returnvalue += buffer;
}
}

path = newpath;
return returnvalue;
}

Basically all that does is loop through and keep extracting a directory one
by one. It may not be the most efficient way to do it but I think it works.
Tell me what you think
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top