Manipulating strings(C++)

Started by
8 comments, last by dirtbag 19 years, 10 months ago
Hoping someone can help. To make my problem sound easier to explain.. Say that i'm loading a texture from disk,and i want to copy the filename i choose from the open dialogue,copy (part)of it,and proceed to load a second file using the same name(without the extension or the directory). This is where i load the texture file.. HRESULT OpenFile() { char filename[_MAX_PATH] = { '\0' }; TCHAR Filter[] = TEXT("All Formats \0*.*\0""BMP(BMP \0*.bmp\0"); char Dir[_MAX_PATH]; sprintf(Dir, "%s\\Textures", RootPath); OPENFILENAME ofn; memset(&ofn, 0, sizeof(OPENFILENAME)); ofn.lStructSize = sizeof(OPENFILENAME); ofn.hwndOwner = hwnd; ofn.hInstance = hInstance; ofn.lpstrFilter = Filter; ofn.nFilterIndex = 1; ofn.lpstrFile = filename; ofn.nMaxFile = sizeof(filename); ofn.lpstrFileTitle = NULL; ofn.nMaxFileTitle = NULL; ofn.lpstrInitialDir = InitDir; ofn.lpstrDefExt = NULL; ofn.lpstrTitle = "Loading image..."; ofn.Flags = OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_NOCHANGEDIR; if (GetOpenFileName(&ofn) != NULL) { switch (ofn.nFilterIndex) { case 1: LPSTR fn; file_loader.GetRelativeName(filename, InitDir, &fn); SAFE_RELEASE(m_Texture); LoadTexture(filename); MessageBox(hwnd, ofn.lpstrFile, NULL,MB_OK | MB_ICONWARNING); strcpy(fn, ofn.lpstrFile); LoadSecondFile(fn); break; } } } void LoadSecondFile(LPSTR FileName) { MessageBox(hwnd, ScriptName, NULL,MB_OK | MB_ICONWARNING); } As you can see,i'm using the simple MessageBox(),to display the name of the file,that i choose from the openfile dialogue for my texture,to check what the string of text is. The problem is,i only want to end up with,for example "Texture1" displayed in both message boxes. What i'm getting now,is the complete directory(where it is located,plus the file extension). Such as this: C:Documents and Settings\Administrator\Desktop\SourceTree\May04\Test\Textures\Test1.bmp What i want to end up with is just Test1. or if i chose a texture named lightmap.bmp,i want lightmap. Not the rest(directory and extension). Is there an easy way to get this? am i missing something very simple?I'm sure there is a way to do this using c++. Cheers. [edited by - dirtbag on May 31, 2004 7:50:46 PM]
Why would anyone in a right state of mind attempt to get blood out of a stone?? don''t they know its made of stone..doh.
Advertisement
quote:From the MSDN
nFileOffset
Specifies the zero-based offset, in TCHARs, from the beginning of the path to the file name in the string pointed to by lpstrFile. For the ANSI version, this is the number of bytes; for the Unicode version, this is the number of characters. For example, if lpstrFile points to the following string, "c:\dir1\dir2\file.ext", this member contains the value 13 to indicate the offset of the "file.ext" string. If the user selects more than one file, nFileOffset is the offset to the first file name.
nFileExtension
Specifies the zero-based offset, in TCHARs, from the beginning of the path to the file name extension in the string pointed to by lpstrFile. For the ANSI version, this is the number of bytes; for the Unicode version, this is the number of characters. For example, if lpstrFile points to the following string, "c:\dir1\dir2\file.ext", this member contains the value 18. If the user did not type an extension and lpstrDefExt is NULL, this member specifies an offset to the terminating NULL character. If the user typed "." as the last character in the file name, this member specifies zero.


Thanks Salsa!Colin Jeanne | Invader''s Realm
"I forgot I had the Scroll Lock key until a few weeks ago when some asshole program used it. It even used it right" - Conner McCloud
Thanks,though using nFileOffset doesn''t seem to be helping with manipulating the characters of the filename string much.

Loading multiple files its working,but that isn''t what i''m trying to do.

E.g: if the strFilename is tex1.bmp,i can shorten it to just tex by doing this:

ofn.lpstrFileTitle = strFilename;
ofn.nMaxFileTitle = sizeof(strFilename -1);

so after the function for loading the 2nd file has been called,
the string "tex" is displayed in my message box.

Though with longer filenames(used for the texture)e.g Texture1.bmp i end up with something like "Texture1." as the string???

All i want to do is when i choose the file from the file requester dialogue box,say Texture1.bmp,is then manipulate the filename..to get just the string "Texture1" then display it in a dialogue box.

That will be evrything i need,to do what it is i''m doing.
If anyone can help with this,i''ll be very grateful.

Thanks.
Why would anyone in a right state of mind attempt to get blood out of a stone?? don''t they know its made of stone..doh.
Given the string "C:\path\to\file.ext" the substring "file" will begin at

ofn.lpstrFile[ofn.nFileOffset]

and end at

ofn.lpstrFile[ofn.nFileExtension - 1]

What problems are you having with nFileOffset?

Thanks Salsa!Colin Jeanne | Invader''s Realm
"I forgot I had the Scroll Lock key until a few weeks ago when some asshole program used it. It even used it right" - Conner McCloud
_______________________________________________
What problems are you having with nFileOffset?
_______________________________________________

Just can''t end up with the correct string in my message box.

What i end up passing to the MessageBox() is always something
like Tex,Te,Text...or the full Texture.bmp
not just the filename, minus the .ext and the path.

If anyone can show the correct way to do it (in some code)
will be a great help.
Cheers.
Why would anyone in a right state of mind attempt to get blood out of a stone?? don''t they know its made of stone..doh.
Did you try the code I posted above?

Thanks Salsa!Colin Jeanne | Invader''s Realm
"I forgot I had the Scroll Lock key until a few weeks ago when some asshole program used it. It even used it right" - Conner McCloud
Invader X,cheers for taking the time to reply to these lame problems i''m having,i do appreciate it man.

The thing is,is no matter what i''m trying with the file name(i mean the filename from the file dialogue box)and the nFileOffset
,its still just passing the same(or similar strings)but not what i want..(just taking off the extension and the path).

I really don''t know what else to try(losing patience with it).

If you can make sense of my above code(posted in frustration)or are willing to help me further,i''ll post the code that i''m using,
if you can help.

I''d really appreciate it,becuase its doing my head in something that would seem so simple

Cheers.
Why would anyone in a right state of mind attempt to get blood out of a stone?? don''t they know its made of stone..doh.
Have you tried something like this:

//after GetOpenFileName()char real_filename[MAX_PATH] = {0};strncpy(real_filename, filename + ofn.nFileOffset, ofn.nFileOffset - ofn.nFileExtension - 1);

That should copy only the file''s name, without the path and without the extension, into the variable real_filename.

Thanks Salsa!Colin Jeanne | Invader''s Realm
"I forgot I had the Scroll Lock key until a few weeks ago when some asshole program used it. It even used it right" - Conner McCloud
Thanks InvaderX,
Ok,this is what is happening in my code.
I''m actually loading a map(not a texture),and straight after the map has loaded a data file needs to be loaded(with the same name as the map,minus the path+extension).

after GetOpenFileName()...
LoadMap(filename);
MessageBox(hwnd, filename , NULL, MB_OK | MB_ICONWARNING);

These yeilds the string:

"C:Documents and Settings\Administrator\Desktop\SourceTree\June04\Test\Maps\Grave1.TRSL"

so i know that the full filename is correct at this part in the code.

next...

char real_filename[MAX_PATH] = {0};strncpy(real_filename, filename + ofn.nFileOffset, ofn.nFileOffset -ofn.nFileExtension -1);
MessageBox(hwnd, real_filename , NULL, MB_OK | MB_ICONWARNING);

the app now just crashes ...

any further help will be appreciated.
Cheers.
Why would anyone in a right state of mind attempt to get blood out of a stone?? don''t they know its made of stone..doh.
Err... sorry, I had the formula backwards. It should be this:

strncpy(real_filename, filename + ofn.nFileOffset, ofn.nFileExtension - 1 - ofn.nFileOffset);


If the above doesn''t work, can you post the values for ofn.nFileOffset and ofn.nFileExtension for the path string in your last post?

Thanks Salsa!Colin Jeanne | Invader''s Realm
"I forgot I had the Scroll Lock key until a few weeks ago when some asshole program used it. It even used it right" - Conner McCloud

This topic is closed to new replies.

Advertisement