Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What's the difference???

Status
Not open for further replies.

gonzilla

Programmer
Apr 10, 2001
125
0
0
US
Hi,

I just wanted a little clarification. I have run across this in my books:

#include <string.h>

string my_str;

So, is there a difference from that and...

CString my_str;

I figure that &quot;CString&quot; is a class of sorts, I know &quot;char&quot; is a type so what is &quot;string&quot;? A different class or the same as CString? And, what header would I need to include to used the CString?

One other thing if I may. I have been using a while loop similar to this to read input records from a file...

while (getline(infp,buffer))
{
sscanf(buffer.c_str(),&quot;%u %s %s %s %d %u %s&quot;, &Key, Year, Month, Day, Code, Week , Zone)

---processing statements...
}

Obviously, sscanf() works fine using C, but with C++ there must be a better way. One problem I have is with reading Zone. It is a C style char string but when I try to make it:

string Zone;

It bombs with sscanf(). I'm not sure why but I'd rather use more C++ like conventions (still keeping it simple though). Any suggestions?

Thanks for the help.
 
string is like (is really) an array of chars. the fastest and best way to use strings. also takes less memory, and less memory problems.

CString is a MFC class. that is very automated. containing the functions to find chars, get from right, turn to upper-case etc... not recommended for fast and serious programs. takes a lot of memory, and if not used right, could be a very serious memory problem.

if you write just a little prog, you can use MFC, and use CStrings, but if you want to write a serious program, that that works 100% correctly and with not problems, and more faster, use string.


look at the offline MSDN (comes with vc++) for:

fgets: reads the number of chars you specified, or until the \n character if it comes before it reaches the number you specified.
fgetc: just read one single character
fread: reads the number of characters you specify. returns the number of chars read, so you can check if it comes to end-of-file.

i recommend using fread, read all data in file or some lines, whatever you want, the format them, find for \n chras and seperate lines etc...

Daniel Cohen Gindi
dcgsmix@gmx.net
 
How much more efficient is using string instead of CString? My memory management is good so I don't have any leaks using CStrings, but how much overhead do they have? My boss wants to use MFC because it ties in well with our database development, eg. easy integration with ODBC and OLE DB. And I can still use MFC even if I don't use CString, but I do have to use CString in some instances, unless it is deemed easier to scratch that and go with another STL. I've got two months to finish this project by myself and I've got other projects given to me on the side, but speed is really an issue so if it's worth converting back to strings I can do that. I'm using a lot of char * manipulation right now and a lot of accessing CString buffers and encapsulating CStrings around char *'s(although it's not true encapsulation since the CString constructor copies the string). Anyway, is it worth the change? Thanks! MYenigmaSELF
myenigmaself@yahoo.com
 
Using CString and string, I doubt there is any difference at all. The Memory issue comes from the use of MFC itself, if you have any MFC code in your program the MFC DLL must be loaded and so it really makes no difference whether you use CStrings or not. There may be a slight memory advantage with using solely strings but if your using MFC already I don't see the diff. only a slight one at best.

I use both in my programs, it's up to you, it's habit mostly. The only thing to watch out for is that to use strings you must include <string> header file at the top of your page. If your using MFC and VC++ the header files are already included.

As far as controlling the size of your strings, I beleive that strings are automaticly sized so it's pretty safe to use just them but if you truly want to control the size of your strings you need to include an STDLib header file. I've been in the Java world for awhile so I might be mixing up the terms a bit but I believe there are string buffer types that you can use. I think they call them streams in C++ so you'd probably find them in <iostream.h> or something. Someone can correct me if I'm wrong on the names and locals. The buffers allow you to control the size of the string, or you could use chars. *Using StringBuffers[] in Java speeds up your program and is more efficient but Java is a different language.*

Rocco is the BOY!!

M O T I V A T E!
 
Yeah, thanks for the information. I did a little testing myself and once MFC was loaded using CStrings actually worked a bit faster than using strings. And I changed the structure of my project so memory isn't really an issue for me anymore, thus shrinking buffer sizes is no longer a high priority (My program currently uses 8MB on a server with 1Gig). I, too, have been spoiled by Java but I'm enjoying getting back into the C world (Although I must say I do enjoy lower level C and C++ better than VC++). Once again, thanks for the information. MYenigmaSELF:-9
myenigmaself@yahoo.com
 



i want to know how vc++ program compiles and debugging?
 
CString uses a copy on modify system, so it is efficient to pass CStrings by value, std::string should never be passed by value, because the data is copied every time.

i.e.

void Foo(CString strStuff)

is OK, but if you are using std::string it should be

void Foo(const std::string &strStuff)


Because of this CString has a thread safety issue due to a bug in the MFC implementation, which std::string does not.

Also CStrings are set up that the first four bytes of the structure are the pointer to the first byte of the data, so casting a CString to a LPCSTR is cheap and can be done implictly, std::string requires you to call the c_str() method to get a LPCSTR.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top