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

Reading files: when filename is very long

P: n/a
Dear comp.lang.c++,
I'm trying to read a file with very long filename using ifstream.
Although, the file exists the file open for read fails. Is there a
restriction on the size? I'm using winXP with VS2005. Please advice.
Thank you.

Oct 20 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
<pe******@gmail.comwrote in message
news:11**********************@e3g2000cwe.googlegro ups.com...
Dear comp.lang.c++,
I'm trying to read a file with very long filename using ifstream.
Although, the file exists the file open for read fails. Is there a
restriction on the size? I'm using winXP with VS2005. Please advice.
Thank you.
Any restriction would be on the OS level. Ask in a newsgroup appropriate to
your OS.
Oct 20 '06 #2

P: n/a
On 20 Oct 2006 15:12:32 -0700 in comp.lang.c++, pe******@gmail.com
wrote,
>Dear comp.lang.c++,
I'm trying to read a file with very long filename using ifstream.
Although, the file exists the file open for read fails.
Where is the code? What is the error message from perror()?

This issue is covered in Marshall Cline's C++ FAQ. See the topic
"[5.8] How do I post a question about code that doesn't work
correctly?" It is always good to check the FAQ before posting.
You can get the FAQ at:
http://www.parashift.com/c++-faq-lite/
Oct 20 '06 #3

P: n/a
pe******@gmail.com wrote:
Dear comp.lang.c++,
I'm trying to read a file with very long filename using ifstream.
Although, the file exists the file open for read fails. Is there a
restriction on the size? I'm using winXP with VS2005. Please advice.
Thank you.
The issue has nothing to do with a std::ifstream. Why don't you
implement a solution with error checking, or exceptions, that displays
the target filename used and perhaps a path+filename size check? Have
you considered that perhaps the issue is with spaces in the filename?

How will you know what the issue is if you don't implement some form of
error-checking?
I'll gladly supply you with an example of a simple error checking
mechanism but with no code you only get the skeleton.
I did it like this:

#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <iterator>
#include <stdexcept>

class FileException : public std::exception
{
// members
public:
// parametized FileException ctor and d~tor throw()
char const* what() const throw()
{
// return relevent error data
}
};

class FileReader
{
// declare members like a std::string to hold filename
// declare a std::vector<std::stringas a dynamic container for
storage
public:
// parametized ctor with path+filename length check, XP <= 240 chars
/* member functions */
void filecheck(std::string& r_s) const
{
// throw FileException if path+filename 240 chars
}
void read()
{
// decare a std:ifstream, check for error, throw if e
// open input file stream, check for error, throw if e
// while std::getline using a buffer and load the std:vector
// check if ifs failure was *not* eof(), throw e if so
}
/* friends */
friend
std::ostream&
operator<<(std::ostream& os, const FileReader& r_fr)
{
// use std::copy to std::ostream_iterate the vector into the os
stream
// return os
}
};

int main()
{
try
{
// declare a new FileReader object with target filename + check
filename size
// call the object's read() member function
// std::cout << object; with overloaded op<<
}
catch (const std::exception& e)
{
std::cerr << e.what() << std::endl;
}
return 0;
}

/* contents of file:
string 0
string 1
string 2
string 3
string 4
*/

Do not use new / delete or the exception mechanism becomes needlessly
complicated.

Oct 21 '06 #4

P: n/a
pe******@gmail.com wrote:
Dear comp.lang.c++,
I'm trying to read a file with very long filename using ifstream.
Although, the file exists the file open for read fails. Is there a
restriction on the size? I'm using winXP with VS2005. Please advice.
Thank you.
Use std::string to contain the filename.
Use std::string::c_str() to get data to pass to open:
std::string filename;
//...
std::ifstream input_file(filename.c_str());
//...

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
Oct 21 '06 #5

P: n/a
On 20 Oct 2006 15:12:32 -0700, pe******@gmail.com wrote:
>I'm trying to read a file with very long filename using ifstream.
Although, the file exists the file open for read fails. Is there a
restriction on the size? I'm using winXP with VS2005. Please advice.
Maybe it's a problem with UNC path names or with blanks in your path?
In the latter case try additional double quotes ("") around your path.

Best wishes,
Roland Pibinger
Oct 21 '06 #6

P: n/a
David Harmon wrote:
On 20 Oct 2006 15:12:32 -0700 in comp.lang.c++, pe******@gmail.com
wrote,
>>Dear comp.lang.c++,
I'm trying to read a file with very long filename using ifstream.
Although, the file exists the file open for read fails.

Where is the code? What is the error message from perror()?
AFAIK, perror doesn't necessarily report errors from ifstreams.

Oct 21 '06 #7

P: n/a

pe******@gmail.com wrote:
Dear comp.lang.c++,
I'm trying to read a file with very long filename using ifstream.
Although, the file exists the file open for read fails. Is there a
restriction on the size? I'm using winXP with VS2005. Please advice.
Thank you.
Without seeing either the code or the filename it is hard to tell, but
if you have diagnosed the problem correctly then you will need to ask
on a Windows group.

There is an oddity on the Windows platform as the API doesn't exactly
match the capabilities of the underlying disk system NTFS. For example
NTFS is case sensitive, but the API isn't.

There's another one to do with very long file name handling, but I
don't remember the details. You'll have to ask on a Windows group to
get an answer for the right format to use for the filename.
K

Oct 22 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.