Help with Loop (C++/MFC)

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2007
Posts: 1
Reputation: EOTF is an unknown quantity at this point 
Solved Threads: 0
EOTF EOTF is offline Offline
Newbie Poster

Help with Loop (C++/MFC)

 
0
  #1
Oct 16th, 2007
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:


  1. #include <iostream>
  2. #include <fstream>
  3. #include <strstream>
  4.  
  5. //....
  6.  
  7. void Csn3Dlg::OnBnClickedButton1()
  8. {
  9. UpdateData(TRUE);
  10.  
  11. FILE * pFile;
  12. char buffer [20];
  13. CString prefix = "Old";
  14. CString readprefix;
  15. CString temp;
  16. CString read;
  17. int count=0;
  18.  
  19. pFile = fopen ("d:\\namelist.txt" , "r");
  20.  
  21. if (pFile == NULL)
  22. {
  23. MessageBox("Error opening file", NULL, MB_OK | MB_ICONSTOP);
  24. exit(0);
  25. }
  26.  
  27. else
  28. {
  29. while ( ! feof (pFile) )
  30. {
  31. fgets (buffer , 20 , pFile);
  32. fputs (buffer , stdout);
  33. read = read+buffer+"\r\n";
  34. }
  35. fclose (pFile);
  36.  
  37. //My problems are in there:
  38. while ( prefix != readprefix )
  39. {
  40. for (int j = count ; read[j]!='\n'; j++)
  41. temp = temp+read[j];
  42. for(int i=0 ; i!=3 ; i++)
  43. readprefix = readprefix+temp[i];
  44.  
  45. if(prefix == readprefix)
  46. break;
  47. else
  48. count++;
  49. }
  50.  
  51. m_out = temp;
  52.  
  53. }
  54.  
  55. UpdateData(FALSE);
  56. }

Any suggestions or help will be much appreciated!
Last edited by Ancient Dragon; Oct 16th, 2007 at 8:37 pm. Reason: replace quote tags with code tags
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,578
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1486
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Help with Loop (C++/MFC)

 
0
  #2
Oct 16th, 2007
line 29 is wrong
  1. while( fgets (buffer , 20 , pFile) )
  2. {
  3. // blabla
  4. }

fgets() normally adds the '\n' that is in the file at the end of the input string that you need to strip off. And it would make your life a lot easier if you would put the strings in an array instead of concantinating them into one big string.
  1. while( fgets (buffer , sizeof(buffer) , pFile) )
  2. {
  3. if( buffer[strlen(buffer)-1] == '\n')
  4. buffer[strlen(buffer)-1] = 0;
  5. // blabla
  6. }
Last edited by Ancient Dragon; Oct 16th, 2007 at 8:46 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC