473,720 Members | 2,745 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 13775
"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.publ ic.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.goo gle.com...
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.t xt (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(path name);
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
37908
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 thought I would try <fstream>. The code below causes an infinite loop in the while() statement. Debugging, I see that the buffer just has a 0 in the first position, and nothing else.
2
11152
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 nature in the docs, aside from creating an array with 'scandir' (http://publib.boulder.ibm.com/doc_link/en_US/a_doc_lib/libs/basetrf2/scandir.htm#A12F0c9) which will also give me the number of enties in that array. This seems a little overkill. I'm...
1
1431
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 usw. My code:
3
15052
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 button (blue arrow) then it fails to open the file. any ideas? source:
2
2940
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 disk to a text file for future reference. I now have over 60 huge text files and would like to place the information into either a database or an excel document. I would like to read in from a text file and output the text file to an excel .cvs...
1
2605
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 save some typing and cleaner than this. thanks
5
4694
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()) { cout << "Bad input." << endl; return -2; }
5
5462
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: %s\n", foobar, strerror(errno)); exit(EXIT_FAILURE);
0
8833
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
8727
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9365
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9126
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9055
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4482
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3186
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2567
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2125
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.