如何不覆盖文件,C ++日志记录

时间:2016-01-21 05:29:58

标签: c++ logging fstream

这是我的代码。每次我“保存”,它都会覆盖旧的txt文件。如何将新行或图集输出到同一文件到新文件。这是一个动态数组,我正在使用switch case。输入数据后,我想将其保存为文本文件。并在下次加载它。加载功能也很有效。

#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;
    }
}
    }}

2 个答案:

答案 0 :(得分:4)

在&#34中打开文件;追加&#34;模式。

save.open( "data.txt", ofstream::out | ofstream::app );

如果文件不存在,这将创建文件,否则将写指针放在文件的末尾。

您不必明确致电open。有一个构造函数可以帮到你:

ofstream save( "data.txt", ofstream::out | ofstream::app );

您也不需要致电close,因为这会在save遭到破坏时自动发生。

答案 1 :(得分:1)

您可能有两种情况: 1)在文件末尾附加数据 2)在文件中间插入数据

案例I:在文件末尾附加数据: 要在末尾附加数据,您应该以“APPEND”模式打开文件。

public void Highlight(int index,double ratio = 1 ) {
StopAllCoroutines(); // stop all co routines already running.
Transform[] bones = null;
switch (index) {
case (int)Skeleton.Head:
    bones = head;
    break;
case (int)Skeleton.UpperBody:
    bones = upperBody;
    break;
case (int)Skeleton.LowerBody:
    bones = lowerBody;
default:
    break;
 if(bones != null)
 {       
   startCoroutine(actualHighlight(bones));
 }

}
Ienumrator actualHighlight(Transform[] bones)
{
 if (smr != null) {
    var mesh = smr.sharedMesh;
    var weights = mesh.boneWeights;
    var colors = new Color32[weights.Length];
    var sums = new float[weights.Length];

    for (int j= 0; j<bones.Length; j++) {

        var idx = GetBoneIndex (bones [j]);

        for (int i = 0; i < colors.Length; ++i) {
            float sum = 0;
            if (weights [i].boneIndex0 == idx && weights [i].weight0 > 0)
                sum += weights [i].weight0;
            if (weights [i].boneIndex1 == idx && weights [i].weight1 > 0)
                sum += weights [i].weight1;
            if (weights [i].boneIndex2 == idx && weights [i].weight2 > 0)
                sum += weights [i].weight2;
            if (weights [i].boneIndex3 == idx && weights [i].weight3 > 0)
                sum += weights [i].weight3;
            sums [i] += sum;
            colors [i] = Color32.Lerp (regularColor, highlightColor, sums [i] * (float)ratio);
            mesh.colors32 = colors;
            yield return new WaitForEndOfFrame();
        }


        //Debug.Log("bone index:\t"+bones[j].ToString());
    }
} else {
    Debug.Log("smr null");
 }
}

案例II:在文件中间插入数据: 要在文件中间插入数据,您不能使用“APPEND”模式。使用其他模式打开文件并使用i.e. std::ofstream LogFile("TempFile.txt", std::ios_base::app | std::ios_base::out); ,它允许您提供偏移和方向。 e.g

seekp(offset,direction)