3个回答
展开全部
using namespace System;
using namespace System::IO;
using namespace System::Text;
int main()
{
String^ path = "c:\\temp\\MyTest.txt"; //文件的路径
{
FileStream^ fs = File::Create( path ); //创建文件
try
{
AddText( fs, "This is some text" );
AddText( fs, "This is some more text," );
AddText( fs, "\r\nand this is on a new line" );
AddText( fs, "\r\n\r\nThe following is a subset of characters:\r\n" );
for ( int i = 1; i < 120; i++ )
{
AddText( fs, Convert::ToChar( i ).ToString() );
if ( Math::IEEERemainder( Convert::ToDouble( i ), 10 ) == 0 )
{
AddText( fs, "\r\n" );
}
}
}
finally
{
if ( fs )
delete (IDisposable^)fs;
}
}
{
FileStream^ fs = File::OpenRead( path );//打开文件,后面代码是完成读文件内容
try
{
array<Byte>^b = gcnew array<Byte>(1024);
UTF8Encoding^ temp = gcnew UTF8Encoding( true );
while ( fs->Read( b, 0, b->Length ) > 0 )
{
Console::WriteLine( temp->GetString( b ) );
}
}
finally
{
if ( fs )
delete (IDisposable^)fs;
}
}
}
void AddText( FileStream^ fs, String^ value ) //写入文件
{
array<Byte>^info = (gcnew UTF8Encoding( true ))->GetBytes( value );
fs->Write( info, 0, info->Length );
}
这够全了吧 =。=
using namespace System::IO;
using namespace System::Text;
int main()
{
String^ path = "c:\\temp\\MyTest.txt"; //文件的路径
{
FileStream^ fs = File::Create( path ); //创建文件
try
{
AddText( fs, "This is some text" );
AddText( fs, "This is some more text," );
AddText( fs, "\r\nand this is on a new line" );
AddText( fs, "\r\n\r\nThe following is a subset of characters:\r\n" );
for ( int i = 1; i < 120; i++ )
{
AddText( fs, Convert::ToChar( i ).ToString() );
if ( Math::IEEERemainder( Convert::ToDouble( i ), 10 ) == 0 )
{
AddText( fs, "\r\n" );
}
}
}
finally
{
if ( fs )
delete (IDisposable^)fs;
}
}
{
FileStream^ fs = File::OpenRead( path );//打开文件,后面代码是完成读文件内容
try
{
array<Byte>^b = gcnew array<Byte>(1024);
UTF8Encoding^ temp = gcnew UTF8Encoding( true );
while ( fs->Read( b, 0, b->Length ) > 0 )
{
Console::WriteLine( temp->GetString( b ) );
}
}
finally
{
if ( fs )
delete (IDisposable^)fs;
}
}
}
void AddText( FileStream^ fs, String^ value ) //写入文件
{
array<Byte>^info = (gcnew UTF8Encoding( true ))->GetBytes( value );
fs->Write( info, 0, info->Length );
}
这够全了吧 =。=
追问
编译器通不过,VC++6.0的
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
定义一个fstream或者ofstream
然后和一个文件关联。就可以用<<那样了
ofstream fout;
fout.open("file",ios_base::out);
fout<<"132123"<<endl;
fout.close();
然后和一个文件关联。就可以用<<那样了
ofstream fout;
fout.open("file",ios_base::out);
fout<<"132123"<<endl;
fout.close();
更多追问追答
追问
我的原始代码很简单#include
#include
int main()
{
using namespace std;
string words;
while(cin>>words&&words!="q")
cout<<words;
return 0;
}
就是想把这个输出流写到文件里,大神你帮个忙,帮我实现哈,我不会文件...
追答
#include
#include
#include
using namespace std;
void main()
{
string s;
char file[30];
ofstream fout;
cout>file;
cout>s;
fout.open(file,ios::out);
if(s!="q")
{
fout<<s;
}
fout.close();
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2011-09-14
展开全部
路过
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询