Is there anyway to get a NonNullTerminated string?
Printable View
Is there anyway to get a NonNullTerminated string?
Strings are always non-null terminated. Unless you are referring to arrays of chars.
search for std::string
I'm refering to an array of chars because those strings aren't that good with files..
>> Is there anyway to get a NonNullTerminated string?
That is about as descriptive as saying: "Can I get an int that equals 12?".
Geez.
Well, I won't try to convince you of something different, but I don't use those relicts from the past at all ;)Quote:
Originally posted by sPlOrYgOn
I'm refering to an array of chars because those strings aren't that good with files..
Anyway, I'm still not sure if I understand your problem, but you could try to truncate the last char, or create them without a \0 at the end.
The only problem is, that unless you hand it to a function that takes a lenght as parameter, the function will read all data in the string and then begin to read data behind the string (because of the missing \0). It will continue to do so until - by chance - it hits a \0 in memory.
edit: btw, this is a very bad thing to do ;)
>I'm refering to an array of chars because those strings aren't that good with files..
For an array of char to be considered a string you must terminate it with nul, or have some other way of determining the length. How is the std::string class not good with files? Are you using FILE*'s or something?
im doing binary files
[edit]
and everytime i save the structure with the string in it the string will be all messed up thats y i use an array of chars
[/edit]
I've got this one licked!
Use an array of ints instead and fill each element with the ascii code instead of the letter.
To print it cast as char. :D
Give an example of how you are reading/writing to the file. Chances are you are making a *very* common mistake.
You can't save a string like that. It's an object. You need to save the strings data.Quote:
Originally posted by sPlOrYgOn
im doing binary files
[edit]
and everytime i save the structure with the string in it the string will be all messed up thats y i use an array of chars
[/edit]
string.data()
Give this link a chance ;)
i'm saving the files like this
and the array of ints sounds good but i dunno the number for the letters :(Code:fout.write((const char*)MyStructure, sizeof(Structure));
[edit]
i save strings just fine in binary files
[/edit]
my solution was kind of a joke I don't suggest you use it, but nonetheless, it is a 2minute job to display the ascii charset:
Code:#include <iostream>
using namespace std;
int main()
{
int x;
for(x=0 ; x<255 ; x++)
cout << "No. " << x << " \t" << static_cast<char>(x) << endl;
return 0;
}
i found a bigger chart
http://sPlOrYgOn.distanthost.com/Chart.php?min=0
only problem is that's not ascii.
>fout.write((const char*)MyStructure, sizeof(Structure));
Is MyStructure a pointer? If yes, then the above should work. Otherwise try:
fout.write((const char*)&MyStructure, sizeof(Structure));