-3

I am editing the question because i have solved the problems to map and split the string as i wish. Now i have found how to find a particular string in the file. but couldn figure out how print all those lines that this specific string is located on. For example if string t1("Cap900") occurs in both 5th and 7th line i want to print the whole line beneath each other.

ifstream f("test.txt");

string t1("Cap900");

istreambuf_iterator<char> eof;
if(eof == search(istreambuf_iterator<char>(f), eof, t1.begin(), t1.end()) )
cout << "String \"" << t1 << "\" was NOT found in the file " << endl;
else
cout << "String \"" << t1 << "\" was found in the file " << endl;
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
user3521035
  • 23
  • 1
  • 9

1 Answers1

0

I don't know if I'm reading this correctly, but it looks like you are trying to sort all of the lines in your file. I have a suggestion if this is the case.

Are you familiar with the std::map data structure? If the data after Cap900 is what you want to match to, you could create a map that uses that string as a key, either by hashing that string to an integer value (ex: using a hash you can find here) or by using the full string as the hash though I think it might be slower. Something like this:

// ... other code up here ...
string original;
getline(inFile, original, '\0');

int hash = FuncThatHashesAString(original);

std::map<int, std::vector<string> > lineMap;

// indexing into the map at a location will either return a reference to a vector or create a new one that is empty
lineMap[hash].push_back(original);

// afterwards you can print out each line in order using a loop like this
auto it = lineMap.begin();
for(; it != lineMap.end(); ++it)
{
  for(unsigned i = 0; i < it->second.size(); ++i)
  {
    string strOriginal = it->second[i];
    // then print your string to a new file if you want
  }
}

Of course this is adding the full line though, you would need to parse your data first before using it as the hash. I think you would also want to add the full line to the map as well, not just the data to the right of Cap900. Let me know if this helps you!

Community
  • 1
  • 1
KiraBox
  • 69
  • 1
  • 10
  • Firstly i really appreciate orbit for the help. no i am not familiar with std::map. I think you are expecting me to write FuncThatHashesAString(original) i will try and see, but the time is limit, would appreciate if you help me out with this, i am sure i will do this by myself but the deadline will pass and there will be no use of it. – user3521035 May 26 '15 at 07:30
  • There are plenty of hashing functions out there. They can be very complicated and can involve a lot of math, so I highly recommend just finding a c++ hashing function and copying it into your program, not writing it yourself. There is a hashing function in the [link](http://stackoverflow.com/questions/8317508/hash-function-for-a-string) above you can use. You can search that page for 'unsigned hash_str(const char* s)' to find it. – KiraBox May 26 '15 at 14:18
  • The hash function will take your string and give you back a unique integer hash. If you used an array and wanted to index it by this number, you would need an array of a size that is at least that hash value plus one, and your array would have a lot of unused space! You can think of a std::map like a "sparse array". It only stores the data it needs, and doesn't waste a lot of space. So indexing a map by your hash will not require an index of at least hash, it will "map" your index to one that first into its current size (ex: 293840 might map to 6, or something like that). – KiraBox May 26 '15 at 14:28