- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hello,
I am using Intel Fortran 2011to get a folder path, I got a problem when usedAPI function SHGetFolderPath. The problem is that I got a string like "C:\\....\\My Documents , I want to use trim() function to remove the space but failed, it seemed not space. could you help to solve the problem?
Thanks,
Kafty
I am using Intel Fortran 2011to get a folder path, I got a problem when usedAPI function SHGetFolderPath. The problem is that I got a string like "C:\\....\\My Documents , I want to use trim() function to remove the space but failed, it seemed not space. could you help to solve the problem?
Thanks,
Kafty
Link Copied
9 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi,
I think that the SHGetFolderPath returns a C string (i.e. with a null character at the end), The best way to find where the string terminates is to use the index() function like in the sample code below:
length = index(Document, char(0)) - 1
and instead of using the trim() function, you can retrieve the substring by:
Document(1:length)
Regards,
Phil
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Kafty,
TRIM removes trailingspaces not embeded spaces like in"My Documents".
If you wish to remove embedded spaces then you must write a routine to do it BUT of course the result won't be the actual path.
For example you could use "index" and loop to find each space and adjust the string to remove it or replace the space with something else (e.g. underscore)
Les
TRIM removes trailingspaces not embeded spaces like in"My Documents".
If you wish to remove embedded spaces then you must write a routine to do it BUT of course the result won't be the actual path.
For example you could use "index" and loop to find each space and adjust the string to remove it or replace the space with something else (e.g. underscore)
Les
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Kathy, is the problem that when you use the path in a generated command line that it gets treated as two or more arguments? The way to deal with this is to enclose the path in "".
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Les,
Thanks, I only want to remove tailing spaces. what I guess that is not space but string characterin behind of the string. so the Trim function does not work
Kafty
Thanks, I only want to remove tailing spaces. what I guess that is not space but string characterin behind of the string. so the Trim function does not work
Kafty
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Kafty,
I have experienced the same strange behavior with API functions that returns a NULL terminated string. In this case it seems that the trim() function does not work properly and that the only way to get the correct lenght is to find the NULL character in the string (by using the index() function) as mentioned in my previous reply.
Regards,
Phil.
I have experienced the same strange behavior with API functions that returns a NULL terminated string. In this case it seems that the trim() function does not work properly and that the only way to get the correct lenght is to find the NULL character in the string (by using the index() function) as mentioned in my previous reply.
Regards,
Phil.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
To truncate the string, deleting everything from the first blank onwards (when searching from left to right in the string):
stringshort=stringlong(1:INDEX(stringlong,' ')-1)
stringshort=stringlong(1:INDEX(stringlong,' ')-1)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
This works only if the string does not contains any blank inside !
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
First define what you mean by 'the end of the string'.
Fortran strings may be defined as ending 'at the character before a continuous run of blank characters up to the last byte in the string'. The TRIM function will deal with this type of string.
C-strings are terminated by one, but sometimes 2, null characters (CHAR(0)).
So the first occurrence of a null character in the string, searching from left to right, will be where the C-string is supposed to end - all characters after that may be rubbish.
If you have a C-string, and there is a run of one or more blank characters up to this first NULL character, and you want to terminate the string at the start of this run of blanks, you need to do a TRIM on the string that ignores the bytes from the first null character onwards, left to right.
so
stringshort = TRIM( cstring( 1:INDEX( cstring,CHAR(0) )-1 ) )
If you decide that the strings that you are interested in are 'terminated' with a particular character CHAR(N), then just substitute CHAR(n) in the above.
Fortran strings may be defined as ending 'at the character before a continuous run of blank characters up to the last byte in the string'. The TRIM function will deal with this type of string.
C-strings are terminated by one, but sometimes 2, null characters (CHAR(0)).
So the first occurrence of a null character in the string, searching from left to right, will be where the C-string is supposed to end - all characters after that may be rubbish.
If you have a C-string, and there is a run of one or more blank characters up to this first NULL character, and you want to terminate the string at the start of this run of blanks, you need to do a TRIM on the string that ignores the bytes from the first null character onwards, left to right.
so
stringshort = TRIM( cstring( 1:INDEX( cstring,CHAR(0) )-1 ) )
If you decide that the strings that you are interested in are 'terminated' with a particular character CHAR(N), then just substitute CHAR(n) in the above.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hello,
Thanks for all of your help. Phil's solution can help me to solve the problem.
Thanks a lot.
Kafty

Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page