Searching text files

H

hivie

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
 
J

John Harrison

hivie said:
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
 
H

hivie

John Harrison said:
hivie said:
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
 
J

John Harrison

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top