Pelles C forum

C language => Expert questions => Topic started by: raviv on February 16, 2011, 04:53:44 PM

Title: fopen problem, will not open file where name length bigger than than 31
Post by: raviv on February 16, 2011, 04:53:44 PM
Problem example:

fopen() succesfully opens:
C:\Render\Floors\1234567890.bmp

But fails to open:
C:\Render\Floors\1234567890a.bmp

what could be the problem?

Thank you
Title: Re: fopen problem, will not open file where name length bigger than than 31
Post by: JohnF on February 16, 2011, 06:36:57 PM
C:\Render\Floors\1234567890a.bmp

should be

C:\\Render\\Floors\\1234567890a.bmp

John
Title: Re: fopen problem, will not open file where name length bigger than than 31
Post by: raviv on March 02, 2011, 10:12:35 AM
I work in win32 env
anyway I tried,

1.   C:\\Render\\Floors\\1234567890.bmp    => FAILED

2.   C:\Render\Floors\1234567890.bmp        => SUCCESS

...
Title: Re: fopen problem, will not open file where name length bigger than than 31
Post by: CommonTater on March 02, 2011, 11:46:57 AM
How big is the buffer you are storing your file name in?  

Unless the filename is "hard coded', I typically allow 256 characters (MAX_PATH in windows) but I'm guessing you have it set to 32... which is far too small.


Title: Re: fopen problem, will not open file where name length bigger than than 31
Post by: raviv on March 02, 2011, 12:21:43 PM
Bingo,

here is your guess(and my mistake):
#define MAXFLDSIZE 32

Thank you
Title: Re: fopen problem, will not open file where name length bigger than than 31
Post by: CommonTater on March 02, 2011, 05:27:05 PM
Bingo,

here is your guess(and my mistake):
#define MAXFLDSIZE 32

Thank you

 ;D  Every once in a while I get real lucky...

If you are using #include <windows.h> you can use their MAX_PATH as I mentioned.  It's used throughout windows so you can be pretty sure you won't get a filename longer than that.

Title: Re: fopen problem, will not open file where name length bigger than than 31
Post by: Stefan Pendl on March 02, 2011, 11:07:39 PM
The ANSI Windows API functions use MAX_PATH as the maximum path length.

The UNICODE functions use 32kB as the maximum path length.

You just need to decide, which route to go.
Title: Re: fopen problem, will not open file where name length bigger than than 31
Post by: CommonTater on March 02, 2011, 11:48:29 PM
The ANSI Windows API functions use MAX_PATH as the maximum path length.

The UNICODE functions use 32kB as the maximum path length.

You just need to decide, which route to go.

I may be horridly mistaken here but I don't think that's universal... MAX_PATH is used internally by most windows functions...  It's still 260 characters but of course in Unicode a character is 16 bits, not 8.

Title: Re: fopen problem, will not open file where name length bigger than than 31
Post by: Stefan Pendl on March 02, 2011, 11:54:57 PM
From the CreateFile Function (http://msdn.microsoft.com/en-us/library/aa363858%28VS.85%29.aspx) description at MSDN:

Quote
In the ANSI version of this function, the name is limited to MAX_PATH characters. To extend this limit to 32,767 wide characters, call the Unicode version of the function and prepend "\\?\" to the path.

And this is not the only location, where you can find it.
Title: Re: fopen problem, will not open file where name length bigger than than 31
Post by: CommonTater on March 03, 2011, 12:26:22 AM
From the CreateFile Function (http://msdn.microsoft.com/en-us/library/aa363858%28VS.85%29.aspx) description at MSDN:

Quote
In the ANSI version of this function, the name is limited to MAX_PATH characters. To extend this limit to 32,767 wide characters, call the Unicode version of the function and prepend "\\?\" to the path.

And this is not the only location, where you can find it.

Yes, but only with the special code... without that: MAX_PATH, either way.  This much I already knew.