CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    Feb 2011
    Posts
    6

    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.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    Re: reading txt file. How do I FAST read the data?

    Please:
    1. Use Code tags while posting code snippets.
    2. Don't declare variables as a comma separated list! One variable - one line.
    3. 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

  3. #3
    Join Date
    Feb 2011
    Posts
    6

    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

  4. #4
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    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.

  5. #5
    Join Date
    Feb 2011
    Posts
    6

    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)"

  6. #6
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    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).

  7. #7
    Join Date
    Feb 2011
    Posts
    6

    Re: reading txt file. How do I FAST read the data?

    I used the same code that you have posted.

  8. #8
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

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

  9. #9
    Join Date
    Feb 2011
    Posts
    6

    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

  10. #10
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    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; ??

  11. #11
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: reading txt file. How do I FAST read the data?

    See http://www.codeguru.com/forum/showthread.php?t=507380
    for a similar issue and solution hints

  12. #12
    Join Date
    Feb 2011
    Posts
    6

    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.

  13. #13
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    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
  •  





Click Here to Expand Forum to Full Width

Featured