943,913 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 784
  • C++ RSS
May 31st, 2009
0

Emptying char MAX_PATH array?

Expand Post »
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?:
CPP Syntax (Toggle Plain Text)
  1. if (fileName = "")
  2. {
  3. }

How could I do this? I have tried:
CPP Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 18
Solved Threads: 2
Light Poster
killdude69 is offline Offline
45 posts
since Jul 2008
May 31st, 2009
1

Re: Emptying char MAX_PATH array?

Use memset...

memset(fileName, 0, MAX_PATH);

Rewrites the elements to 0.
Reputation Points: 60
Solved Threads: 17
Junior Poster in Training
Yiuca is offline Offline
88 posts
since Sep 2008
May 31st, 2009
0

Re: Emptying char MAX_PATH array?

Keep it simple:
C++ Syntax (Toggle Plain Text)
  1. fileName[0] = '\0';
  2. // or even
  3. *fileName = 0;
  4. // to test:
  5. if (*fileName) { // non-empty
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
May 31st, 2009
0

Re: Emptying char MAX_PATH array?

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.
Reputation Points: 18
Solved Threads: 2
Light Poster
killdude69 is offline Offline
45 posts
since Jul 2008
May 31st, 2009
1

Re: Emptying char MAX_PATH array?

Click to Expand / Collapse  Quote originally posted by killdude69 ...
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?
C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
May 31st, 2009
0

Re: Emptying char MAX_PATH array?

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.
Reputation Points: 18
Solved Threads: 2
Light Poster
killdude69 is offline Offline
45 posts
since Jul 2008
May 31st, 2009
0

Re: Emptying char MAX_PATH array?

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
Reputation Points: 18
Solved Threads: 2
Light Poster
killdude69 is offline Offline
45 posts
since Jul 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Is it illegal to compare NULL pointers?
Next Thread in C++ Forum Timeline: Unexplained INVALID_HANDLE_VALUE from CreateFile()





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC