写入文件而不覆盖内容. [英] writing a file without overwriting the contents.
本文介绍了写入文件而不覆盖内容.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
用于在不覆盖已写入内容的情况下写入文件的代码.以下面的代码为例:
What code is used to write a file without overwriting the already written content. Taking an example the following code:
using namespace System;
using namespace System::IO;
int main()
{
//creating the file object data.txt
FileStream^ fs = gcnew FileStream("data.txt", FileMode::Create);
StreamWriter^ sw = gcnew StreamWriter(fileName);
try
{
//writing lines from a string and a textbox input
sw->WriteLine("First Name: "+ this->FnameTextBox->Text);
}
//Exception handler
catch (Exception^)
{
Console::WriteLine("data could not be written!");
fs->Close();
return -1;
}
fs->Close();
return 0;
}
上面的代码将覆盖文件data.txt的内容.
我应该添加什么代码才能从文本文件的下一行开始写新内容?
预先感谢您提供的任何帮助.
The above code overwrites the content of the file data.txt.
What code should I add in order to write new content starting from the next line of the text file?
Thanx in advance for any help that you''ll give me.
推荐答案
正确组合FileModes应该可以解决问题.在此处阅读有关信息: http://msdn.microsoft. com/de-de/library/system.io.filemode(v = VS.80).aspx [
A proper combination of FileModes should do the trick. Read about it here: http://msdn.microsoft.com/de-de/library/system.io.filemode(v=VS.80).aspx[^]
using namespace System;
using namespace System::IO;
int main()
{
// Writing the file object data.txt
// FileMode::Append only in combination with FileMod::Write
FileStream^ fs = gcnew FileStream("data.txt", FileMode::Write | FileMode::Append);
StreamWriter^ sw = gcnew StreamWriter(fileName);
try
{
//writing lines from a string and a textbox input
sw->WriteLine("First Name: "+ this->FnameTextBox->Text);
}
//Exception handler
catch (Exception^)
{
Console::WriteLine("data could not be written!");
fs->Close();
return -1;
}
fs->Close();
return 0;
}
最好的问候,
-MRB
Best Regards,
-MRB
更改
FileStream^ fs = gcnew FileStream("data.txt", FileMode::Create);
到
to
FileStream^ fs = gcnew FileStream("data.txt", FileMode::Append | FileMode::Write);
换句话说,以附加模式打开文件将使您可以在文件末尾写入文件.
In other words, open your file in append mode will allow you to write it at end of file.
这篇关于写入文件而不覆盖内容.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文