cliff_m
asked on
Questions about strings or (char arrays) and classes
I have several questions regarding the same thing - strings.
I am trying to read in at most 128 characters, null terminated. In fact, the string may not even contain anything, so it would be 128 null chars.
I have been able to do this and print it out to STDOUT. I want to assign this value (or a pointer) to an array in my class.
The last thing I want to do is see if the string contains anything, and if so print it out to a file.
Here is my code:
test.h
class Test {
private:
char StreetName[129];
};
test.cpp
test::GetData{
char StreetNameVal[129];
inFile.read(StreetNameVal, 128);
// Next line or some kind of pointer assign?
strcpy(StreetName,StreetNa meVal);
}
test::PrintData{
if(StreetName) {
XmlFile << "\t<StreetName>" << StreetName << "</StreetName>" << endl ;
} else {
XmlFile << "\t<StreetName/>" << endl ;
}
By the way I am new to C++ (but you knew that) so be nice. :)
Cliff
I am trying to read in at most 128 characters, null terminated. In fact, the string may not even contain anything, so it would be 128 null chars.
I have been able to do this and print it out to STDOUT. I want to assign this value (or a pointer) to an array in my class.
The last thing I want to do is see if the string contains anything, and if so print it out to a file.
Here is my code:
test.h
class Test {
private:
char StreetName[129];
};
test.cpp
test::GetData{
char StreetNameVal[129];
inFile.read(StreetNameVal,
// Next line or some kind of pointer assign?
strcpy(StreetName,StreetNa
}
test::PrintData{
if(StreetName) {
XmlFile << "\t<StreetName>" << StreetName << "</StreetName>" << endl ;
} else {
XmlFile << "\t<StreetName/>" << endl ;
}
By the way I am new to C++ (but you knew that) so be nice. :)
Cliff
ASKER
It doesn't work when I try to assign it to my class(private variable) and so far I have only been able to print hexadecimal character sequence to my xml file(obviously a reference of some sort).
What I am looking for, I guess, is the right way to do it, passing by reference instead of by value, and being able to dereference it to print out to a file.
Cliff
What I am looking for, I guess, is the right way to do it, passing by reference instead of by value, and being able to dereference it to print out to a file.
Cliff
>>strcpy(StreetName,Street NameVal); error
If string can contain 0x00, you code must be:
memcpy(StreetName,StreetNa meVal,128) ;
becouse strcpy copies only untill 1-st
0x00 (and 0, of course).
If string can contain 0x00, you code must be:
memcpy(StreetName,StreetNa
becouse strcpy copies only untill 1-st
0x00 (and 0, of course).
ASKER
I forgot to mention that the statement:
strcpy(StreetName,StreetNa meVal);
crashes the application. So does:
memcpy(StreetName,StreetNa meVal,128) ;
but I can cout << StreetName and get either a blank line or the text in that field.
BTW I don't need to read in the x00, I was just saying that it won't always be there to read in.
Cliff
strcpy(StreetName,StreetNa
crashes the application. So does:
memcpy(StreetName,StreetNa
but I can cout << StreetName and get either a blank line or the text in that field.
BTW I don't need to read in the x00, I was just saying that it won't always be there to read in.
Cliff
Should you be reading exactly 128 chars (always) or at most 128 chars? The Q contradicts itself.
Hi cliff_m
test::GetData{
char StreetNameVal[129];
streetNameVal[129] = '\0';
inFile.read(StreetNameVal, 128);
strcpy(StreetName,StreetNa meVal);
}
this I think will not give U assertion
any way what does this statement do?
XmlFile << "\t<StreetName>" << StreetName << "</StreetName>" << endl ;
test::GetData{
char StreetNameVal[129];
streetNameVal[129] = '\0';
inFile.read(StreetNameVal,
strcpy(StreetName,StreetNa
}
this I think will not give U assertion
any way what does this statement do?
XmlFile << "\t<StreetName>" << StreetName << "</StreetName>" << endl ;
Hi cliff_m,
Can't you use this :
test::GetData (void)
{
StreetName[0] = '\0';
inFile.read (StreetName, 128);
}
Now you skip the copy at all.
About the test.
Use strlen (StreetName) > 0 to check if
their is anything in the array.
The test
if(StreetName) { ...
will alway be true because StreetName is a value <> 0
Regards,
Toronado.
Can't you use this :
test::GetData (void)
{
StreetName[0] = '\0';
inFile.read (StreetName, 128);
}
Now you skip the copy at all.
About the test.
Use strlen (StreetName) > 0 to check if
their is anything in the array.
The test
if(StreetName) { ...
will alway be true because StreetName is a value <> 0
Regards,
Toronado.
>> inFile.read(StreetNameVal, 128);
works if the string is always stored as exactly 128 characters. Is that the case? Like if the string is short, oul it be padded to 128 characters with NUL characters? That woudl be unusual. Also you have room to store 129 characters, that is there is room in your array for 1 more. Do you neeed to be placing a NUL in the last position, like might this string sometimes be 128 characters that are not NUL, so you have to put a NUL in in the 129th?
I think there are probably two problems here, but can't tell for sure.
I.e a missing NUL and the assumption that the string is always the same length.
>> What I am looking for, I guess, is the right way to do it,
>> passing by reference instead of by value, and being able
>> to dereference it to print out to a file.
Passing what by reference? I don't see anything that is a candidate for that.
Perhaps you need to consider using a string class? They can be passed around extremely efficiently.
>> I forgot to mention that the statement:
>> strcpy(StreetName,StreetNa meVal);
>> crashes the application.
That will happen if the source, StreetNameVal, is not properly terminated with a NUL, as I suggested above this might be the case.
>> So does:
>> memcpy(StreetName,StreetNa meVal,128) ;
That is very surprising. Are you sure?
>> I can cout << StreetName and get either a blank line
>> or the text in that field.
"cout << " with a charater array only works if the character arrays is properly NUL terminated. I suspect yours is not.
>> I don't need to read in the x00
Then you should consider using the getline() function. it reads up to 1 line, regardless of its length.
works if the string is always stored as exactly 128 characters. Is that the case? Like if the string is short, oul it be padded to 128 characters with NUL characters? That woudl be unusual. Also you have room to store 129 characters, that is there is room in your array for 1 more. Do you neeed to be placing a NUL in the last position, like might this string sometimes be 128 characters that are not NUL, so you have to put a NUL in in the 129th?
I think there are probably two problems here, but can't tell for sure.
I.e a missing NUL and the assumption that the string is always the same length.
>> What I am looking for, I guess, is the right way to do it,
>> passing by reference instead of by value, and being able
>> to dereference it to print out to a file.
Passing what by reference? I don't see anything that is a candidate for that.
Perhaps you need to consider using a string class? They can be passed around extremely efficiently.
>> I forgot to mention that the statement:
>> strcpy(StreetName,StreetNa
>> crashes the application.
That will happen if the source, StreetNameVal, is not properly terminated with a NUL, as I suggested above this might be the case.
>> So does:
>> memcpy(StreetName,StreetNa
That is very surprising. Are you sure?
>> I can cout << StreetName and get either a blank line
>> or the text in that field.
"cout << " with a charater array only works if the character arrays is properly NUL terminated. I suspect yours is not.
>> I don't need to read in the x00
Then you should consider using the getline() function. it reads up to 1 line, regardless of its length.
consider
include <strig>
using namespace std;
class Test
{
private:
string StreetName; // stores any length string,
// passes it around efficiently.
};
// test.cpp
Test::GetData
{
geline(inFile,StreetName); // reads whole line
// regardless of length.
}
Test::PrintData
{
if(StreetName.length())
{
XmlFile << "\t<StreetName>" << StreetName << "</StreetName>" << endl ;
}
else
{
XmlFile << "\t<StreetName/>" << endl ;
}
}
include <strig>
using namespace std;
class Test
{
private:
string StreetName; // stores any length string,
// passes it around efficiently.
};
// test.cpp
Test::GetData
{
geline(inFile,StreetName);
// regardless of length.
}
Test::PrintData
{
if(StreetName.length())
{
XmlFile << "\t<StreetName>" << StreetName << "</StreetName>" << endl ;
}
else
{
XmlFile << "\t<StreetName/>" << endl ;
}
}
ASKER
About the data, string is always padded to 128 characters and will always contain a null so I was in error declaring a char array of 129(128+1). It should be 128(127+1).
I will look at proposed solutions and get back to all of you.
Cliff
I will look at proposed solutions and get back to all of you.
Cliff
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I appreciate all of the help. The main issue that I was dealing with was not terminating my char arrays. I am giving the points to ymeng because of his excellent answer.
Thank you,
Cliff
Thank you,
Cliff
What's the problem ???