Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I have the following code

if (StartMonitor) //StartMonitor
{

    TotalVoltage5s += CalculatePower();
    //TotalVoltage5s += 20;
    xx123++;

    CString sad;
    m_power_edit.GetWindowText(sad);

    int num1 = _ttoi(sad);
    if(xx123 >= num1){
        if(TotalTime > 19){

            Power_Log_Chart.ClearChart();
            TotalTime = 0;

        }

        CTime tm;
        tm=CTime::GetCurrentTime();
        CString str=tm.Format("%X");
        ostringstream os;
        float ds= (float)atof((char *)(LPTSTR)(LPCTSTR)sad);
        os << TotalVoltage5s/ds;
        ostringstream os1;
        m_power_list.InsertColumn(Current_columns,"",LVCFMT_CENTER,80,0);
        m_power_list.SetItemText(0, Current_columns, str);
        m_power_list.SetItemText(1, Current_columns, os.str().c_str());
        m_power_list.SetItemState(Current_columns, LVIS_SELECTED|LVIS_FOCUSED, LVIS_SELECTED|LVIS_FOCUSED);
        m_power_list.EnsureVisible(Current_columns, FALSE);


        Power_Log_Chart.SetXYValue(TotalTime, TotalVoltage5s/ds, TotalTime, 0);
        TotalTime++;
        TotalVoltage5s = 0;


What it simply does, it takes the calculated value of Calculatepower() function and display the results in every 5 seconds using Totalvoltage5s and and TotalTime. I need a way to save these data in a txt file every 19 seconds.

What I have tried:

I have tried to save the data using a FOR loop but I keep getting an error.
Posted
Updated 22-Aug-18 14:33pm
Comments
jeron1 2-Aug-18 13:45pm    
and that error would be?
Baderd94 2-Aug-18 13:54pm    
that is what i tried to do,

for (int q =0; q< 19; q++)
{
SavePowerData[q] = TotalVoltage5s;
}

but when i display the data, it gives no results so empty file, where SavePowerData is a global variable
jeron1 2-Aug-18 14:06pm    
What file are you referring to?
Patrice T 2-Aug-18 14:13pm    
and the error message is ?
Baderd94 2-Aug-18 14:12pm    
I have the following code

for (int q =0; q< 19; q++)
{
SavePowerData[q] = TotalVoltage5s;
SaveTimeData[q] = TotalTime;
}

To store the data, then i have another function called onsavedata, in side this function i put the following

CString str10;
CFileDialog FileDlg10(FALSE," ","Power Log Data.txt",OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT , "TXT File(*.txt)|*.txt|All(*.*)|*.*|");
if(FileDlg10.DoModal()==IDOK)
{
ofstream ofs(FileDlg10.GetPathName());
ofs.write(str10,str10.GetLength());
ofs.close();
AfxMessageBox("Saved!");
}
else
{
AfxMessageBox("Failed to Save!");
}
ofstream PowerData(FileDlg10.GetPathName(), ios::out);

PowerData<<SavePowerData<<endl;

PowerData.close();

No need for a for loop, the following code will save the data for us in an excel file:

std::ofstream outfile;
outfile.open("Real-Time.csv", std::ios_base::app);
outfile<<TotalTime<<", "<<os.str().c_str()<<", "<<os1.str().c_str()<<endl;
outfile.close(); 
 
Share this answer
 
This code works for me:


std::ofstream outfile;
outfile.open("Real-Time.csv", std::ios_base::app);
outfile<<TotalTime<<", "<<os.str().c_str()<<", "<<os1.str().c_str()<<endl; 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900