PDA

View Full Version : Reading a text file



anh5kor
5th March 2015, 05:30
Hello

In my Project I am creating a LogReport.txt. Which maintain a Report like below
ECU->USS
Sensor Time Data
USS1 17:45:51 0x0,0xff,0x0,0x0
USS2 17:45:52 0x0,0x0,0xff,0x0
USS3 17:45:53 0x0,0x0,0x0,0x0
USS4 17:45:54 0x0,0xff,0x0,0x0
USS->ECU
Sensor Time Data
USS1 17:45:51 0x0,0x0,0x0,0x0
USS2 17:45:52 0x0,0xff,0x0,0x0
USS3 17:45:53 0x0,0xff,0x0,0x0
USS4 17:45:54 0x0,0x0,0xff,0x0
------
------
And so on until the code run.
Now once I stop my code I have to read back same data on button click.
Suppose on first click I have to read 17:45:51 0x0 0xff 0x0 0x0 and second click 17:45:52 0x0 0x0 0xff 0x0 and so on.
Can any one let me know how can I read each line with five different column.

Writefile


file->setFileName("LogReport.txt");
file->open(QIODevice::WriteOnly | QIODevice::Text);
QTextStream stream(file);
int index = 0;

stream << "ECU->USS\n";
stream <<"Sensor"<<" "<<"Time"<<" "<<"Data"<<endl;
for(int i = 0;i<12;i++)
{
stream << "USS"<<i+1<<" ";

stream<<time_log_ecu2uss[i];
if(i<9)
{
stream<<" ";
}
else
{
stream<<" ";
}

for(int j = 0;j<4;j++)
{

stream<<("0x"+QString::number(Data_ecu2uss[index],16));
if(j<3)
{
stream<<",";
}
index++;
}

stream<<endl;
}

index = 0 ;
stream << "USS->ECU\n";
stream <<"Sensor"<<" "<<"Time"<<" "<<"Data"<<endl;
for(int i = 0;i<12;i++)
{
stream << "USS"<<i+1<<" ";
stream<<time_log_uss2ecu[i];
if(i<9)
{
stream<<" ";
}
else
{
stream<<" ";
}
for(int j = 0;j<4;j++)
{
stream<<("0x"+QString::number(Data_uss2ecu[index],16));
if(j<3)
{
stream<<",";
}
index++;
}
stream<<endl;
}

stream<<"************************************************** ****************"<<endl;

}

Lesiok
5th March 2015, 07:17
Use QTextStream::readLine

anh5kor
5th March 2015, 08:21
For readline it will read whole Line like


QString line = in.readLine();
qDebug()<<"line"<<line;

o/p:- "USS1 12:17:52 0x0 0x0 0x0 0x0"
Now how can I extract each text and store in array.
it is better if I read 12:17:52 ,0x0,0x0,0x0,0x0 separately every time.

wysota
5th March 2015, 08:26
So what is stopping you from doing that?

anh5kor
5th March 2015, 09:56
So I have button in my gui when it click I should read First line.
Again if I click I should read second line and so on but
when I click first time I am getting the first line
but when clicking on second time I am getting nothing.



QTextStream in(file);
QString line = in.readLine();
qDebug()<<"line"<<line

o/p:-USS1 15:14:33 0x0 0x0 0x0 0x5d (on first click)
" "(on second click)

It seem it is not going to second line

wysota
5th March 2015, 10:15
Show some concrete code please.

anh5kor
6th March 2015, 06:11
void ui_logging::Button_clicked()
{

QTextStream in(file);
QTextStream stream(file);
QString str1;


for(int i = 0;i<6;i++)
{

in>>str1;
qDebug()<<str1;

}

}

O/p:First Click->USS1 15:14:33 0x0 0x0 0x0 0x5d
2nd Click->"" "" "" "" "" ""
3rd click->"" "" "" "" "" ""
--------so on
So on every click I have to read 6 string and on another click read the next 6 string but I am only getting the data on First click only and from
next click getting Null

wysota
6th March 2015, 07:51
What is file? Where do you open it? Why are you creating two text streams on it? Why do you do that every time the slot is called?

anh5kor
6th March 2015, 08:38
ui_logging::ui_logging()//constructor
{
QFile *file = new QFile();
}
openwrite_logfile() //call at start button
{
file->setFileName("LogReport.txt");
file->open(QIODevice::WriteOnly | QIODevice::Text);
}
openread_logfile()//call at stop button
{
file->open(QIODevice::ReadOnly | QIODevice::Text);
}

void ui_logging::Button_Clicked //display data on read button clicked
{

QTextStream in(file);
for(int i = 0;i<10;i++)
{

in>>str1;
qDebug()<<str1;

}

}


I don't have any idea to declare QTextStream in(file) as globally

wysota
6th March 2015, 09:56
You are aware 'file' variable from line #3 and the one from line #18 are different variables, right?

One warning from me -- when asked for code if you ever again post pseudo-code then I will either ignore your post or give you a pseudo-answer comparable to your pseudo-code.

anh5kor
6th March 2015, 10:12
I think till now you understand what exactly I am asking for.
I have recognize the Problem and my problem is in below code


void ui_logging::Button_Clicked()
{

QTextStream in(file); //My Problem is here on every click I am creating the object again and again

for( i = 0;i<10;i++)
{

in>>str1;
qDebug()<<str1;

}
int n = in.pos();


}

Do you have any solution for the above.

wysota
6th March 2015, 10:14
The file is probably a class member variable. Make the stream a class member variable as well.