PDA

View Full Version : Strage problem Writing to a Notepad file



nagabathula
23rd June 2011, 07:42
Hello every one. I have a small problem i am writing to a notepad file every 30 seconds. Everything works fine but after some time there is some extra text written after the end of file i am actually updating the file with temperature data which i acquire and dumb into data base.


void datadisplay::datatcplog()
{
newfilenames = newdatpath; //"E:/DataLog/"+ QDateTime::currentDateTime().toString("ddd dd-MM-yyyy, hh.mm") +".txt";

QSqlQuery sq1("SELECT "+ colames +" FROM thdata where rowid = (select max(rowid) from thdata)");

while(sq1.next())
{
for (int i = 0; i < ntnewtempchnames.count(); i++)
{
txtdata.push_back(sq1.value(i).toString());
}
}

qDebug()<<"NotePad Log file Temperature Data:"<< txtdata;

QString notepadfl;
notepadfl.clear();
notepadfl.append(QTime::currentTime().toString("hh:mm:ss"));
numbch=ntnewtempchnames.count();

file5 = new QFile(newfilenames);
file5->open(QFile::ReadWrite | QFile::Text);

QTextStream out8(file5);

out8 <<"START OF FACILITY DATA" ;
out8 <<"\n" <<"";
out8<<notepadfl<<' '<<newnote<<"\n";
out8 <<"\n" <<"";

out8 <<"VACUUM PARAMETERS";
out8 <<"\n" <<"";
out8 <<"0" <<"";
out8 << vaccumparameters.size();
out8 <<"\n" <<"";

out8 <<"\n" <<"";
out8 <<"MOTION SIMULATOR PARAMETERS";
out8 <<"\n" <<"";
out8 <<"0" ;
out8 <<"\n" <<"";

out8 <<"AYDIN VECTOR PARAMETERS";
out8 <<"\n" <<"";
out8 <<ntnewtempchnames.count();
out8 <<"\n" <<"";
for(int d = 0;d < ntnewtempchnames.count();d++)
{
out8 << nttempidnames.at(d)<<' '<<txtdata.at(d)<< "\n";
}
out8 <<"\n" <<"";

out8 <<"END OF FACILITY DATA";
file5->close();
nttempidnames.clear();
txtdata.clear();
}

This is the text file which is generated but after some time i get some extra text written more then what i write in the program.


START OF FACILITY DATA
10:21:39
VACUUM PARAMETERS
0
MOTION SIMULATOR PARAMETERS
0
AYDIN VECTOR PARAMETERS
10
TE 5000_1 -120
TE 5000_2 -120
TE 5000_3 -120
TE 5000_4 -120
TE 5000_5 -120
TE 5000_6 -120
TE 5000_7 -120
TE 5000_8 -120
TE 5000_9 -120
TE 5000_10 -120
END OF FACILITY DATA



START OF FACILITY DATA
10:21:39
VACUUM PARAMETERS
0
MOTION SIMULATOR PARAMETERS
0
AYDIN VECTOR PARAMETERS
10
TE 5000_1 -120
TE 5000_2 -120
TE 5000_3 -120
TE 5000_4 -120
TE 5000_5 -120
TE 5000_6 -120
TE 5000_7 -120
TE 5000_8 -120
TE 5000_9 -120
TE 5000_10 -120
END OF FACILITY DATA
// But i get some extra text below after this i am not actually writing any thing after END OF FACILITY DATA but then this extra text is written.
0
TE 5000_606 -150
TE 5000_626 -150

DIRECT THERMOCOUPLE PARAMETERS
0
END OF FACILITY DATA
TE 5000_606 -10.3
TE 5000_626 -10.3

DIRECT THERMOCOUPLE PARAMETERS
0
END OF FACILITY DATA49.8

DIRECT THERMOCOUPLE PARAMETERS
0
END OF FACILITY DATA

Don't know whats wrong here. Some can see some thing wrong in the program. ?

Thank you

Santosh Reddy
23rd June 2011, 07:55
try adding these at the end


out8 <<"END OF FACILITY DATA";
out8.flush(); // Generally not required
file5->close();
delete file5; // Add this

nagabathula
23rd June 2011, 08:07
Hello Anna thankx for the reply . .. i have added the below code which you suggested, but it din work. There is still some extra data after some 5 updates into the notepad.


out8.flush();
file5->close();
delete file5;
wondering what is happening in the program it looks so simple and plain in the program what can be going wrong at all.

regrads

Santosh Reddy
23rd June 2011, 08:14
Couple of question to help me understand better...
Is void datadisplay::datatcplog() a slot ?
Is void datadisplay::datatcplog() is called every 30 sec?
Do you use file5, out side void datadisplay::datatcplog() function?

dbzhang800
23rd June 2011, 08:17
void datadisplay::datatcplog()
{
file5 = new QFile(newfilenames);
file5->open(QFile::ReadWrite | QFile::Text);


You should use QFile::WriteOnly instead of QFile::ReadWrite in your case.

nagabathula
23rd June 2011, 08:32
Yea datatcplog() is a slot. I am calling it with a timer every 30 seconds. I have not used file5 any where else in the program.
I have another routine of writing to a excel file i have named it file4 and there is no problem at all, it logs into excel with out any extra char's. Its only the notepad file which i have a problem. And all the containers which have the data for notepad file are different from the excel logging data containers so there should be nothing extra i am clearing all the containers at the end of the writing function.


connect(logtimer, SIGNAL(timeout()), this, SLOT(datatcplog()));

Added after 6 minutes:

hi dbzhang800 i have changed as you have suggested. In testing now. !! it seems to work there is nothing extra after 5 minutes of logging will check for longer duration. ..

Thank you