how to write to a file?

  • Thread starter Thread starter Geoff Cox
  • Start date Start date
G

Geoff Cox

Hello,

In C# I can write

TextWriter tw = new StreamWriter("d:\\path\\file.txt");

tw.WriteLine("fred");

tw.Close;



What is the C++ equivalent for Visual C++ ?

Thanks

Cheers

Geoff
 
In C# I can write

TextWriter tw = new StreamWriter("d:\\path\\file.txt");

tw.WriteLine("fred");

tw.Close;



What is the C++ equivalent for Visual C++ ?

Equivalent at what level?

1) You can use the TextWriter and StreamWriter classes in Managed C++ and
with C++/CLI that is included in VS.Net 2005 if you tatget the .Net platform

2) You can also make use of the standard I/O streams classes in C++, e.g.

#include <iostream>
#include <fstream>

fstream os;

os.open("c:\\test.dat2", ios_base::out);
os.write("fred\r\n", 6);
os.close();

3) You can make use of the Win32 I/O functions

CreateFile()
WriteFile()
CloseHandle()

Regards,
Will
 
1) You can use the TextWriter and StreamWriter classes in Managed C++ and
with C++/CLI that is included in VS.Net 2005 if you tatget the .Net platform

Will,

I guess I would like to use above approach but not at all clear how to
do it .... my effort below is clearly wrong for Visual C++ 2005
Express - can you please give me an idea of how it should be done?

private: System::Void endMessage() {

TextWriter outfile = new StreamWriter("h:\\a-temp1\\data.txt");

for (int i=0; i < LHSquestions->Length; i++) {
outfile.WriteLine(results);
}
outfile.Close();

}

Thanks

Geoff
 
Will,

I guess I would like to use above approach but not at all clear how to
do it .... my effort below is clearly wrong for Visual C++ 2005
Express - can you please give me an idea of how it should be done?

private: System::Void endMessage() {

TextWriter outfile = new StreamWriter("h:\\a-temp1\\data.txt");

The old MC++ syntax is

TextWriter *outfile = new StreamWriter("h:\\a-temp1\\data.txt");

The new C++/CLI (VS.2005) syntax is

TextWriter ^outfile = gcnew StreamWriter("h:\\a-temp1\\data.txt");
outfile.WriteLine(results);
Try

outfile->WriteLine(results);

outfile.Close();


Try

outfile->Close();

Regards,
Will
 
The new C++/CLI (VS.2005) syntax is

TextWriter ^outfile = gcnew StreamWriter("h:\\a-temp1\\data.txt");
outfile.WriteLine(results);
Try

outfile->WriteLine(results);

outfile.Close();


Try

outfile->Close();


Will,

Thanks for the above. I am getting a couple of errors ...

line 155 error C2227 left of '->ToString' must point to
class/struct/union/generic type
line 181 error C2228 left of '.Close' must have class/struct/union

I have following for initialization which I think is OK.


private: static array<String^>^ LHSquestions = gcnew array<String^>
{"question 1", "question 2"};

private: static array<String^>^ RHSquestions = gcnew array<String^>
{"question 1", "question 2"};

private: static int qnumber = 0;

private: static array<String^>^ results = gcnew array<String^>
[LHSquestions->Length];



and for the first error message

this->results[qnumber] = trackBar1->Value->ToString(); // line 155



and for the second error message

TextWriter^ outfile = gcnew StreamWriter("h:\\a-temp1\\data.txt");

for (int i=0; i < LHSquestions->Length; i++) {


outfile->WriteLine(results);

}

outfile.Close(); // line 188

Any ideas?

Cheers

Geoff
 
Will

I have found the cause of the 2nd one - I had outfile.Close() instead
of outfile->Close()

also found error in the other code but still get 2 similar error
messages

results[qnumber] = trackBar1_Scroll->Value->ToString();

(I had trackBar1 not trackBar1_Scroll)

error messages re above

error C2227: left of '->Value' must point to
class/struct/union/generic type
error C2227: left of '->ToString' must point to
class/struct/union/generic type

thought?

Cheers

Geoff
 
Back
Top