|
Hello,
i am developing an application which i have to compare two wav files!Do you have to suggest me something(e.x. link)?How can i do it because i am newbbie on the issue!
Thanks
|
|
|
|
|
Hi all,
Simple question .... (funny that I can't remember how to do it)
I have variable:
char TestString[3];
How can one test if the TestString is Empty
I have tried the following, without success:
<br />
if (TestString == "")<br />
{ <br />
....<br />
}<br />
Thanx in advance
The only programmers that are better than C programmers are those who code in 1's and 0's.....
Programm3r
|
|
|
|
|
strlen .
Programm3r wrote: char TestString[3];
In this case, it will not be an empty string.Contains some garbage.
try,
char TestString[3] = {0};
or
char TestString[3] = "";
cout<<strlen(TestString)<<endl;
It will give you desired output. i.e. output will be 0, which indiacates empty string.
|
|
|
|
|
You need to do
if(strlen(TestString) == 0)
or
if(strcmp(TestString, "") == 0)
Remember that your string will not be initialised when you declare it though, so neither test is valid until you've put something into the string (NULL for the first character at the very least). Or you can do the following...
memset(&TestString[0], 0, sizeof(TestString));
Hope this helps,
Joel.
|
|
|
|
|
Your string
Programm3r wrote:
char TestString[3];<code></blockquote><br />
<br />
is not empty, it's random (junk) initialised (end it is not <code>NULL terminated!).
maybe the following code snipped will help you:
char TestString[3]="";
if (!strcmp(TestString, ""))
{
printf("empty\n");
}
else
{
printf("not empty: %s\n", TestString);
}
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
|
|
|
|
|
if (!TestString[0])
{
// The first character is null, therefore it's empty
}
That's assuming you have set it to be empty. This is one of many reasons to avoid char arrays and use std::string or CString. Unless you're trapped in C, which I seem to recall, you are.
In this case, make sure you use memset to clear the contents of a new char array, so the above test will work. All the other tests will do the same thing, they will look to see how many characters there are before a NULL.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
|
In addition to all the things said, it's always good programming practice to initialize the variables before testing on them.
IMO, one common reason for problem is that memory is allocated differently in debug and release mode.
In debug, the allocated buffer becomes filled with null-bytes.
In release-build, it isn't. It's filled with junk characters (whatever is on the stack).
So (without initializing) testing for '\0' at index 0 works in debug mode, but not in release mode.
That's something to watch out for..
Alcohol. The cause of, and the solution to, all of life's problems - Homer Simpson
|
|
|
|
|
kakan wrote: n debug, the allocated buffer becomes filled with null-bytes.
In release-build, it isn't. It's filled with junk characters (whatever is on the stack).
and this is the source of very weird bugs.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
|
|
|
|
|
Hello,
when i include a header file of a class in a ".c" file ...i get error ... "class name" is undefined identifier.
how do i solve this?
amy clues?
regards
Shailesh
|
|
|
|
|
class is C++ keyword. Why you want to use it in C ?
|
|
|
|
|
You're speaking Dutch to a Frenchman. In other words, no .c file can contain a class. There are no classes in C.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
Shailesh Halankar wrote: how do i solve this?
You cannot.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
|
|
|
|
|
Shailesh Halankar wrote: i include a header file of a class in a ".c" file
is that what u said, or are we getting u wrong?? you are trying to use classes in C??
May be u are not explaining correctly i guess.
vijay.
|
|
|
|
|
Shailesh Halankar wrote: when i include a header file of a class in a ".c" file
You make your file a .cc, .cpp, or .cxx file
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
Where am I going wrong? I am trying to implement strcpy manually.
<code>
main()
{
char *source = "Hi", *dest = "";
while(*source++) *dest++ = *source;
}
Visual C++ 6.0 Compiler with service pack 6. WindowsXP 32 bit edition.
|
|
|
|
|
Aljechin wrote: *dest = "";
u havent allocated memory for the dest
nave
|
|
|
|
|
Neither have I allocated memory for the source...
char *source = "Hi", *dest = "";
dest = "Hello world";
This is working fine... Someone please tell me why is it crashing if i enable that while loop.
|
|
|
|
|
Aljechin wrote: Neither have I allocated memory for the source...
Yes you have. source is 3 characters long, "Hi" and the terminating null byte.
modified:
Or rather, source is pointing to a memory area which is initialized to "Hi" and the terminating null byte.
Alcohol. The cause of, and the solution to, all of life's problems - Homer Simpson
|
|
|
|
|
kakan wrote: Yes you have. source is 3 characters long, "Hi" and the terminating null byte.
That way even the dest has the terminating null byte. ( "" == '\0' ).
Sorry but I am just curious why it's not working? I am not expert like you guys. Please help me.
I tried declaring *dest as "Halo" and it still crashes.
|
|
|
|
|
You shouldn't do it this way. Try this instead:
main(){
char temp[30];
char *source = "Hi", *dest = temp;
while(*source++) *dest++ = *source;
}
Alcohol. The cause of, and the solution to, all of life's problems - Homer Simpson
|
|
|
|
|
kakan wrote: You shouldn't do it this way. Try this instead:
Please tell me where exactly was I going wrong. Can you show me a sample to implement strcpy this way? I am trying like mad since half an hour.
|
|
|
|
|
This really is basics, I suggest you read a book that covers C. It would make things clearer to you.
Your original code was this:
main()
{
char *source = "Hi", *dest = "";
while(*source++) *dest++ = *source;
}
A declaration like this:
char *source = "Hi"
is (normally) used for constant values (that doesn't change during the execution of the program).
If you want a buffer that can be modified, you'd better allocate it, like this:
char temp[50];
Now you have a buffer that can hold up to 50 characters. Use it as you wish, i.e. like this (as long as your source text doesn't exceed 49 charaters plus the terminating null character):
strcpy(temp, "Hello World");
Alcohol. The cause of, and the solution to, all of life's problems - Homer Simpson
|
|
|
|
|
HI
me also trying for it...
its working release version not in debug version why is that?????
|
|
|
|
|
The original code stomps memory that it doesn't own. In release mode, the behaviour is undefined, in debug, the IDE is watching if you do this, and letting you know that you've done it.
The release mode version will crash, sometimes.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|