By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,369 Members | 1,188 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,369 IT Pros & Developers. It's quick & easy.

ifstream([directory])

P: n/a
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.
Jul 19 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
"Dave Johnston" <cs*****@my-deja.com> wrote...
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
Jul 19 '05 #2

P: n/a
"Dave Johnston" <cs*****@my-deja.com> wrote in message
news:20**************************@posting.google.c om...
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

--
MiniDisc_2k2
To reply, replace nospam.com with cox dot net.


Jul 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.