extracting data from and input file


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: extracting data from and input file

  1. #1
    Join Date
    Feb 2006
    Posts
    1

    Unhappy extracting data from and input file

    I'm a newbie to both this forum and to C++. I need to extract various bits of information from an input file. I have no problem setting up to read from the file but I dont fully understand how to get the information I want from it.

    It is a simple text file:
    -------------------------------------------------------
    Feb13-14,2006

    Kraushaar,Silke 25 Germany 1:02.517 1:06.339 1:10.249
    Huefner,Tatjana 54 Gemany 1:03.969 1:02.134 1:02.19 1:07.660 1:05.372
    Zablocki,Courtney 37 UnitedStates 0:59.828 1:00.826 01:11.001 0:54.742
    Ludan,Liliya 20 Ukraine 0:62.008 01:09.781 1:07.484 0:58.22


    Retrosi,Samantha 42 UnitedStates 01:10.382 0:58.22 1:03.793 1:12.397
    Lauscher,Regan 59 Canada



    ##
    ---------------------------------------------------------------
    I was told I could use strtok_s() to pull pieces from the file. Now I dont fully understand how to use this function. I do understand what tokens and delimators are but I'm not sure how to read say only the times "1:02.517 1:06.339 1:10.249" I can see that they are seperated by spaces but how do i get far enough into the file to read these without picking up all the other information?

    I realize this is probably a rudimentary question but I'm not a hardcore programmer by any means and it has been quite a while since I programmed in C. If you could just give me some hints to point me in the right direction I would be very grateful.

    Thanks

  2. #2
    Join Date
    Jan 2005
    Posts
    42
    Hi,

    I am assuming that you are programming in C++. I wrote a small uitlity function to separate strings given a specific delimiter.

    //************************************************************************
    // simple utility function to split up stings and store them in a vector
    //***********************************************************************

    void split(vector< string > &res, string &s, string &seperator)
    {
    size_t prepos, pos;
    prepos=pos=0;
    res.clear();



    while( (pos = s.find_first_of(seperator, pos)) != std::string::npos)
    {

    res.push_back(s.substr(prepos, pos - prepos));

    pos++;
    prepos= pos;
    }

    res.push_back(s.substr(prepos, s.size() - prepos));

    } // split()



    You can have a loop to read from the file and then send the line to this function

    split( svect, readLine, " " );

    the 4,5 and 6 element of the vector are the tokens you want. This thing will work assuming the pattern of the lines in the text file are all the same. Also make sure while getting the tokens that the size of the vector is 6 or else there will be an error.

    There are possibly simpler ways to do it, but I think this would work

    Cheers

    Tapajyoti

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center