|
I'm still learning this C++ stream stuff, so I don't think we are on the same page here, Zac. For an input file having a format of:
bob 1 2 3 4 5
mary 2 3 4 5 6 7
nathan 9 8 7 6
The first line of the file is read correctly by the operator>> method. The name is stored in the name variable, and the grades are stored in the grades vector. Those then get copied successfully to the Student object. The problem is that the students vector in main() does not get any Student objects added to it.
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
Here is a working example with the data format you gave:
#include <string>
#include <vector>
#include <algorithm>
#include <iterator>
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
class Student
{
public:
Student() : _Name("") {}
~Student() {}
void setName(const string& name) { _Name = name; }
string getName() const { return _Name; }
void setGrades(const vector<int>& grades) { _Grades.assign(grades.begin(), grades.end()); }
vector<int> getGrades() const { return _Grades; }
private:
string _Name;
vector<int> _Grades;
};
std::ostream& operator<<(std::ostream& os, const Student& s)
{
os << s.getName() << " ";
const vector<int> grades = s.getGrades();
copy(grades.begin(), grades.end(), ostream_iterator<int>(os, " "));
os << std::endl;
return os;
}
std::istream& operator>>(std::istream& is, Student& s)
{
string name = "";
is >> name;
string sGrades = "";
getline(is, sGrades);
vector<int> grades;
stringstream ss(sGrades);
copy(istream_iterator<int>(ss), istream_iterator<int>(), back_inserter(grades));
s.setName(name);
s.setGrades(grades);
return is;
}
int main()
{
ifstream fin;
vector<Student> students;
fin.open("data.txt");
copy(istream_iterator<Student>(fin), istream_iterator<Student>(), back_inserter(students));
fin.close();
std::copy(students.begin(), students.end(), ostream_iterator<Student>(cout, "\n\n"));
}
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
Thanks, Zac. So by having copy() read from a stringstream rather than a istream , how did that make a difference? The grades vector had the correct number of items added to it either way.
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
Putting the grades in a stringstream prevents the istream from trying to read the rest of the file. When the istream gets to the end of the file (that is, when the >> returns an eof()) it doesn't add anything to the vector (which is what you were seeing).
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
That helps. Thanks.
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
I came accross this little issue with the a CPrintDialog dialog.
Using the debug build of my code when I display the print setup dialig with the DoModal() method, I get a dialog with the attached printers displayed in a ListView. But in my release build of my code I get a dialog showing the attached printers in a combo-box! Also, in the debug version of the code selecting one of the printers to be the default printer does actually set the selected printer as the default printer but doing the same using the release build the default selection isn't retained by the OS! Both the release and debug builds are using the exact same code and there are no debug defines!
I would appreciate anyones advice on why this is happening, and any way to overcome the problem with the release build not being able to set the default printer, whereas the correct functionality is obtained in the debug build.
|
|
|
|
|
Once again this blasted IDE has brought this neophyte to his knees. My question is: how do you set the system path in Visual Studio 2003. I have been toiling away for the better part of 3 hours to try and get it to see the DLL that's on my hard drive, but it can't see it. Truthfully, I feel like crying.
Alas, any help you can give me would be greatly appreciated - as always.
Peace,
BP
|
|
|
|
|
Here's the search order for DLLs, from the platform SDK
The directory from which the application loaded.
The current directory.
The system directory. Use the GetSystemDirectory function to get the path of this directory.
The 16-bit system directory. There is no function that obtains the path of this directory, but it is searched.
Windows Me/98/95: This directory does not exist.
The Windows directory. Use the GetWindowsDirectory function to get the path of this directory.
The directories that are listed in the PATH environment variable.
|
|
|
|
|
How do I tell it which directory to look in? How do I add another directory to the PATH environment variable? I can't find where to set it.
Thanks,
BP
|
|
|
|
|
How To Manage Environment Variables in Windows XP[^]
From your code, if the dll is being implicitly linked (you are not calling LoadLibrary()) then
you'll need to keep the dll in the current search path.
If using LoadLibrary you can specify the path.
To set the PATH environment variable for the current process use SetEnvironmentVariable() API.
To change for all processes it's in the registry, I think at
HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment (Path variable is
semi-colon delimited series of paths).
|
|
|
|
|
Wow! Thanks, it worked! You don't know how much you have helped me.
Thanks again,
BP
|
|
|
|
|
I am using MS Visual Studio 2005 and i have a problem with message events.
***TestApp.h***
class CTestApp : public CWinApp<br />
{<br />
public:<br />
CTestApp();<br />
virtual ~CTestApp();<br />
virtual BOOL InitInstance();<br />
afx_msg LRESULT OnMsg(WPARAM wParam, LPARAM lParam);<br />
DECLARE_MESSAGE_MAP()<br />
};
***TestApp.cpp***
#include "stdafx.h"<br />
#include "TestApp.h"<br />
<br />
#define WM_MY_EVENT (WM_APP + 100)<br />
<br />
BEGIN_MESSAGE_MAP(CTestApp, CWinApp)<br />
ON_MESSAGE(WM_MY_EVENT, OnMsg)<br />
END_MESSAGE_MAP()<br />
<br />
void FileWrite(char *data)<br />
{<br />
FILE *f;<br />
errno_t err;<br />
err = fopen_s(&f, "C:\\debug.txt", "a");<br />
fprintf(f, data);<br />
fflush(f);<br />
fclose(f);<br />
}<br />
<br />
CTestApp::CTestApp()<br />
{ <br />
FileWrite("Constructed\n");<br />
}<br />
CTestApp::~CTestApp()<br />
{<br />
FileWrite("Destructed\n");<br />
}<br />
<br />
CTestApp theApp;<br />
<br />
BOOL CTestApp::InitInstance()<br />
{<br />
FileWrite("Initialized\n");<br />
return false;<br />
}
This is an error message: error C2440: 'static_cast' : cannot convert from 'LRESULT (__thiscall CTestApp::* )(WPARAM,LPARAM)' to 'LRESULT (__thiscall CWnd::* )(WPARAM,LPARAM)'
how can i fix that?
Thank you in advance
|
|
|
|
|
Try ON_THREAD_MESSAGE in the app class.
Use ON_MESSAGE in a window class.
Mark
|
|
|
|
|
then it gives new error message
error C2440: 'static_cast' : cannot convert from 'LRESULT (__thiscall CTestApp::* )(WPARAM,LPARAM)' to 'void (__thiscall CWinThread::* )(WPARAM,LPARAM)'
|
|
|
|
|
Sorry about that...your function should return void, not LRESULT
|
|
|
|
|
dohh.. it is alive thank you
|
|
|
|
|
edvintas wrote: afx_msg LRESULT OnMsg(WPARAM wParam, LPARAM lParam);
Are you certain you definition of OnMsg match the declaration ?
|
|
|
|
|
now it matches
so finally changes are:
in *.cpp added
void CTestApp::OnMsg(WPARAM wParam, LPARAM lParam)<br />
{<br />
}
and in *.h
LRESULT to void and ON_MESSAGE to ON_THREAD_MESSAGE
hurahhh...
|
|
|
|
|
There is a good article here at CP covering messages. Message Management
By Joseph M. Newcomer. [^] Definaltly worth a read.
I'd love to help, but unfortunatley I have prior commitments monitoring the length of my grass. :Andrew Bleakley:
|
|
|
|
|
I am trying to get the CFtpFileFind class to work. If I test against my company's ftp site, I can connect and navigate(via SetCurrentDirectoy) to the desired directory, but my call to FindFile(NULL) times out, always. Just to see what files were on there, I connected with an ftp client, and it worked fine. There is only a few files on there( < 10 ) so it isn't a timeout due to too many files.
If I change it to connect to the microsoft ftp site(ftp.microsoft.com), I have no problems, FindFile(NULL) succeeds and everything seems to work fine. Is there something that needs to be configured on the ftp server to allow this call to succeed? Any ideas yould be greatly appreciated.
|
|
|
|
|
In my application I need to handle an event on my CListCtrl icon View. Whenever the user clicks the mouse and drags it to select more than one item, the onMouse Move is not called. Really weird actually, but I need an event to handle this movement.
|
|
|
|
|
skullfire wrote: the onMouse Move is not called.
It works on my machine.
led mike
|
|
|
|
|
Are you handling the WM_MOUSEMOVE event in the correct class?
|
|
|
|
|
|
Greetings:
I need to encrypt some data in a file that is quite small, in most cases, less than 300 bytes. I just need something simple: Provide the function with a key and a block of data to encrypt and it returns with the block of data encrypted. A similar function to decrypt is needed, of course.
Can anybody suggest something? I would prefer it be a C++ thing.
Thanks in advance,
Mark
|
|
|
|