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.