By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,262 Members | 1,161 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,262 IT Pros & Developers. It's quick & easy.

Searching text files

P: n/a
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
Jul 19 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a

"hivie" <hi*******@yahoo.com> wrote in message
news:49**************************@posting.google.c om...
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
Jul 19 '05 #2

P: n/a
"John Harrison" <jo*************@hotmail.com> wrote in message news:<be************@ID-196037.news.dfncis.de>...
"hivie" <hi*******@yahoo.com> wrote in message
news:49**************************@posting.google.c om...
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
Jul 19 '05 #3

P: n/a
>
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
Jul 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.