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,StreetNameVal);
 }

 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
cliff_mAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

akalmaniCommented:
Hey cliff !!
 What's the problem ???
0
cliff_mAuthor Commented:
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
0
AlexVirochovskyCommented:
>>strcpy(StreetName,StreetNameVal); error
If string can contain 0x00, you code must be:
  memcpy(StreetName,StreetNameVal,128);
becouse strcpy copies only untill 1-st
0x00 (and 0, of course).


 
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

cliff_mAuthor Commented:
I forgot to mention that the statement:
 strcpy(StreetName,StreetNameVal);
crashes the application. So does:
 memcpy(StreetName,StreetNameVal,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
0
KangaRooCommented:
Should you be reading exactly 128 chars (always) or at most 128 chars? The Q contradicts itself.
0
inprasCommented:
Hi  cliff_m
test::GetData{
char StreetNameVal[129];
streetNameVal[129] = '\0';
inFile.read(StreetNameVal, 128);
strcpy(StreetName,StreetNameVal);
}

this I think will not give U assertion

any way what does this statement do?
XmlFile << "\t<StreetName>" << StreetName << "</StreetName>" << endl ;

0
ToronadoCommented:
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.
0
nietodCommented:
>> 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,StreetNameVal);
>>  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,StreetNameVal,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.
0
nietodCommented:
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 ;
   }
}
0
cliff_mAuthor Commented:
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
0
ymengCommented:
Cliff,

Since you are new to C++, I'll try to stay with your original code as close as possible. I'll point out the errs in your code and add comments using //**
The extra class members are only added for testing purpose. You should be able to cut/paste the code and run it.

#include <fstream.h>
#include <string.h>

//** const is the C++ style
const int BUFFER_LEN = 128;

//test.h
class Test {
public:
 Test();
 void PrintData();

private:  
  void GetData();
  char StreetName[BUFFER_LEN];
  ifstream inFile;
  ofstream XmlFile;
 };

//test.cpp

Test::Test()
{
  inFile.open("test.txt");

  if (inFile.is_open() == false)
        cout << "get problem";

  XmlFile.open("out.txt");
  GetData();
}
 
void Test::GetData()
{
  //** no need for it // char StreetNameVal[BUFFER_LEN ];
  //** null the string,
  //** read() will not null terminate your str

      memset(StreetName, 0, BUFFER_LEN);
      inFile.read(StreetName, BUFFER_LEN);
 
// Next line or some kind of pointer assign?

//** next line is dangerous, read() func
//** may not null terminate StreetNameVal
//  strcpy(StreetName,StreetNameVal);
 }

void Test::PrintData(){
 //** next test is wrong:
 //** StreetName is the address of the
 //** of the variable. It will always
 //** be non-zero, therefor always true
 // if(StreetName) {

 if (strlen(StreetName)) {
   XmlFile << "\t<StreetName>" << StreetName << "</StreetName>" << endl ;
  } else {
   XmlFile << "\t<StreetName/>" << endl ;
  }
}

void main()
{
  Test t;
 
  t.PrintData();

}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
cliff_mAuthor Commented:
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
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.