#### Archived

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

# Manipulating strings(C++)

This topic is 5223 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

##### Share on other sites
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.

"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

##### Share on other sites
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.

##### Share on other sites
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?

"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

##### Share on other sites
_______________________________________________
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.

##### Share on other sites
Did you try the code I posted above?

"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

##### Share on other sites
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.

##### Share on other sites
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.

"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

##### Share on other sites
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()...
MessageBox(hwnd, filename , NULL, MB_OK | MB_ICONWARNING);

These yeilds the string:

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.

##### Share on other sites
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?

"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

1. 1
2. 2
3. 3
Rutin
22
4. 4
frob
17
5. 5

• 9
• 33
• 13
• 13
• 10
• ### Forum Statistics

• Total Topics
632582
• Total Posts
3007200

×