filenames- one for the C programmers
This is one for the C programmers.
I have a programming problem which has been holding me up
for over a month now.
I am trying to read information from Lightwave Obj files, to reconstruct the geometry etc. in a win32 game. I read the name of a texture file (to decorate a particular surface), from the Lightwave object file. I store the name in a string, and try to open the specified texture image file, using the
stored string.
My problem is a win32 problem, rather than a Lightwave
or graphics problem.
I cannot use the contents of the stored string as an argument to the C
function "fopen", which generally takes a filename enclosed by double quotation marks.
the program works fine if i type the name of the file
by hand, as in fopen("texture.bmp", "r")
or if i initialise a character array:
char * filename[] = "texture.bmp";
and then call fopen(filename, "r");
However, if i initialise a character array:
char * filename[] = _{string[0],string[1], string[2],....._{string[9],string[10], string[11], ''\0''};
the statement fopen(filename, "r");
produces an error message.
i have verified that string[0]......string[11], do indeed contain the correct characters to name the file.
I don''t think this isn''t a syntax problem, as the exact same procedure
works to open and read a text file in a win32 console application, using any one of four string initialisation procdedures.
The error messages are various conditions from the io functions like
fopen.c and fscanf.c, which on my system are located at C:\Program Files\Windows Media Player\Microsoft Visual Studio\VC98\CRT\SRC
These error messages generally concern null streams, and as far as I can see just amount to saying that the file can''t be opened.
This is a very boring problem, I know, but if any heroic people can make sense of these details, and know the answer, I''ll be eternally grateful
michael mcmahon
May 06, 2002 12:02 PM
"char * filename[]" is wrong. Either do "char * filename" or "char filename[]".
What you quoted is setting up a double pointer (char **). Post the relevant code, im sure someone will then figure it out.
What you quoted is setting up a double pointer (char **). Post the relevant code, im sure someone will then figure it out.
Thanks for your help.
unfortunately, the extra * in the declaration *filename[] was just a typing error, but i took your advice about posting the code on the web, and in the process of trimming the code to isolate the relevant bits i discovered the problem. two similarly named initialisation functions were called in the wrong order (doh!).
Reversing them solved the problem. What a relief!
Thanks again for taking the trouble to reply.
michael mcmahon
unfortunately, the extra * in the declaration *filename[] was just a typing error, but i took your advice about posting the code on the web, and in the process of trimming the code to isolate the relevant bits i discovered the problem. two similarly named initialisation functions were called in the wrong order (doh!).
Reversing them solved the problem. What a relief!
Thanks again for taking the trouble to reply.
michael mcmahon
Try something like this...
But how do you know the length of string? Is it always 12?
[edited by - __ALex_J_ on May 9, 2002 12:52:09 PM]
char filename[13];memcpy(filename, string, 12);filename[12] = '\0'
But how do you know the length of string? Is it always 12?
[edited by - __ALex_J_ on May 9, 2002 12:52:09 PM]
the max size a file name can be is 255, which is defined in windows as MAX_PATH
so with knowing this.. just define a char array to be MAX_PATH and use it.
--------------8<--------------
char szFilePath[MAX_PATH];
strcpy(szFilePath, "c:\\yadda\\yadda\\yadda.txt");
FILE* f = fopen(szFilePath, "rb");
-------------->8--------------
if the file name is static, just use something like this:
--------------8<--------------
char* szFilePath = "c:\\yadda\\yadda\\yadda.txt";
FILE* f = fopen(szFilePath, "rb");
-------------->8--------------
so with knowing this.. just define a char array to be MAX_PATH and use it.
--------------8<--------------
char szFilePath[MAX_PATH];
strcpy(szFilePath, "c:\\yadda\\yadda\\yadda.txt");
FILE* f = fopen(szFilePath, "rb");
-------------->8--------------
if the file name is static, just use something like this:
--------------8<--------------
char* szFilePath = "c:\\yadda\\yadda\\yadda.txt";
FILE* f = fopen(szFilePath, "rb");
-------------->8--------------
i not am smart stupid no ok
quote:
Original post by SonicMouse2
the max size a file name can be is 255, which is defined in windows as MAX_PATH
Almost ...
#define MAX_PATH 260
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
whoa, and for the past 3 years i thought it was 255
i feel violated
i feel violated
i not am smart stupid no ok
quote:
Original post by SonicMouse2
whoa, and for the past 3 years i thought it was 255
i feel violated
Well, that''s better than thinking it was 265

"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
i have never been able to use the maximum file path allowed by windows... look at this.. this is a 260 char file path.. this is the maximum windows will allow. it kinda puts it into perspective lol:
c:\yadda\yadda\yadda\yadda\yadda\yadda\yadda\yadda\yadda\yadda\yadda\yadda\yadda\yadda\yadda\yadda\yadda\yadda\yadda\yadda\yadda\yadda\yadda\yadda\yadda\yadda\yadda\yadda\yadda\yadda\yadda\yadda\yadda\yadda\yadda\yadda\yadda\yadda\yadda\yadda\yadda\yadda\y.txt
ok im bored
c:\yadda\yadda\yadda\yadda\yadda\yadda\yadda\yadda\yadda\yadda\yadda\yadda\yadda\yadda\yadda\yadda\yadda\yadda\yadda\yadda\yadda\yadda\yadda\yadda\yadda\yadda\yadda\yadda\yadda\yadda\yadda\yadda\yadda\yadda\yadda\yadda\yadda\yadda\yadda\yadda\yadda\yadda\y.txt
ok im bored
i not am smart stupid no ok
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement