473,387 Members | 1,597 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

ifstream([directory])

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
2 13723
"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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: John | last post by:
Hello all, I am trying to read in lines into a buffer from a file. Normally I would do this very low-level, but I have come to the conclusion I must stop doing everything the hard way. So, I...
2
by: 73blazer | last post by:
Hello, I'm writing some C++ code, and I need to be able to find the number of files in a given directory. Is it possible under AIX4.3.3 with C++ 3.6.4? I cannot seem to locate anything of this...
1
by: Bernhard Hidding | last post by:
Hi there, I want to read an ASCII file ("magnetfile.dat") consisting of doubles into an array. The ASCII file looks like this: 1.0 1.0 1.0 0.5 2.0 1.0 1.0 0.4...
3
by: Murasama | last post by:
Hi there, Im trying a simple file IO operation in Visual Studio .NET 2003 and it can't seem to open the file. If I run the exe in the debug directory it works fine but if I click the start...
2
by: Pat | last post by:
Here is my problem that I am having with a current project. I went and backed up all my hard drives to DVD's. After backing up the drives, I used a batch file to get all the file names from the...
1
by: Gary Wessle | last post by:
hi opening a file ifstream in("../../../../../../data/ZB/Jun06/20060405"); then on the command line I do fred@debian:~/myPrograms/cpp/practice/ticpp1/co2/3$ make; ./proj is there a way to...
5
by: Dragonizer | last post by:
Hello all, I need a little help with this. I can't seem to get ifstream ( or fstream ) to read anything. In the code: ifstream input = NULL; input.open("tester.txt"); if(input.bad()) {...
5
by: Johannes Bauer | last post by:
Hello group, coming from C, I'm used to somthing like this when opening a file const char *foobar = "this.txt"; FILE *f; f = fopen(foobar, "r"); if (!f) { fprintf(stderr, "Couldn't open %s:...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.