-
February 7th, 2011, 06:25 AM
#1
reading txt file. How do I FAST read the data?
Hi everybody
I wrote a program that reads from txt. But it takes to much time to read the data. My txt file contains following information:
=====Traces=====
10:18:18.176 -> 52 1 0 -1 -690 1000 0 100 0 690 1000 0 100
10:18:18.285 -> 52 1 0 -1 -490 1000 0 100 0 6904 1000 0 100
10:18:18.394 -> 52 1 0 -1 -690 1000 0 100 0 690 1000 0 100
...
I have to convert this numbers to integer. my function does it like this:
after I read the position of whitespaces
leerzeichen is an array that contains a position of whitespaces of the line(zeile).
...
Code:
ostringstream sLength,sType,sTimestamp,sX1,sX2,sY1,sY2,sZ1,sZ2,sDy1,sDy2,sConf1,sConf2;
for(i=0;i<sizeof(zeile);i++){ // TAKES TO MUCH TIME!!!
if(i>leerzeichen[1] && i<leerzeichen[2])
sLength<<zeile[i];
else if(i>leerzeichen[2] && i<leerzeichen[3])
sType<<zeile[i];
else if(i>leerzeichen[3] && i<leerzeichen[4])
sTimestamp<<zeile[i];
else if(i>leerzeichen[4] && i<leerzeichen[5])
sX1<<zeile[i];
else if(i>leerzeichen[5] && i<leerzeichen[6])
sX2<<zeile[i];
else if(i>leerzeichen[6] && i<leerzeichen[7])
sY1<<zeile[i];
else if(i>leerzeichen[7] && i<leerzeichen[8])
sY2<<zeile[i];
else if(i>leerzeichen[8] && i<leerzeichen[9])
sZ1<<zeile[i];
else if(i>leerzeichen[9] && i<leerzeichen[10])
sZ2<<zeile[i];
else if(i>leerzeichen[10] && i<leerzeichen[11])
sDy1<<zeile[i];
else if(i>leerzeichen[11] && i<leerzeichen[12])
sDy2<<zeile[i];
else if(i>leerzeichen[12] && i<leerzeichen[13])
sConf1<<zeile[i];
else if(i>leerzeichen[13])
sConf2<<zeile[i];
}
string temp=sLength.str();
t_length=atoi(temp.c_str());
temp=sType.str();
t_type=atoi(temp.c_str());
temp=sTimestamp.str();
t_timestamp=atoi(temp.c_str());
temp=sX1.str();
t_x1=atoi(temp.c_str());
temp=sX2.str();
t_x2=atoi(temp.c_str());
temp=sY1.str();
t_y1=atoi(temp.c_str());
temp=sY2.str();
t_y2=atoi(temp.c_str());
temp=sZ1.str();
t_z1=atoi(temp.c_str());
temp=sZ2.str();
t_z2=atoi(temp.c_str());
temp=sDy1.str();
t_dy1=atoi(temp.c_str());
temp=sDy2.str();
t_dy2=atoi(temp.c_str());
temp=sConf1.str();
t_conf1=atoi(temp.c_str());
temp=sConf2.str();
t_conf2=atoi(temp.c_str());
the problem is that sstream, it takes along time. How can i fix that problem? Any ideas?
Thank you
Last edited by diman65; February 7th, 2011 at 07:10 AM.
-
February 7th, 2011, 06:31 AM
#2
Re: reading txt file. How do I FAST read the data?
Please:
- Use Code tags while posting code snippets.
- Don't declare variables as a comma separated list! One variable - one line.
- Use white spaces between operands.
Otherwise your code becomes absolutely unreadable!
As for more fast reading - just read the whole line and only then parse it.
Victor Nijegorodov
-
February 7th, 2011, 06:38 AM
#3
Re: reading txt file. How do I FAST read the data?
im sorry about that
Im new in this forum. Next time I'll do this right.
ps: can't find edit button on the thread :S
-
February 7th, 2011, 06:41 AM
#4
Re: reading txt file. How do I FAST read the data?
If all the liens have the same amount of values, you can read directly into
the variables without reading in the line and then using stringstream:
Code:
const int nValues = 13;
string date_time , arrow;
int values[nValues];
ifstream in("data.txt");
while (in >> date_time >> arrow)
{
for (int i=0; i<nValues; ++i)
{
in >> values[i];
}
}
Last edited by Philip Nicoletti; February 7th, 2011 at 06:43 AM.
-
February 7th, 2011, 07:02 AM
#5
Re: reading txt file. How do I FAST read the data?
Thank you for quick reply Philip,
I dont really understand the line "in >> date_time >> arrow"
I have also a compiler error
"error C2678: binary '>>' : no operator defined which takes a left-hand operand of type 'class ifstream' (or there is no acceptable conversion)"
-
February 7th, 2011, 07:21 AM
#6
Re: reading txt file. How do I FAST read the data?
Can you post the code that gives the error (with the declarations
of the types of variables).
-
February 7th, 2011, 08:15 AM
#7
Re: reading txt file. How do I FAST read the data?
I used the same code that you have posted.
-
February 7th, 2011, 08:19 AM
#8
Re: reading txt file. How do I FAST read the data?
Do you have all the proper includes ? Can you test this code stand-alone ?
Code:
#include <fstream>
#include <string>
using namespace std;
int main()
{
const int nValues = 13;
string date_time , arrow;
int values[nValues];
ifstream in("data.txt");
while (in >> date_time >> arrow)
{
for (int i=0; i<nValues; ++i)
{
in >> values[i];
}
}
return 0;
}
-
February 7th, 2011, 08:38 AM
#9
Re: reading txt file. How do I FAST read the data?
It works, if I test the code in separate project.
But inside a function of my class it doesen't work any more. But what is a sense of this line?
in >> date_time >> arrow
im still trying to understand that. But I dont get it
-
February 7th, 2011, 08:42 AM
#10
Re: reading txt file. How do I FAST read the data?
It is just standard C++ operator >> ...
Does your project include <string> and <fstream> ? (with .h extensions ?)
using namespace std; ??
-
February 7th, 2011, 08:51 AM
#11
Re: reading txt file. How do I FAST read the data?
-
February 7th, 2011, 09:14 AM
#12
Re: reading txt file. How do I FAST read the data?
yes its includes <fstream.h>
I dont know why but this was a problem. Thank you very much Philip.
I will try to use this in my project.
-
February 7th, 2011, 09:28 AM
#13
Re: reading txt file. How do I FAST read the data?
<fstream.h> is the old header from before the C++ standard in 1998.
It looks like your version of fstream.h does not have an overload to read into a std::string.
You could change the variables, date_time and arrow from std::string to
Code:
char date_time[20];
char arrow[20];
if changing to <fstream> is not feasible.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|