473,411 Members | 2,068 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

how to write to a file?

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
Nov 17 '05 #1
6 1528
<Geoff Cox> wrote in message
news:gi********************************@4ax.com...
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
Nov 17 '05 #2
On Wed, 17 Aug 2005 15:52:29 -0400, "William DePalo [MVP VC++]"
<wi***********@mvps.org> wrote:

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[i]);
}
outfile.Close();

}

Thanks

Geoff




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


Nov 17 '05 #3
<Geoff Cox> wrote in message
news:f6********************************@4ax.com...
On Wed, 17 Aug 2005 15:52:29 -0400, "William DePalo [MVP VC++]"
<wi***********@mvps.org> wrote:

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");


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[i]);
Try

outfile->WriteLine(results[i]);
outfile.Close();


Try

outfile->Close();

Regards,
Will
Nov 17 '05 #4
On Wed, 17 Aug 2005 16:56:32 -0400, "William DePalo [MVP VC++]"
<wi***********@mvps.org> wrote:
The new C++/CLI (VS.2005) syntax is

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

outfile->WriteLine(results[i]);
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[i]);

}

outfile.Close(); // line 188

Any ideas?

Cheers

Geoff



Regards,
Will


Nov 17 '05 #5
On Wed, 17 Aug 2005 16:56:32 -0400, "William DePalo [MVP VC++]"
<wi***********@mvps.org> wrote:
<Geoff Cox> wrote in message
news:f6********************************@4ax.com.. .
On Wed, 17 Aug 2005 15:52:29 -0400, "William DePalo [MVP VC++]"
<wi***********@mvps.org> wrote:

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 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
Nov 17 '05 #6
On Thu, 18 Aug 2005 08:49:07 +0100, Geoff Cox <> wrote:
results[qnumber] = trackBar1_Scroll->Value->ToString();


Solved! It should have been

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

Geoff
Nov 17 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Ellixis | last post by:
Hello, How can I use fwrite() and fseek() in order to write data in the middle (or anywhere else) of a file without overwriting existing data ? People told me that I should load the file into...
5
by: Just Me | last post by:
Using streams how do I write and then read a set of variables? For example, suppose I want to write into a text file: string1,string2,string3 Then read them later. Suppose I want to write...
6
by: sambuela | last post by:
How can I write message to the file located in the wwwroot directory? It seems that IIS protect these files. Let make me cannot do the I/O writing sucessfully. I try to open file's write...
2
by: ykgoh | last post by:
Hi. I've a problem of being able to create and remove a directory but unable to write a file inside the created directory for some strange reason. I suspect that this problem could be vaguely...
5
by: philip | last post by:
Here is some lines of code than I wrote. You can copy/paste theis code as code of form1 in a new project. My problem is this one : I try to write in a file a serie of bytes. BUT some bytes...
1
by: =?Utf-8?B?R2FuZXNoIE11dGh1dmVsdQ==?= | last post by:
Hello All, Our application write logs to a file in a folder. Before our application starts writing to that file, I want to check if the current user has write access to that file, for example,...
0
by: Buddy Home | last post by:
Hello, I'm trying to upload a file programatically and occasionally I get the following error message. Unable to write data to the transport connection: An established connection was aborted...
3
by: Buddy Home | last post by:
Hello, I'm trying to upload a file programatically and occasionally I get the following error message. Unable to write data to the transport connection: An established connection was aborted...
6
by: globalrev | last post by:
i ahve a program that takes certain textsnippets out of one file and inserts them into another. problem is it jsut overwrites the first riow every time. i want to insert every new piece of...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.