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
連結已複製
9 回應
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
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
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.
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.