Archived

This topic is now archived and is closed to further replies.

michael mcmahon

filenames- one for the C programmers

Recommended Posts

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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
"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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Try something like this...



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]

Share this post


Link to post
Share on other sites
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--------------

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites