ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I'm having problems with my loops, and possibly more...hehe!
I'm loading a text file, which has 1 word per line, into a buffer and I want to search the buffer for all the words that start with the same prefix (3 letters) and return the words in an edit box.
I successfully accomplished reading 1 line, compare the prefix, and show the word in an edit box if prefix matches. Now, I'm just stuck with making it loop.
Exemple: The Prefix is: "Bla". The text file contains:
Benji
Bennett
Bentley
Beverly
Billy
Birch Grove
Birch Hill
Birch
Birchdale
Birchmount
Birchwood
Black Beach
Black
Blair
Bleury
Blue Heron
Blue Rock
@Hivemind
Why? It's a fully valid C++ problem that any C++ programmer should be able to advise on.
@EOTF
Unfortunately I don't do OOP, so can not really advise. A thing that looks suspicious (from a C perspective) is that you add read and buffer together. Not sure if that's possible in C++ as they are of different types (Cstring and char).
Further I suggest that you look at string functions for compare and substring etc (e.g. here.
Well, just because this happens to be a class member function doesn't make it an oop-problem. I noticed the OP is using UpdateData(), which should be avoided, and the experts who can point out and explain such things to him usually can be found there, not here. And if he's using UpdateData() there are likely to be all sorts of things he does in MFC which can be done in a better way. I rarely see MFC questions here. I don't see what your contribution was, I didn't answer his question but pointed him to a place where his question most likely wouldn't go unanswered for so long as it did here. Maybe you just don't like the fact that I pointed the OP to another place?
The question really had nothing to do with MFC, as was pointed out, but I still think it's a good idea if the OP visits a really good MFC forum, like the ones I suggested earlier. A standard c++ solution to the problem I provide here, I wrote it from scratch, not bothering with the OPs attempt.
Code:
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
bool matches_prefix(const string& word, const string& prefix);
int
main()
{
ifstream infile("words.txt");
if (!infile)
{
cerr << "Error opening file." << endl;
return 1;
}
const string prefix = "Bla";
string current_word;
while (getline(infile, current_word))
{
if (matches_prefix(current_word, prefix))
{
cout << current_word << endl;
}
}
}
bool
matches_prefix(const string& word, const string& prefix)
{
if (word.length() < prefix.length())
{
return false;
}
string s = word.substr(0, prefix.length());
return s == prefix;
}
Tried with the example file showed by the OP and got the expected output.
First of, Hivemind, thanks for the code pal, it's working great! It's not MFC, but I found a way around it by exporting the result into a text file, than read the text file from the MFC app. I'll use that until I figure out how to do it properly in an MFC app.
Second, I'm sorry for posting in the wrong group...it is my first post in this Forum!
And finally, I'm using MFC because I love it and this is how I want the result to display; in an Edit Box, not in a DOS window. This is just a small part of a bigger program (MFC) and the prefix part was the only part that's been giving me a bit of a hard time.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.