Advertisement

Getting a filename from a file, then opening....

Started by January 01, 2000 05:29 PM
13 comments, last by ThaUnknownProgga 24 years, 11 months ago
Alright, I''m writing a conversion utility. At this point in the file structure there are filenames. I read them in like this: /*note i cannot close any array things or anything that starts with '[' because this bulletin board messes with it. I use variables a and b, so it makes a link with one and bold text with the other. hehehe */ char *fname; // ''a'' is an int that contains the //length of the filename fname = new char[a; for(int b=0; b<=a; b++) fname[b = fgetc(fp); I next want to pass this to a function that takes a char* filename of the file to open. How can I do this? When I try now it doesn't work. Edited by - ThaUnknownProgga on 1/1/00 5:34:22 PM
make sure that you have a null-terminator on that line....

char *p;
p = new char[l+1];
for (int x = 0; x<l; x++)
p[x] = fgetc(fp);
p[l] = NULL;

Apollo


Edited by - ApolloG on 1/1/00 5:46:47 PM
Advertisement
Yeah, I shoulda mention I tried tacking a NULL terminator on. I tried a ''\0'' also. Didn''t work.
You might be having problems if your filename is shorter than a.

Check the getc funciton to make sure it''s not returning an error code. If it does return an error code, dump a 0 into the array and terminate the loop.
Nope. I run the getc thing twice, as in i loop through once to get the length of the filename, then create the array of characters, then loop through again storing the characters this time.
As a quick debugging idea you might want to add in a line to print out the fname string after it''s read in. This might help you to see where the problem is and what''s going wrong.

Regards

Starfall
Advertisement
Yup I did that too. I added a ''*'' on each side to see any extra spaces and such. It prints out to the file: *c:\games\textures\clouds.bmp* but it still doesn''t load the file.
I think I''ve hit the same snag using the get,getc,and read functions of standard MSVC++ i/o libraries.

It seems that I can write everything to a file but if I try to read in a byte that is IA in hex, everything goes to hell.

is this just me or could this be realted
What function are you using to try to open the file? What syntax are you using? Can you post the code that goes after the code you posted?

Regards

Starfall
here it is in more context so you can see what it looks like and get a better feel for what im doing. in the file it has c:\games\textures\clouds.bmp listed as a texture for an object. that''s what im trying to get out and load.

void OpenTexture(char *fname){   HBITMAP hbmp;   BITMAP bm;   HDC SurfDC, BmpHdc;   hbmp = (HBITMAP)LoadImage(hInst, fname, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE / LR_CREATEDIBSECTION);   if(hbmp==NULL)		return FALSE;   //blah blah the rest then:   return TRUE;}  // so you know, hbmp is null, that is the part that fails.char *fname;fname = new char[fnamelength];for(int b=0; b<fnamelength; b++)   fname = fgetc(fp);fname[fnamelength] = ''\0'';  //also tried "NULL" so you knowOpenTexture(fname);     

This topic is closed to new replies.

Advertisement