Emptying char MAX_PATH array?

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jul 2008
Posts: 45
Reputation: killdude69 is an unknown quantity at this point 
Solved Threads: 1
killdude69 killdude69 is offline Offline
Light Poster

Emptying char MAX_PATH array?

 
0
  #1
May 31st, 2009
This is a very simple problem, but for some reason I can't get it to work. I need to empty a variable ( char fileName[MAX_PATH] =""; ).

It needs to be to where when I use this that it sees if it equals "", this wont work if it equals NULL will it?:
  1. if (fileName = "")
  2. {
  3. }

How could I do this? I have tried:
  1. fileName = "";
  2. fileName = NULL;
  3. fileName[MAX_PATH] = "";

Those all return errors, why can't I empty the variable?
Last edited by killdude69; May 31st, 2009 at 2:33 am.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 83
Reputation: Yiuca is on a distinguished road 
Solved Threads: 15
Yiuca Yiuca is offline Offline
Junior Poster in Training

Re: Emptying char MAX_PATH array?

 
1
  #2
May 31st, 2009
Use memset...

memset(fileName, 0, MAX_PATH);

Rewrites the elements to 0.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is online now Online
Postaholic

Re: Emptying char MAX_PATH array?

 
0
  #3
May 31st, 2009
Keep it simple:
  1. fileName[0] = '\0';
  2. // or even
  3. *fileName = 0;
  4. // to test:
  5. if (*fileName) { // non-empty
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 45
Reputation: killdude69 is an unknown quantity at this point 
Solved Threads: 1
killdude69 killdude69 is offline Offline
Light Poster

Re: Emptying char MAX_PATH array?

 
0
  #4
May 31st, 2009
How would I test if it IS empty.
Because I am currently using: if (fileName == "")

And I am using this for over 6 if statments.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is online now Online
Postaholic

Re: Emptying char MAX_PATH array?

 
1
  #5
May 31st, 2009
Originally Posted by killdude69 View Post
How would I test if it IS empty.
Because I am currently using: if (fileName == "")

And I am using this for over 6 if statments.
Didn't you familiar with logical not operator?
  1. if (!*fileName)
  2. // or
  3. if (fileName[0] == '\0')
  4. // or
  5. if (strlen(fileName) == 0)
By the way, fileName == "" is equal to false in all cases. Why? Think about pointers comparison
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 45
Reputation: killdude69 is an unknown quantity at this point 
Solved Threads: 1
killdude69 killdude69 is offline Offline
Light Poster

Re: Emptying char MAX_PATH array?

 
0
  #6
May 31st, 2009
I know how to use logical operators, I just wasn't sure if !* was even valid.

the strlen() aproach seems best to me ArkM, I will try it.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 45
Reputation: killdude69 is an unknown quantity at this point 
Solved Threads: 1
killdude69 killdude69 is offline Offline
Light Poster

Re: Emptying char MAX_PATH array?

 
0
  #7
May 31st, 2009
Sorry for the DP, but I just want to let you know, ArkM, that that worked perfectly. Thanks to you to Yiucia, I used your memset approach. Rep for you all. Problem solved.

And just so you know, I am a decent programmer, don't be fooled by this simple question
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC