Hi Guys,

I am using visual studio 2009 using vc++. I am reading from a text file using a streamReader one line at a time. The data is tab delimited. I am trying to split the data and then stuff all the strings into struct. Here's what I am doing

struct
{
  std::string day;
  std::string month;
  std::string year:
}cal;

cal newRecord;

main()
{
  StreamReader^ file = gcnew StreamReader("c:\\txtFile.txt");
  String^ del = "\t"
  array<String^>^delim = del->ToCharArray();
  array<String^>^line = file->ReadLine()->Split(delim,3);
  newRecord = (cal*)line;
}

Data Read: 1\tjan\t2010

So every thing upto reading in the data and splitting it works. But when I try to type cast to the struct I get the error:
error c2440 type cast : cannot convert from 'cli::array<Type> ^ ' to 'cal*'

I am totaly stuck here. Any help will be appreciated.

You need to turn your struct into a managed one, otherwise you'll have to marshal it back and forth and that will give you a headache (if you have to do it per requirement search for the class Marshal on MSDN).
So something like:

value struct cal
	{
	String^ day;
	String^ month;
	String^ year;
	cal(String ^ day,String ^ month,String ^ year) :day(day),month(month),year(year){}
	
	};

which gives you a constructor to use when instantiating your structs. You can then probably make an array with the number of rows from your file and with 3 columns.

As it were you wouldn't be able to cast your properties out of the array all at once like that. newRecord = (cal*)line; The only other problem is that you need an array<char> to store your del->ToCharArray() call.

You need to turn your struct into a managed one, otherwise you'll have to marshal it back and forth and that will give you a headache (if you have to do it per requirement search for the class Marshal on MSDN).
So something like:

value struct cal
	{
	String^ day;
	String^ month;
	String^ year;
	cal(String ^ day,String ^ month,String ^ year) :day(day),month(month),year(year){}
	
	};

which gives you a constructor to use when instantiating your structs. You can then probably make an array with the number of rows from your file and with 3 columns.

As it were you wouldn't be able to cast your properties out of the array all at once like that. newRecord = (cal*)line; The only other problem is that you need an array<char> to store your del->ToCharArray() call.

Thanks for the reply. I actually do have array<char> to store del->ToCharArray(); It was a bad typing on my part. Thanks for making me check.

Now I have tried using String^ for my struct. It doesn't like it. Might have something to do with the fact that the struct is sitting in a xxxx.h file. VC moans and complains about it. I tried the setup you siggested, didn't work. Just got lot more errors.

Post your entire code (or at least the header and the file containing main).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.