Searching text files

Discussion in 'C++' started by hivie, Jul 8, 2003.

  1. hivie

    hivie Guest

    I have a problem that is causing me problems. I have a text file that
    stores 5 lines of crap (stuff that I dont need( for the user only)).
    After that there is data that is in three columns separated by lots of
    whitespace. I dont think that the bytes are uniform ( I tried
    TextReader.readblock). Sorry, I am using VC++ .net. I only need the
    first two columns that are both double values. I need to either read
    those two columns into a mulit-Dim array or be able to search the
    file expeditously at runtime. The searchable value is the value in
    the first column. Can someone give some direction please???
    Thank you

    hivie
     
    hivie, Jul 8, 2003
    #1
    1. Advertising

  2. "hivie" <> wrote in message
    news:...
    > I have a problem that is causing me problems. I have a text file that
    > stores 5 lines of crap (stuff that I dont need( for the user only)).
    > After that there is data that is in three columns separated by lots of
    > whitespace. I dont think that the bytes are uniform ( I tried
    > TextReader.readblock). Sorry, I am using VC++ .net. I only need the
    > first two columns that are both double values. I need to either read
    > those two columns into a mulit-Dim array or be able to search the
    > file expeditously at runtime. The searchable value is the value in
    > the first column. Can someone give some direction please???
    > Thank you
    >
    > hivie


    What is TextReader.readblock? You'll only get a answer in standard C++ here.

    What's in the third column? This is going to have to be read, even though
    you don't need it.

    Your searching requirements would be best dealt with using a std::map

    #include <map>

    std::map<double, double> my_map;

    Much, much more efficient than an array. If you need to hold duplicate
    values for the first column, then use a multi_map instead of a map.

    Something like this? (untested)

    #include <fstream>
    #include <string>
    #include <map>

    ifstream my_file("myfile.txt");
    std::map<double, double> my_map;
    string dummy;
    // skip file lines
    for (int i = 0; i < 5; ++i)
    getline(my_file, dummy);
    double col1, col2;
    while (my_file >> col1 >> col2)
    {
    my_map[col1] = col2;
    if (!getline(my_file, dummy)) // skip rest of line
    break;
    }

    You should investigate map::lower_bound, map::upper_bound and
    map::equal_range for searching the map. Searching a map, whose key is a
    double, for exact values is unlikely to work.

    john
     
    John Harrison, Jul 8, 2003
    #2
    1. Advertising

  3. hivie

    hivie Guest

    "John Harrison" <> wrote in message news:<bedlv0$3rup5$>...
    > "hivie" <> wrote in message
    > news:...
    > > I have a problem that is causing me problems. I have a text file that
    > > stores 5 lines of crap (stuff that I dont need( for the user only)).
    > > After that there is data that is in three columns separated by lots of
    > > whitespace. I dont think that the bytes are uniform ( I tried
    > > TextReader.readblock). Sorry, I am using VC++ .net. I only need the
    > > first two columns that are both double values. I need to either read
    > > those two columns into a mulit-Dim array or be able to search the
    > > file expeditously at runtime. The searchable value is the value in
    > > the first column. Can someone give some direction please???
    > > Thank you
    > >
    > > hivie

    >
    > What is TextReader.readblock? You'll only get a answer in standard C++ here.
    >
    > What's in the third column? This is going to have to be read, even though
    > you don't need it.
    >
    > Your searching requirements would be best dealt with using a std::map
    >
    > #include <map>
    >
    > std::map<double, double> my_map;
    >
    > Much, much more efficient than an array. If you need to hold duplicate
    > values for the first column, then use a multi_map instead of a map.
    >
    > Something like this? (untested)
    >
    > #include <fstream>
    > #include <string>
    > #include <map>
    >
    > ifstream my_file("myfile.txt");
    > std::map<double, double> my_map;
    > string dummy;
    > // skip file lines
    > for (int i = 0; i < 5; ++i)
    > getline(my_file, dummy);
    > double col1, col2;
    > while (my_file >> col1 >> col2)
    > {
    > my_map[col1] = col2;
    > if (!getline(my_file, dummy)) // skip rest of line
    > break;
    > }
    >
    > You should investigate map::lower_bound, map::upper_bound and
    > map::equal_range for searching the map. Searching a map, whose key is a
    > double, for exact values is unlikely to work.
    >
    > john


    Sorry, the textreader is a .net thing. I have never heard of "map",
    but I am going to look into it. thanks for the direction!
    Heath
     
    hivie, Jul 8, 2003
    #3
  4. >
    > Sorry, the textreader is a .net thing. I have never heard of "map",
    > but I am going to look into it. thanks for the direction!
    > Heath


    std::map is a standard C++ class for an associative array.

    john
     
    John Harrison, Jul 8, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. crazyprakash
    Replies:
    4
    Views:
    3,420
    adrian
    Oct 30, 2005
  2. Rene Aguirre

    Searching text in Excel, Word files

    Rene Aguirre, Mar 3, 2004, in forum: Python
    Replies:
    0
    Views:
    426
    Rene Aguirre
    Mar 3, 2004
  3. Replies:
    4
    Views:
    984
    M.E.Farmer
    Feb 13, 2005
  4. Paul Donaghy
    Replies:
    2
    Views:
    139
    7stud --
    Apr 26, 2008
  5. stumblng.tumblr
    Replies:
    1
    Views:
    222
    stumblng.tumblr
    Feb 4, 2008
Loading...

Share This Page