| | |
Help with Loop (C++/MFC)
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2007
Posts: 1
Reputation:
Solved Threads: 0
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:
Any suggestions or help will be much appreciated!
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:
C++ Syntax (Toggle Plain Text)
#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!
Last edited by Ancient Dragon; Oct 16th, 2007 at 8:37 pm. Reason: replace quote tags with code tags
line 29 is wrong
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.
C++ Syntax (Toggle Plain Text)
while( fgets (buffer , 20 , pFile) ) { // blabla }
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.
C++ Syntax (Toggle Plain Text)
while( fgets (buffer , sizeof(buffer) , pFile) ) { if( buffer[strlen(buffer)-1] == '\n') buffer[strlen(buffer)-1] = 0; // blabla }
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.
![]() |
Similar Threads
- vc++ mfc-i can't make getline work with a string for file input (C++)
- Help with gui loop. (C)
- Loop...without the loop (Java)
Other Threads in the C++ Forum
- Previous Thread: Adding things to Array-Based Lists
- Next Thread: how to retrieve part of the string
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamic dynamiccharacterarray email encryption error file format forms fstream function functions game givemetehcodez graph gui homeworkhelp iamthwee ifstream input int java lib library linker list loop looping loops map math matrix memory microsoft newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings struct temperature template templates text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






