Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

please help me how to trim whitespaces in VC++
In VC++ i am getting ImgFileName value as null. but i am getting empty space in the output so i am not able to get inside the if condition and moving to else part.

so i am not undersating how to write trim to remove whitespace


if( ImgFileName == NULL)
	
	*pbFileFound = FALSE;
else 
-----
----


What I have tried:

I am new to VC++. I am not able to understand how to perform trim in VC++
Posted
Updated 13-Jul-18 0:47am
Comments
Jochen Arndt 13-Jul-18 6:10am    
What type is ImgFileName?
If that is a pointer to some kind of string, you can't trim it while it is NULL.
suniti dinesh 13-Jul-18 6:16am    
Hi Jacon

ImgFileName type is

LPCTSTR ImgFileName
Jochen Arndt 13-Jul-18 6:19am    
LPCTSTR is Long Pointer [to] Const T STRing.
The keyword here is const. Such strings can't be modified. You have to create a copy of the string and remove the spaces from that.

But a final answer requires knowing the context of your code (how it is called / preceeded and where and how should the string be used afterwards).

You can use the green 'Improve question' link to edit your question and add this information.
suniti dinesh 13-Jul-18 6:26am    
Hi Jacon
Thank you so much for your Quick Reply.
yes it is a Const T STRing.

if the string is empty(IMGFileName) we have to make file found to false and we will close the application. in current scenario because of white space we are moving to else part and trying to open the file and we are not closing the application.
Jochen Arndt 13-Jul-18 6:38am    
There is a difference between an empty string (usually indicated as "") and a NULL pointer to a string as explained by Griff.

But again:
Without knowing from where you got that string and what you want to do with it, it is impossible to answer.

NULL and whitespace aren;t that same at all: a NULL value means "there is nothing here", not even an empty string as represented by "", or a string full of whitespace " \n\n\n "
If ImgFileName is NULL, you can;t trim it - there is nothing to trim!

Think about it as cars: you have a parking space outside your house where you always park your car. One day you decide to go to the shops, and find your wife has moved the car - the space is empty. As a result, you cannot use the content of the parking space for anything - there is no car there to unlock, let alone drive!
The space is returning NULL - "No car here!" - so you must find out why it's NULL by speaking to your wife before you can try and do anything with the car.

Your code is the same: ImgFileName is telling you "there is no name here" - you need to look at why it's it null, not try to deal with it as if it wasn't.
 
Share this answer
 
Comments
suniti dinesh 13-Jul-18 6:34am    
Hi OriginalGriff,
Thank you so much for your clear explanation. here the thing is like, if we don't have file in the specific location ImgFileName will be null and we will close the application. i have written logs and found that there is a white space in ImgFileName output, because of it we are trying to open the file instead of closing the application so we are getting error in my application.
You can use the CString class:
C++
// Added also a check for an empty string here
if (ImgFileName == NULL || *ImgFileName == _T('\x0'))
    *pbFileFound = FALSE;
else 
{
    // Create a CString copy from the string
    CString strFileName(ImgFileName);
    // Removes leading and trailing spaces from the string
    strFileName.Trim();
    // Optionally check again for empty string after trimming
    if (strFileName.IsEmpty())
        *pbFileFound = FALSE;
    else
    {
        // Can use strFileName to open the file
    }
}
 
Share this answer
 
Comments
suniti dinesh 13-Jul-18 8:17am    
Thank you So Much Jochen. i am able to trim now. it worked like a charm
Jochen Arndt 13-Jul-18 8:27am    
You are welcome and thank you for accepting my answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900