Hi,
Earlier i was working with VC++.net code to read contents of a file & copy it to another file. Now i want to perform the same task using c#.net. pls help me.. The code in vc++.net was
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
String^ s;
FileStream ^stmGrades = gcnew FileStream("CFG.txt",FileMode::Create, FileAccess::Write);
StreamWriter ^bnwGrades = gcnew StreamWriter(stmGrades);
// Create a FileInfo object based on the file
FileInfo ^fleLoan = gcnew FileInfo(openFileDialog1->FileName);
// Open the file
StreamReader ^stmLoan = fleLoan->OpenText();
//Read each line in the selected file & assign/store that line to string pointer s till null or EOF is reached
while(s = stmLoan->ReadLine()) // THIS LINE I WANT TO MODIFY TO C#
{
bnwGrades->WriteLine(); //write the line in the created file
}
}
stmLoan->Close();
bnwGrades->Close();
}
Loading
AlanPosted Oct 11, 2007, 5:26 AM
I must confess I'd only looked at the 'while' clause :)
However, bearing in mind that both languages are calling the same framework method (StreamWriter.WriteLine()) , I don't see how that line (without the 's') could have worked in C++/CLI either?
beginnerPosted Oct 11, 2007, 5:06 AM
bnwGrades->WriteLine(s); . That "s" was not reqd in vc++ , in c# without "s" the file was empty.
AlanPosted Oct 11, 2007, 4:51 AM
Jan's first solution will work but he's forgotten to put the brackets around the first expression which will lead to string/bool conversion errors. It should be:
while((s = stmLoan.ReadLine()) != null)
This is one of those niggling differences between C# and C++/CLI. When using the former you always need to explicitly test for null or zero which, IMO, is no bad thing :)
Jan MontanoPosted Oct 11, 2007, 4:26 AM
{
bnwGrades.WriteLine(); //write the line in the created file
}
if above code doesn't work, try
while(!stmLoan.EndOfStream) // THIS LINE I WANT TO MODIFY TO C#
{
s = stmLoan.ReadLine();
bnwGrades.WriteLine(); //write the line in the created file
}