Click to See Complete Forum and Search --> : Console vs. API
Ray Schmidt
March 25th, 2004, 08:09 PM
I'm having trouble getting specific things to work in a dialog driven application that work fine in a console application. Can someone offer me some direction on how to read in a simple asci file and store to variables. Below is what I got help on working so far (console - works GREAT!!!)
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
string line;
vector<string> vLines;
char fname[]="t21a.tre";
fstream in(fname, ios::in);
if(!in) {
cout << "Cannot open an input file named: " << fname;
}
while (getline(in,line))
vLines.push_back(line);
// output the lines read in
for (int i=0; i<vLines.size(); ++i) {
cout << vLines[i] << endl;
}
cout <<vLines[237];
cout << endl;
return 0;
}
ferox
March 30th, 2004, 02:58 AM
Are you using MFC in your GUI Windows application?
Paul McKenzie
March 30th, 2004, 05:55 AM
Originally posted by Ray Schmidt
I'm having trouble getting specific things to work in a dialog driven application that work fine in a console application. Can someone offer me some direction on how to read in a simple asci file and store to variables. Below is what I got help on working so far (console - works GREAT!!!)
Are you saying that if you copied and pasted your main function and called it foo() in your dialog app, you don't get the same behavior if you call foo()??
What exactly "doesn't work"? It doesn't help us if you don't tell us what is not working. A compiler error? A runtime error? If it's a compiler error, state what the error is. If it's a runtime error, then how do you know that it isn't the other things you're doing in your app that is causing the problem?
Regards.
Paul McKenzie
Ray Schmidt
March 30th, 2004, 08:57 PM
Not sure what "foo()" is but when I add:
// The header calls are included at the top of my code for vector and string.
string line;
vector<string> vLines;
that I get the message:
'string' : undeclared identifier
syntax error : missing ';' before identifier 'line'
'line' : undeclared identifier
'vector' : undeclared identifier
'vLines' : undeclared identifier
Ray Schmidt
March 30th, 2004, 09:06 PM
Are you using MFC in your GUI Windows application?
:confused: Yes, I have used my MFC AppWizard(exe) to generate most of my code. Could using this be my problem??? :confused:
RussG1
March 30th, 2004, 09:20 PM
Did you include:
using namespace std;
In your GUI version? (and add the includes for string and vector?)
RussG1
March 30th, 2004, 09:30 PM
You do not necessarily need to add the line:
using namespace std;
But you do need to specify the namespace for string and vector in one way or another.
One way is with:
using namespace std;
another way is to prepend std:: to the types, ie.
std::string line;
std::vector<std::string> vLines;
Paul McKenzie
March 30th, 2004, 10:04 PM
Originally posted by Ray Schmidt
Not sure what "foo()" is but when I add:
It's the name of a dummy function that contains the same code you had in main().
[quote]
// The header calls are included at the top of my code for vector and string.
string line;
vector<string> vLines;
vector and string are in the std namespace. If you take a look at the console app again, you'll see
using namespace std;
In your GUI app, you did not specify the namespace where the vector and string classes are defined. Namespaces are discussed in many threads here, and I believe it's in the CodeGuru FAQ and should be in your favorite C++ book.
Regards,
Paul McKenzie
Ray Schmidt
March 30th, 2004, 10:11 PM
That cleared those errors up!!! What do I do with the conversion from:
cout << vLines[i] << endl;
to a variable that I can use on the list box (m_showit) ???
RussG1
March 30th, 2004, 10:21 PM
See the Codeguru faq for info on converting between CString and std::string.
Strings: How to convert between CString and std::string? (http://www.codeguru.com/forum/showthread.php?s=&threadid=231155)
Ray Schmidt
March 30th, 2004, 11:01 PM
I think I've got it right but I keep getting unexpected results...
for (i=0; i<vLines.size(); ++i) {
string buffer(vLines[i]);
CString cs(buffer.c_str());
}
and the result in the box is:
ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌ
I am going from CString to std::string: right???
RussG1
March 31st, 2004, 02:39 AM
Can you show the rest of the code? i.e. Where you are adding the text to the listbox, etc.
Also, do you mean to use ++i, rather than i++ in your loop?
(index is zero based)
If m_showit is a CString, you should be able to do something like this:
m_showit = vLines[i].cstr();
Or pass the std::string itself to the CListBox::InsertString function.
ie.
// assuming m_Lines is CListBox control member variable
for (int i = 0; i<vLines.size(); i++)
m_List.InsertString(i, vLines[i].c_str());
Sam Hobbs
March 31st, 2004, 08:34 PM
Originally posted by RussG1
Or pass the std::string itself to the CListBox::InsertString function.Finally some sanity in this ....
Since the program is a MFC program, this question would get better answers in the VC forum.
The std::string class is very useful and can be used in most situations instead of the MFC CString class. However when writing MFC programs, it helps to know how to use the CString class. In the VC forum, the answers are more likely to use the CString class.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.