I was wondering if it's more practical to use the old C strings than it is to use a class of strings.

I -think- a C string is really just a pointer to the first element in an array of characters, whereas a C++ string is a class.

If that's the case, wouldn't it be better to use the class string? I know that can really only be answered in context, but take for example the program I was writing that used the class string for file i/o.

One particular reply made it seem that C++ strings weren't used as often as C strings. If someone could point out the pros and cons of each, or direct me to some sort of site that can help me with this, that'd be great. =)

I'm a beginner, and if you look at the code (attatched to the aforementioned thread) you can probably tell. =P The program works for the most part; the only thing wrong with it is saving a list. Sometimes the lists save properly, but in other cases it wil duplicate the list instead of writing over it.

The way it's supposed to save is look through the lists that are already saved, and if it's a new list it just adds it to the file. This works fine. If it's an already existing list, it reads the old lists into a representation of the new lists, and excludes the old list.

At this point the representation (done with vectors) contains the rest of the lists, and not the old list. Then, it adds the list being saved to the end, just as if it were a new list.

It determines which list to exclude by comparing the names of the lists, and if they match, it skips over it while reading into the representation (rather, reads it, but discards it).

For some reason that only works once! If the list being over written is any list but the first list, this won't work.

More accurately a C-string is the array of chars, and the pointer is simply a tool to aid in using that string. (edit: a better definition just came to mind - it generally refers to the concept of using them in conjunction to represent language)

As far as which is better, neither is really, 'better' in general. In the context of C++, C++ strings are probably a better choice, as they take advantage of the OO nature of C++ and the power that comes with it. In the context of C, obviously C-strings are the ONLY choice. Beyond that, it's a matter of personal choice. I use C-strings simply because I'm used to working with them so I don't mind the extra work, and because it's one step to have code that can compile as both C and C++.

>Even more accurately a c-string is a null terminated array of chars.
As opposed to a counted string, or a high-order bit string, or a record based string, or a hybrid format. The trick to understanding C-style strings is to realize that it's just an array with a special way of marking how many characters are there. There's a physical length (the total allocated memory for the array) and a logical length (the number of meaningful characters present in the array).

If I wanted, I could make my own string standard as a length-prefix counted string:

All I'm doing is manipulating the cells of an array. As long as there's a way to tell how many meaningful characters are in the array, it's a string. The standard C-style string uses a null character ('\0') as a sentinel at the end to signify the end of the sequence.

A C++ string is a data type that takes care of the low level stuff for you. Where you would have to micro-manage the null character and array size in a C-style string, a C++ string object does all of this behind the scenes. This leaves you free to solve more interesting problems.

>They are classes, and as such have boolean operators right?
Not necessarily. Though == is overloaded for the string class.

>The only one I really need is '==' which I used, and it seemed to do what I wanted but...only once.
For a string object or a C-style string? If it's the latter then you got lucky with a pointer comparison. If it's the former then you probably didn't use it right.

The code's attatched to the thread I mentioned in the first post.
And nah, I never blamed it on the library. I realize there's obviously something faulty with my code. =P That's why I posted it, so that maybe someone could point out what's wrong and I might learn from it. =)

Fair enough. Now explain what you mean by "only seemed to work once". I'd rather go off of a short and detailed description of the problem than to go through the tedious steps of testing your relatively long code.

Bleh, I guess it's not so much that it only works once, it just...barely works at all. =P In some cases, the first list will save correctly (by finding that it first actually does exist, then making a representation of the new save file without the list being overwritten, then it just appends the 'new' list to the end as if it were actually a new list.

Also sometimes it hits an infinite loop which makes it keep 'skipping over' the last line. (usually the last contact's phone number...)

I'm still not sure why this is since for that loop i'm using two conditions, one that checks to see if the next line should be skipped over, and the other to see if the fstream is at the end of file.

Egh, this is all a little complicated for me, so maybe I should take it down a notch and practice something easier or something that doesn't require saving. Any advice?