Hi all, I'm trying to convert a console application that I wrote in Codeblocks to a Windows Form application written in Visual Studio .NET 2003. I'm having trouble converting a piece of code that reads in a formatted text file line by line (the text in the file is always consistent). My code in 'traditional' C++ is

while (infile2 >> a >> b >> c1 >> d >> e >> f >> g1){

    }

and my (current) code in Visual C++ is

if(openFileDialog1->ShowDialog() == DialogResult::OK){
		
                // consider file.open method?
		System::IO::StreamReader * sr = new
		System::IO::StreamReader(openFileDialog1->FileName);

		StreamReader* sr2 = File::OpenText(openFileDialog1->FileName);
		String* s = S"";
        
	           while (sr2->ReadLine() >> a >> b >> c1 >> d >> e >> f >> g1 ) {
		   }
		   }

Should I not use streamreader? I only chose this as it seemed an easy way to open a file from an openfiledialog box which precedes this piece of code. Or is there another method I should be using? Thanks in advance :)

You might be interested in std::fstream, but do whatever way is easiest for you.

Maybe I should have been more explicit, the VC++ code errors - and when I've tried ifstream with Windows forms, it doesn't seem to work (it only seems to work with console apps in Visual Studio .NET 2003?)

I don't know about 2003, I use 2008.
I've never had a problem with std::fstream (fstream is both in and out)

What is the error you are getting?

...\Form1.h(136) : error C2653: 'std' : is not a class or namespace name
...\Form1.h(136) : error C2065: 'fstream' : undeclared identifier

#include <fstream>

Check out http://msdn.microsoft.com/en-us/library/aa904305(VS.71).aspx to see how to use the Split() method of the string. You will form a string array with each of the different tokens at a separate index. Depending on what data type they are you may need to parse them (see http://msdn.microsoft.com/en-us/library/aa328656(v=VS.71).aspx for an example).

You are using the right method with the streamreader. I do not believe there is an overload of the >> operator for the string type in .NET.

#include <fstream>

Yep I have that in my main .cpp file. I'm sure I've read somewhere that Microsoft's implementation of it was different in Visual Studio forms?

Check out http://msdn.microsoft.com/en-us/libr...05(VS.71).aspx to see how to use the Split() method of the string. You will form a string array with each of the different tokens at a separate index. Depending on what data type they are you may need to parse them (see http://msdn.microsoft.com/en-us/libr...(v=VS.71).aspx for an example).

You are using the right method with the streamreader. I do not believe there is an overload of the >> operator for the string type in .NET.

thanks, I'll have a look at this later when I get in..

Yep I have that in my main .cpp file. I'm sure I've read somewhere that Microsoft's implementation of it was different in Visual Studio forms?

There are type incompatibilities between the managed and unmanaged code. All of the old libraries are considered unmanaged, so you have to "marshal" the data returned by the std methods.

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.