LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-16-2007, 06:04 PM   #1
EOTF
LQ Newbie
 
Registered: Oct 2007
Posts: 2

Rep: Reputation: 0
Help with Loop (C++/MFC)


Hi folks,

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

The program will return:
Black Beach
Black
Blair


Here's the code for the button:


Quote:
#include <iostream>
#include <fstream>
#include <strstream>

//....

void Csn3Dlg::OnBnClickedButton1()
{
UpdateData(TRUE);

FILE * pFile;
char buffer [20];
CString prefix = "Old";
CString readprefix;
CString temp;
CString read;
int count=0;

pFile = fopen ("d:\\namelist.txt" , "r");

if (pFile == NULL)
{
MessageBox("Error opening file", NULL, MB_OK | MB_ICONSTOP);
exit(0);
}

else
{
while ( ! feof (pFile) )
{
fgets (buffer , 20 , pFile);
fputs (buffer , stdout);
read = read+buffer+"\r\n";
}
fclose (pFile);

//My problems are in there:
while ( prefix != readprefix )
{
for (int j = count ; read[j]!='\n'; j++)
temp = temp+read[j];
for(int i=0 ; i!=3 ; i++)
readprefix = readprefix+temp[i];

if(prefix == readprefix)
break;
else
count++;
}

m_out = temp;

}

UpdateData(FALSE);
}
Any suggestions or help will be much appreciated!
 
Old 10-17-2007, 09:34 AM   #2
Hivemind
Member
 
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
It would probably be a better idea to ask that question here instead. Or, if you prefer Microsofts web interface go here (it's the same group).
 
Old 10-17-2007, 10:34 AM   #3
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Rep: Reputation: 282Reputation: 282Reputation: 282
@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.
 
Old 10-17-2007, 10:47 AM   #4
Hivemind
Member
 
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
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?
 
Old 10-17-2007, 11:14 AM   #5
Hivemind
Member
 
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
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.
 
Old 10-18-2007, 01:11 PM   #6
EOTF
LQ Newbie
 
Registered: Oct 2007
Posts: 2

Original Poster
Rep: Reputation: 0
Hi again,

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.

Thanks again folks!

Last edited by EOTF; 10-18-2007 at 01:55 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to loop over text file lines within bash script for loop? johnpaulodonnell Linux - Newbie 9 07-28-2015 03:49 PM
bash loop within a loop for mysql ops br8kwall Programming 10 04-30-2008 03:50 AM
converting shell while loop to for loop farkus888 Programming 8 09-12-2007 02:30 AM
for loop only works properly on first loop symo0009 Programming 1 12-25-2005 05:17 PM
MFC and GTKMM janiv Linux - Software 0 09-11-2003 05:57 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:27 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration