-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
  • 15,110
  • 2
  • 31
  • 51
taurette
  • 11
  • 1
  • 4
  • 1
    Why are you using C-style strings and not `std::string` ? – Paul R Aug 25 '14 at 14:15
  • I have to use array for checking ( [0] =='<' && [1] =='n' ) to check the start of the data if they consist certain characters... – taurette Aug 25 '14 at 14:19
  • You can, of course, do that with proper C++ strings (`std::string`) as well as pretty much anything else that you might want to do with old style C strings. The difference though is that it's a lot easier and a lot safer/more robust with `std::string`. – Paul R Aug 25 '14 at 14:21
  • 1
    @taurette You can easily do that with C++ strings. – devdot Aug 25 '14 at 14:22
  • 1
    @taurette you can do that with `std::string` (i.e. using `[]` for indexing, just check length first). – crashmstr Aug 25 '14 at 14:22
  • thanks! using std::string fixed my problem – taurette Aug 25 '14 at 16:23

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,483
  • 1
  • 11
  • 26
  • @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
  • 62,093
  • 7
  • 131
  • 191
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,768
  • 1
  • 20
  • 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