I try to write a char[] to a binary file, this is my method, for an int, a float, a double, and a char*
friend ofstream& operator<<(ofstream& outB, Test& t) {
//scriem int, float si double normal
outB.write((char*)&t.i, sizeof(int));
outB.write((char*)&t.f, sizeof(float));
outB.write((char*)&t.d, sizeof(double));
//scriem stringul ca un char ( +1 pentru \0)
outB.write(t.s.c_str(), t.s.size() + 1);
//scriem charul
int nrCHAR = strlen(t.c) + 1;
outB.write((char*)&nrCHAR, sizeof(int));
outB.write(t.c, strlen(t.c) + 1);
return outB;
}
I can do it like this?
outB.write(c, strlen(c) + 1);
c being my char[]
Usually, text fields are variable length, so you will have to write the length and the text. Here's my suggestion:
const uint32_t length = strlen(c);
outB.write((char *) &length, sizeof(length));
outB.write(c, length);
When reading, you can do the reverse:
const uint32_t length = 0;
inpB.read((char *) &length, sizeof(length));
char * text = new char [length + 1];
inpB.read(&text[0], length);
text[length] = '\0';
A nice feature of writing the length first, is that you can allocate dynamic memory for the string before reading the text. Also, since the length of the text is known, a block read can be performed.
When using a sentinel character, like '\0', you have to read character by character until the sentinel is read. This is slow, really slow. You may also have to reallocate your array, since you don't know the size of the string.
Related
I need to write float price on a file using write function not << operator but it crashes
int addDevice(fstream & f, Device & d)
{
char buffer[200];
strcpy(buffer, d.id);
strcat(buffer, "#");
strcat(buffer, d.name);
strcat(buffer, "#");
short length = strlen(buffer) + sizeof(d.price);
char c = '$';
f.seekp(0, ios::end);
f.write((char*) & c, 1);
f.write((char*)&length, sizeof(length));
f.write(buffer, length);
f.write((char *) & d.price, sizeof(float));
f.write((char *)'#', 1);
f.seekp(0, ios::beg);
return length;
}
The problem is likely to be the line
f.write((char *)'#', 1);
This reinterprets the numerical value of '#' (i.e., 35) as a memory address, dereferences that address and would write the byte at that address into f if that address were valid, which it isn't. This is where you get undefined behaviour and probably a crash (although you can't depend on it that this sort of thing always crashes).
I think you want
f.put('#');
I'm trying to make my own string class, and I'm trying to create the constructor. So far I'm having a little struggle.
I have my function prototypes defined in a header file and the variable buffer defined in the header file like this char *buffer;, in a new cpp file I'm trying to define the constructor like this:
mystring::mystring(const char *s)
{
int counter = strlen(s)+1;
*buffer = generate_c_array(counter);
}
char * mystring::generate_c_array(int size)
{
return new char[size];
}
I'm basically trying to make it such that buffer is a char array of size s+1 and contains the contents of s followed by a null terminator. Though, this is causing my program to crash.
I'm not sure how to assign buffer correctly, does this look correct?
You can simply write your code
mystring::mystring(const char *s)
{
sz = strlen(s) // sz- another data member, holds size
buffer = new char[sz + 1];
strcpy(buffer, s); // assuming s is null-terminated
}
The for loop and counter were not necessary, in fact
You assign to the specific character in the buffer, not to the variable buffer. Change to:
buffer = generate_c_array(counter);
Other than that, strdup will do the same job for you, but you will have to deallocate with free(). In current code you actually don't really copy, you will have to do strcpy(buffer, s) unless using the strdup mentioned earlier.
Why are you looping over length of the string s?
If you need length of the string use it directly:
std::size_t length = strlen(s);
buffer = generate_c_array(length + 1);
// Copy your string:
memcpy(buffer, s, length)
// Add the terminator character:
buffer[length] = '\0';
You haven't copied the content..
If you want to assign a pointer ( which is what new give you ) to another pointer, you don't need *.
strncpy do the copying for you.
mystring::mystring(const char *s)
{
int counter = strlen(s)+1;
char* buffer = generate_c_array(counter);
strncpy(buffer, s, counter);
}
char * mystring::generate_c_array(int size)
{
return new char[size];
}
char *readByteArray() {
unsigned char l = readByte (); // reads one byte from the stream
char ret[l + 1]; // this should not be done
ret[0] = l; // or could also define a struct for this like {int length, char *data}
readBytes((char *)&ret + 1, l);
return (char *)&ret;
}
So the problem is, that I want to return an array, and the length of the array is determined by the function.
A better example of this would be the function I use for reading a string:
char *readString () {
unsigned char l = readByte (); // reads one byte from the stream
char ret[l + 1]; // +1 for null byte
readBytes((char *)&ret, l); // reads l bytes from the stream
ret[l] = '\0'; // null byte
return (char *)&ret; // problem
}
If the length of the array would be determined before the function I could allocate the array outside the function and pass it as a parameter, but calling this:
unsigned char l = readByte ();
char ret[l + 1];
readString (&ret, l);
every time I want to read a string would kind of defeat the purpose of the function.
Is there an elegant solution for this on windows AND ATmega328 (STL is not available)?
One of the following options should work:
Return a pointer to an array of char allocated from the heap. Make sure to delete the returned value in the calling function.
char* readByteArray()
{
unsigned char l = readByte();
char *ret = new char[l + 1];
ret[0] = l;
readBytes(ret + 1, l);
return ret;
}
Return a std::vector<char>.
std::vector<char> readByteArray()
{
unsigned char l = readByte();
std::vector<char> ret(l);
readBytes(ret.data(), l);
return ret;
}
like the topic mention.... for example
int[10] msg;
msg[0] = 1;
msg[1] = 2;
const char* a = (const char*)msg[0];
const char* b = (const char*)msg[1];
It seem there is no value when I test by printf
I'm going to use it this way
char test[20];
strcpy(test, a);
strcat(test, ",");
strcat(test, b);
strcat(test, "\0");
mclient.publish("topic1/sensorAck",test);
The result show only comma
strcpy and strcat both stop when they reach a '\0' character in the source string. Since int 1 is actually 4 bytes (0, 0, 0, 1), it will stop on the first byte, because it's zero '\0' and never reach the '1' value byte.
Here's the C code to convert an integer into a C string.
int i = 1;
char text[12];
sprintf(text, "%d", i);
I've tried so may ways on the Internet to append a character to a char* but none of them seems to work. Here is one of my incomplete solution:
char* appendCharToCharArray(char * array, char a)
{
char* ret = "";
if (array!="")
{
char * ret = new char[strlen(array) + 1 + 1]; // + 1 char + 1 for null;
strcpy(ret,array);
}
else
{
ret = new char[2];
strcpy(ret,array);
}
ret[strlen(array)] = a; // (1)
ret[strlen(array)+1] = '\0';
return ret;
}
This only works when the passed array is "" (blank inside). Otherwise it doesn't help (and got an error at (1)). Could you guys please help me with this ? Thanks so much in advanced !
Remove those char * ret declarations inside if blocks which hide outer ret. Therefor you have memory leak and on the other hand un-allocated memory for ret.
To compare a c-style string you should use strcmp(array,"") not array!="". Your final code should looks like below:
char* appendCharToCharArray(char* array, char a)
{
size_t len = strlen(array);
char* ret = new char[len+2];
strcpy(ret, array);
ret[len] = a;
ret[len+1] = '\0';
return ret;
}
Note that, you must handle the allocated memory of returned ret somewhere by delete[] it.
Why you don't use std::string? it has .append method to append a character at the end of a string:
std::string str;
str.append('x');
// or
str += x;
The function name does not reflect the semantic of the function. In fact you do not append a character. You create a new character array that contains the original array plus the given character. So if you indeed need a function that appends a character to a character array I would write it the following way
bool AppendCharToCharArray( char *array, size_t n, char c )
{
size_t sz = std::strlen( array );
if ( sz + 1 < n )
{
array[sz] = c;
array[sz + 1] = '\0';
}
return ( sz + 1 < n );
}
If you need a function that will contain a copy of the original array plus the given character then it could look the following way
char * CharArrayPlusChar( const char *array, char c )
{
size_t sz = std::strlen( array );
char *s = new char[sz + 2];
std::strcpy( s, array );
s[sz] = c;
s[sz + 1] = '\0';
return ( s );
}
The specific problem is that you're declaring a new variable instead of assigning to an existing one:
char * ret = new char[strlen(array) + 1 + 1];
^^^^^^ Remove this
and trying to compare string values by comparing pointers:
if (array!="") // Wrong - compares pointer with address of string literal
if (array[0] == 0) // Better - checks for empty string
although there's no need to make that comparison at all; the first branch will do the right thing whether or not the string is empty.
The more general problem is that you're messing around with nasty, error-prone C-style string manipulation in C++. Use std::string and it will manage all the memory allocation for you:
std::string appendCharToString(std::string const & s, char a) {
return s + a;
}
char ch = 't';
char chArray[2];
sprintf(chArray, "%c", ch);
char chOutput[10]="tes";
strcat(chOutput, chArray);
cout<<chOutput;
OUTPUT:
test