is Array == Array condition allowed?

Jul 14, 2016 at 10:44am
Can I treat, say char[100] like a std::string? For example, can I do this:
1
2
char text[100] = "sometext"
if (text != "someothertext") text = "someothertext";

Or do I have to deal with strcmp, and strcpy? (Same about TCHARs, Im actually using unicode, which means wcscmp, wcscpy)
Last edited on Jul 14, 2016 at 10:46am
Jul 14, 2016 at 10:59am
closed account (48T7M4Gy)
strcmp() is the one.
Jul 14, 2016 at 11:00am
Are you doing C or C++ ?

If you use std::string then you can - because it has lots of different operators defined.

If using C, then functions like wcsncmp et al. might be the go. Note the use of the more secure version of the function with the n in them, they do bounds checking

http://en.cppreference.com/w/c/string/wide/wcsncmp
Jul 14, 2016 at 11:05am
Jf you want to use char or TCHAR you have to use the old C style functions.

If you want to try to use the std::string with TCHAR have a look here:
http://www.codeproject.com/Articles/13036/A-TCHAR-style-header-file-for-STL-strings-and-stre
Jul 14, 2016 at 11:10am
C++.
I would use std::wstring but I can't because I doesn't let me to write to files with it, like this:
1
2
3
ofstream file("file.txt");
wstring text = "sadsd";
file << text << endl;

So Im going to use stdio.h functions, but Im wondering if it doesn't take more memory? It marks << red in Visual Studio and says "Error: no operator matches these operands".
Jul 14, 2016 at 11:15am
If you use wstring than you need to use wofstream. All the streams exist for wchar_t as well.
Jul 14, 2016 at 11:21am
Damn, I suspected, but didn't know there were unicode streams. That's what I needed. Thanks, but there is more. I already moved on to TCHAR arrays and it became handy.
kemort, so you're saying I can't use if (TCHAR[] != TCHAR[]) but strcmp (or whatever unicode's version is out there) instead?
Last edited on Jul 14, 2016 at 11:25am
Jul 14, 2016 at 11:24am
I would use std::wstring but I can't because I doesn't let me to write to files with it, like this:


It's definition is an overload of std::basic_string which deal with wide characters:

http://en.cppreference.com/w/cpp/string/basic_string
Jul 14, 2016 at 11:32am
> I would use std::wstring but I can't because I doesn't let me to write to files with it
This is how it is done :
1
2
3
wofstream file(L"file.txt");
wstring text = L"sadsd";
file << text << endl;
Last edited on Jul 14, 2016 at 11:32am
Jul 14, 2016 at 11:45am
closed account (48T7M4Gy)
AFAIK strcmp() is the way unless you want to write it yourself array element by element because that's all strcmp() does. I'm just taking your post on face value where you have a char array. All the rest being posted beyond that is outside anything I want to add to.

You can try it your way if you like, but it doesn't work "Warning: array comparison always evaluates to false".

TCHAR can probably can be used with strcmp(), it's just a Microsoft thing (to corner the market :) ) - but it's the same as char IIRC.

http://www.cplusplus.com/reference/cstring/strcmp/?kw=strcmp
Jul 14, 2016 at 11:50am
Thanks everyone
Jul 14, 2016 at 11:54am
Good to hear :)
Jul 14, 2016 at 12:22pm
closed account (48T7M4Gy)
Yep, well done glad to help. ;)
Topic archived. No new replies allowed.