如何不覆盖文件,C++日志记录
How to not overwrite the file, C++ logging
这是我的代码。每次我"保存"时,它都会覆盖旧的 txt 文件。如何将新行或图集输出到同一文件中的新文件。这是一个动态数组,我正在使用开关大小写。输入数据后,我想将其保存到文本文件中。并在下次将其加载回去。加载功能也运行良好。
#include<iostream>
#include<string>
#include<fstream> //to save file in text
using namespace std;
int main ()
{
int *p1;
int size=0;
int counter=0;
p1 = new int[size];
int userchoice;
int i;
int position;
while(1)
{
cout << "Please enter your choice " << endl;
cout<<endl;
cout << "To insert Press '1'" << endl;
cout << "To Delete press '2'" << endl;
cout << "To View press '3'" << endl;
cout << "To Search press '4'" << endl;
cout << "To Save Press '5'" << endl;
cout << "To Load Previously saved Data press '6'" << endl;
cout << "To Exit press '7'" << endl;
cout << endl;
cin>>userchoice;
switch(userchoice)
{
case 1:
cout<<"Enter a Number -->";
cin>>p1[size];
counter++;
size++;
break;
case 2:
int udelete;
cout<<"Enter a number to delete --> ";
cin>>udelete;
for(position = 0; position<size; position++)
{
if (p1[position] == udelete)
break;
}
if(position>size)
{
cout<<"The number is not in the memory ";
cout<<endl;
break;
}
for(i = position; i<size; i++){
p1[i]=p1[i+1];
}
size--;
cout<<"Successfully Deleted!!! ";
cout<<endl;
break;
case 3:
for (i=0; i<size; i++)
{
cout<<"Your data" <<" " << i << " " << "-->" <<p1[i]<<endl;
}
break;
case 4:
{
int usearch;
cout<<"Please enter the figure you would like to search ";
cout<<"->";
cin>>usearch;
for(i=0; i>size; i++)
{
if (p1[size]==usearch)
break;
}
if(usearch==size)
{
cout<<"not found";
}
cout<<"Position at: "<<i+1<<endl;
break;
}
case 5:
{
ofstream save;
save.open("data.txt");
for (i=0; i<size; i++)
{
save <<p1[i] <<endl;
}
save.close();
cout<<"File Saved "<<endl;
break;
}
case 6:
{
string read;
ifstream file_("data.txt");
if (file_.is_open())
{
while(getline(file_,read))
{
cout << read << "n";
}
file_.close();
}
else
cout << "File Not open" << endl;
cin.get();
break;
}
case 7:
{
return 0;
}
}
}}
在"追加"模式下打开文件。
save.open( "data.txt", ofstream::out | ofstream::app );
这将创建文件(如果不存在),否则将写入指针放置在文件末尾。
您不必显式调用open
。 有一个构造函数可以为您完成此操作:
ofstream save( "data.txt", ofstream::out | ofstream::app );
您也不需要调用 close
,因为这会在save
被销毁时自动发生。
您可能有两种情况:1) 在文件末尾附加数据2)在文件中间插入数据
案例 I:在文件末尾附加数据:要在末尾附加数据,您应该在"追加"模式下打开文件。
i.e. std::ofstream LogFile("TempFile.txt", std::ios_base::app | std::ios_base::out);
案例二:在文件中间插入数据:要在文件中间插入数据,不能使用"追加"模式。使用其他模式打开文件并使用允许您提供偏移量和方向的seekp(offset,direction)
。例如
fstream LogFile("TempFile.txt", std::ios_base::binary | std::ios_base::out | std::ios_base::in);
LogFile.seekp(10, ios::beg);
LogFile.write("abc", 4);
相关文章:
- EvtExportLogneneneba API正在将远程计算机的事件日志保存到远程PC本身.如何将其保存到主机
- 是否可以配置提升日志刷新?
- 跟踪日志中的T.11803()是什么意思?
- 加快在C++中读取/处理日志文件的速度
- 如何将消息时间戳写入日志文件?
- 分析包含 NMEA 句子的日志文件C++
- 如何修复输出日志中的"EnableInput can only be specified on a Pawn for its Controller"错误
- 如何实现具有多个平台__FILE__和__LINE__信息的 C/C++ 可变参数日志记录宏?
- Log4net,将日志消息从 c++ dll 发送到 c# 应用程序?
- 返回ERROR_INVALID_PARAMETER的事件日志函数
- 为什么系统日志有两个不同的函数声明?
- 从更改日志获取最新的 USN 数据
- 如何从日志文件中抓取状态代码?(在 C++ 中)
- 未创建日志文件
- C++ 中混合二进制/文本日志记录的最佳做法
- 将日志宏转换为目标 C 字符串
- 轻松日志记录++如何避免多个初始化
- 尝试使用 EvtSetChannelConfigProperty() 函数更新最大事件日志文件大小时插入的错误值
- 输出 c++ 窗口 wstring 日志在终端中正确
- 查询事件日志.如何查询才能单独获取第一个和最后一个事件?