-1

I have declared an array: names[1000]; and another array, data[1000];, to store the data temporarily

and later used an ifstream to read data from an XML file.

then later, I used cin.getline(data, 300) to put the data into data[] array.

but when I assign data[] array to names[] array, an error occurs:

invalid operands of types char[1000] and char[1000] to binary operator>>

code:

char data[1000];
char names[1000];


ifstream openFile("myfile.xml");

if(!openFile)
{
    cout<<"File not found! please re-enter filename"<<endl;
}

while (openFile.getline (data, 300))
{
    if (data[0] == '<' && data[1] == 'n') // to only check the <name> xml tag
    {
        cout<<data<<endl;
        data >> names;
    }
}

Any idea why I cant assign data array to names array?

Thanks!

scohe001
  • 14,655
  • 2
  • 30
  • 50
taurette
  • 11
  • 1
  • 4

3 Answers3

1

">>" operator is usually defined for streams, but data is just an array. if you want to copy the content, use strncpy from string.h:

strncpy(names, data, 1000);

if you want to treat your string/array as stream, try stringstream.

BTW, you may want to use C++ string instead of character arrays -- it's more convenient (but not so efficient).

mariusm
  • 1,368
  • 1
  • 10
  • 24
  • @taurette if you are into XML, then please consider XML library in the long term, because reading line-by-line is not reliable (one can have line breaks in funny places). http://stackoverflow.com/questions/170686/best-open-xml-parser-for-c – mariusm Aug 25 '14 at 14:36
0

Because you can't assign arrays. At all.

You may want to take a look at std::vector or std::string, which can be assigned among other cool things.

If you want to stick to char arrays, you can do the following :

std::copy(std::begin(data), std::end(data), std::begin(names));

Or (to avoid copying trash after the 300th element) :

std::copy(std::begin(data), std::begin(data) + 301, std::begin(names));
Quentin
  • 60,592
  • 7
  • 125
  • 183
0

The operator >> is not used for assignment ! You could assign using strncpy like this

strncpy(names, data, 1000);

and add the include

#include <string.h>
webNeat
  • 2,591
  • 1
  • 17
  • 22
  • Array names are not lvalues, thus can't be assigned. – Quentin Aug 25 '14 at 14:20
  • I meant to assign pointers so they point to same container. But I see now that this will be a problem because the content will be erased when reading a new line. So `strncpy` is the solution. Editing my answer – webNeat Aug 25 '14 at 14:25
  • 2
    [**Array names are not pointers either**.](http://stackoverflow.com/questions/4607128/in-c-are-arrays-pointers-or-used-as-pointers) – Quentin Aug 25 '14 at 14:27
  • From link; "If the expression `a` (which is an `int [10]`) appears in a context other than as the operand of the sizeof or & operators, then its type is implicitly converted to `int *`, and its value is the address of the first element." – webNeat Aug 25 '14 at 14:35
  • Indeed. But a conversion yields an rvalue, a temporary if you prefer, which by definition can't be assigned for native types. – Quentin Aug 25 '14 at 14:37
  • I hope I helped you understand arrays better, they are confusing to many :) – Quentin Aug 25 '14 at 14:51