|
thanks, I forgot to remove the second " character from the command line.
|
|
|
|
|
davidbr4 wrote: thanks, I forgot to remove the second " character from the command line.
CopyFile will work too!
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers,
Alok Gupta
VC Forum Q&A :- I/ IV
Support CRY- Child Relief and You
|
|
|
|
|
that's what I used, thanks
|
|
|
|
|
Fist Part
I am trying to work on a simple database project which needs to determine if a record is null. I am wanting to do this by using the function isNull() in the classes Personal and Student to determine that a record is null. Then overwrite the record to be deleted by a null record. A null record can be defined as having nonnumeric characters (a tombstone) in the first position of the SSN member.
Second Part
Then I would like to define the function remove() in the Database which should locate the position of a record to be deleted and overwrite it with a null record. Then after a session is finshed. I would like to invoke a database destructor which copies the nonnull records to a new data file, deletes the old data file, and renames the new data file with the name of the old data file.
Here is what I have so far... Any help with code, advice, or tips would be greatly appreciated...
************************ personal.h *********************
public:
Personal();
Personal(char*,char*,char*,int,long);
void writeToFile(fstream&) const;
void readFromFile(fstream&);
void readKey();
int size() const {
return 9 + nameLen + cityLen + sizeof(year) + sizeof(salary);
}
bool operator==(const Personal& pr) const {
return strcmp(pr.SSN,SSN) == 0;
}
protected:
const int nameLen, cityLen;
char SSN[10], *name, *city;
int year;
long salary;
ostream& writeLegibly(ostream&);
friend ostream& operator<<(ostream& out, Personal& pr) {
return pr.writeLegibly(out);
}
istream& readFromConsole(istream&);
friend istream& operator>>(istream& in, Personal& pr) {
return pr.readFromConsole(in);
}
};
#endif
****************** personal.cpp *******************
#include <iostream>
#include "personal.h"
Personal::Personal() : nameLen(10), cityLen(10) {
name = new char[nameLen+1];
city = new char[cityLen+1];
}
Personal::Personal(char *ssn, char *n, char *c, int y, long s) :
nameLen(10), cityLen(10) {
name = new char[nameLen+1];
city = new char[cityLen+1];
strcpy(SSN,ssn);
strcpy(name,n);
strcpy(city,c);
year = y;
salary = s;
}
void Personal::writeToFile(fstream& out) const {
out.write(SSN,9);
out.write(name,nameLen);
out.write(city,cityLen);
out.write(reinterpret_cast<const char*="">(&year),sizeof(int));
out.write(reinterpret_cast<const char*="">(&salary),sizeof(int));
}
void Personal::readFromFile(fstream& in) {
in.read(SSN,9);
in.read(name,nameLen);
in.read(city,cityLen);
in.read(reinterpret_cast<char*>(&year),sizeof(int));
in.read(reinterpret_cast<char*>(&salary),sizeof(int));
}
void Personal::readKey() {
char s[80];
cout << "Enter SSN: ";
cin.getline(s,80);
strncpy(SSN,s,9);
}
ostream& Personal::writeLegibly(ostream& out) {
SSN[9] = name[nameLen] = city[cityLen] = '\0';
out << "SSN = " << SSN << ", name = " << name
<< ", city = " << city << ", year = " << year
<< ", salary = " << salary;
return out;
}
istream& Personal::readFromConsole(istream& in) {
char s[80];
cout << "SSN: ";
in.getline(s,80);
strncpy(SSN,s,9);
cout<<'['<<ssn<<']';
cout="" <<="" "name:="" ";
="" in.getline(s,80);
="" strncpy(name,s,namelen);
="" cout<<'['<<name<<']';
="" "city:="" strncpy(city,s,citylen);
="" cout<<'['<<city<<']';
="" "birthyear:="" in="">> year;
cout<<'['<<year<<']';
cout="" <<="" "salary:="" ";
="" in="">> salary;
cout<<'['<<salary<<']';
cin.ignore();
="" return="" in;
}
****************************="" student.h="" *********************
#ifndef="" student
#define="" student
#include="" "personal.h"
class="" student="" :="" public="" personal="" {
public:
="" student();
="" student(char*,char*,char*,int,long,char*);
="" void="" writetofile(fstream&)="" const;
="" readfromfile(fstream&);
="" int="" size()="" const="" {
="" personal::size()="" +="" majorlen;
="" }
protected:
="" char="" *major;
="" ostream&="" writelegibly(ostream&);
="" friend="" operator<<(ostream&="" out,="" student&="" sr)="" sr.writelegibly(out);
="" }
="" istream&="" readfromconsole(istream&);
="" operator="">>(istream& in, Student& sr) {
return sr.readFromConsole(in);
}
};
#endif
******************************** student.cpp ********************
#include <iostream>
#include "student.h"
Student::Student() : majorLen(10) {
Personal();
major = new char[majorLen+1];
}
Student::Student(char *ssn, char *n, char *c, int y, long s, char *m) :
majorLen(11) {
Personal(ssn,n,c,y,s);
major = new char[majorLen+1];
strcpy(major,m);
}
void Student::writeToFile(fstream& out) const {
Personal::writeToFile(out);
out.write(major,majorLen);
}
void Student::readFromFile(fstream& in) {
Personal::readFromFile(in);
in.read(major,majorLen);
}
ostream& Student::writeLegibly(ostream& out) {
Personal::writeLegibly(out);
major[majorLen] = '\0';
out << ", major = " << major;
return out;
}
istream& Student::readFromConsole(istream& in) {
Personal::readFromConsole(in);
char s[80];
cout << "Major: ";
in.getline(s,80);
strncpy(major,s,9);
return in;
}
***************************** database.h ************************
#ifndef DATABASE
#define DATABASE
template<class t="">
class Database {
public:
Database();
void run();
private:
fstream database;
char fName[20];
ostream& print(ostream&);
void add(T&);
bool find(const T&);
void modify(const T&);
friend ostream& operator<<(ostream& out, Database& db) {
return db.print(out);
}
};
#endif
*********************** database.cpp *************************
#include <iostream>
#include "personal.h"
#include "student.h"
#include "database.h"
template<class t="">
Database<t>::Database() {
}
template<class t="">
void Database<t>::add(T& d) {
database.open(fName,ios::in|ios::out|ios::binary);
database.seekp(0,ios::end);
d.writeToFile(database);
database.close();
}
template<class t="">
void Database<t>::modify(const T& d) {
T tmp;
database.open(fName,ios::in|ios::out|ios::binary);
while (!database.eof()) {
tmp.readFromFile(database);
if (tmp == d) { // overloaded ==
cin >> tmp; // overloaded >>
database.seekp(-d.size(),ios::cur);
tmp.writeToFile(database);
database.close();
return;
}
}
database.close();
cout << "The record to be modified is not in the database\n";
}
template<class t="">
bool Database<t>::find(const T& d) {
T tmp;
database.open(fName,ios::in|ios::binary);
while (!database.eof()) {
tmp.readFromFile(database);
if (tmp == d) { // overloaded ==
database.close();
return true;
}
}
database.close();
return false;
}
template<class t="">
ostream& Database<t>::print(ostream& out) {
T tmp;
database.open(fName,ios::in|ios::binary);
while (true) {
tmp.readFromFile(database);
if (database.eof())
break;
out << tmp << endl; // overloaded <<
}
database.close();
return out;
}
template<class t="">
void Database<t>::run() {
cout << "File name: ";
cin >> fName;
cin.ignore(); // skip '\n';
database.open(fName,ios::in);
if (database.fail())
database.open(fName,ios::out);
database.close();
char option[5];
T rec;
cout << "1. Add 2. Find 3. Modify a record; 4. Exit\n";
cout << "Enter an option: ";
while (cin.getline(option,5)) {
cout<<'['<<option<<']';
if="" (*option="=" '1')="" {
="" cin="">> rec; // overloaded >>
add(rec);
}
else if (*option == '2') {
rec.readKey();
cout << "The record is ";
if (find(rec) == false)
cout << "not ";
cout << "in the database\n";
}
else if (*option == '3') {
rec.readKey();
modify(rec);
}
else if (*option != '4')
cout << "Wrong option\n";
else return;
cout << *this; // overloaded <<
cout << "Enter an option: ";
}
}
int main() {
Database<personal>().run();
// Database<student>().run();
return 0;
}
|
|
|
|
|
Well, for starters, I don't see destructors to clean up those char * you're allocating, so you'll leak memory. HOwever, this is a lot of code to hope for people to read, you should really distill your post to a specific question, and a specific snippet that relates to it.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
Hello, I need to draw graphics dnamically during runtime on a dialog box.
Being a VC++ noob, how is it best ot do this? Should I draw directly to the dialog box within a predefined area or draw to some other control. I can do this to a ListView object, but is it possible to use a ListView on a dialog box, or again, is there some other control that I should use?
Thanks much!
|
|
|
|
|
You don't provide enough details for what you are trying to accomplish.
ctroyp wrote: Should I draw directly to the dialog box
Usually no, but you didn't state what you are trying to do. Your mention of the listview is rather confusing since it doesn't sound like you want a listview. You can place controls on a dialog box via the dialog editor, and this would include the listview.
In most cases, if there is no common control that fits your purpose, you write your own custom control. There are various articles on codeproject about doing this.
|
|
|
|
|
Let me begin again... Sometimes it's hard to know how to ask the right question.
I have a dialog box that contains some textboxes and such, but also want to draw graphics in a pre-defined drawing area (canvas) using CPen during runtime. In addition, this canvas needs to be bound to the dialog box and should not change. I am not concerned with how I will actually draw to this canvas at this time (until I know what I am using), but I just need to know what object to use for the canvas. What are my options for this canvas? Or should I just define a section on the dialog itself that can be drawn on with predefined bounds?
Does this make any better sense?
|
|
|
|
|
Create a custom control and place it on your dialog at the size you want.
Here[^] is an article on creating custom controls.
|
|
|
|
|
Thank you, I'll give it a try...
|
|
|
|
|
You could place a static control, there are articles about controls based on statics.
Elaine
|
|
|
|
|
I am new to Windows Forms programming using Visual C++ 2005. I'd like your help in writing a simple application which does the following:
When the executable is run, a window opens up with a display of an image that depicts whether the network cable is connected or not. There will be no other controls at present other than a button to exit the app.
Where should I place the code that detects the connection? Should it be in the Form1.h file or the networkapp.cpp(name of the app) file. Since the first thing after the form is created will be to check the network connection, I'm thinking it will go in the network.cpp file right after the
Application::Run(gcnew Form1());
If you could show me some sample code I'd appreciate that.
|
|
|
|
|
This is the wrong forum for managed C++ programming. You should direct your questions here[^] instead.
samaruf wrote: I'm thinking it will go in the network.cpp file right after the
Application::Run(gcnew Form1());
And have you actually attempted this? Instead of asking this sort of question you should try it out. You would then notice that your code never runs until you exit your form as Application::Run() is actually running the message loop.
samaruf wrote: Where should I place the code that detects the connection?
If it just detects the connection on startup, then there is the Load event for the form you can use.
|
|
|
|
|
hie , i just got VS.NET 2005 pro.. and it seems that only C# , J# and VB have the office 2003 look... but when i build a resource in VC .. i dont have the new look.Everything in THE RESOURCE EDITOR is just the regular Xp style.I tried compiling an old VC6 project( i did choose to convert it to .NET) .And i dont even have the regular XP look!!! am i supposed to manually load the comclt 6.0? like VC6 ?
thank in advance!
Gideon
|
|
|
|
|
giddy_guitarist wrote: it seems that only C# , J# and VB have the office 2003 look
For the IDE? The IDE still looks office 2003ish in C++ mode. I guess the margin/ruler tool in the dialog editor does look rather dated, and they seem to have neglected to update the icons for the toolbox.
giddy_guitarist wrote: but when i build a resource in VC .. i dont have the new look
If you are referring to how your application looks, VS2005 does not come with a new version of the common controls. Those come with new Windows versions.
giddy_guitarist wrote: And i dont even have the regular XP look!!!
You need to include a manifest. VS2005 comes with a manifest tool which you can configure in your project properties to make it easier to embed the manifest. It embeds manifest by default on new projects.
|
|
|
|
|
um i dont mean the IDE at all... i 'm only talking about the look of the application i would be building in VS.
http://gideon.110mb.com/sshot.html[^] look at these screenies
??
|
|
|
|
|
That's a Windows Forms .NET control. Are you using the .NET framework?
That said, I was quite disappointed with the quality of the menustrip/toolstrip expecting it to be a bit more closer to the actual ones instead of a superficial paint job. Still it was better than nothing since you do little work to get it.
If you are not developing a commercial application then this[^] might be of interest to you. If are are developing a commercial app, you might want to fork out the license fees to get good UI components (from FOSS Software, Codejock etc)
If you want free, then you've got quite a bit of work to do. You can modify the existing controls like CMenuXP[^] but expect bugs.
|
|
|
|
|
This should convert characters to lower case, however, it is not compiling. Would I be able to work around this issue, since I’m using arrays of chars and not string objects? Type conversion?
#include <string>
..
string CSentenceAr::LowerCase(char stringtoConvert)
{//change each element of the string to lower case
for(unsigned int i=0;i<strToConvert.length();i++)
{
strToConvert[i] = tolower(strToConvert[i]);
}
return strToConvert;//return the converted string
}
..
This is a console application, by the way, does it make a difference?
In order to implement this code the /Ehsc option seems to be expected by the compiler (MSDN). Can this be done from VS 2003?
Jon
|
|
|
|
|
jon_80 wrote: string CSentenceAr::LowerCase(char stringtoConvert)
{//change each element of the string to lower case
for(unsigned int i=0;i < strToConvert.length();i++)
The parameter, strToConvert , is of type char . So you won't find the length() member function.
Maxwell Chen
|
|
|
|
|
in my app, I use new operator to alloc memory.
each "new" allocs 200k to 3 M bytes for multiple times, total size alloced around 20M.
the app crashes at a "new operator".
I am wondering why it crashes just for allocing total 20M bytes.
but in Debug mode, the app runs properly. it crashs only in Relese mode, why?
Question:
which way is the best for allocing memory? GlobalAlloc() or new? any explaination?
|
|
|
|
|
Can you add some message boxes (with debugging text) to pause step-by-step each time when it is trying to allocate in release build?
There should be something wrong in your code. Couple days ago I made an application which allocated many of small memory blocks to constitute a 1 GB disk file. I only used new in the code, and it worked just fine.
Maxwell Chen
|
|
|
|
|
includeh10 wrote: the app crashes at a "new operator".
does it give you any message when the crash happens ?
includeh10 wrote: GlobalAlloc() or new?
unless you need the specialized features that GlobalAlloc provides (ex. shared or locked memory), you should use new.
Why donchoo take a peekchur mayn?
OK, cleeeeek
|
|
|
|
|
For the application to crash in the call to "new" one or both of the situations below may be hapening:
1) You call new 1 and then trash the memory thus making new 2 fail because the heap memory list gets corrupted.
2) The new is creating an object which has an error in the constructor and so crashes the application during construction.
To determine what might be happening try the following code:
#include <stdio.h>
void main(void) {
long ttsz=0;
long bsz;
char* bp;
while(1) {
printf("Input size (in KB): ");
scanf("%d", &bsz);
bsz*=1024;
printf("Allocating a size of %d\n", bsz);
bp=new char[bsz];
printf("Allocated a size of %d into 0x%p\n", bsz, bp);
ttsz+=bsz;
printf("Total allocated size (in KB): %d\n", ttsz/1024);
printf("\n");
}
}
I tried this code several times and it works fine.
You should use GlobalAlloc only when strictly necessary. It is not as simple nor as portable as new.
I hope this helps,
Rilhas
-- modified at 17:10 Friday 4th August, 2006
|
|
|
|
|
now i had implemented through PfAddFiltersToInterface(ihandle,1,&inFilter,0,NULL,&fHandle) instead of addglobal filter to interface so that i could specify the ip port+ip to block of my pc.
but it did not work it depends only the parameter passed in pfcreateinterface rather then in addfilter to interface or i might be interpretting the src and destination as wrong. the complete code below
if(::PfCreateInterface(0,PF_ACTION_FORWARD,PF_ACTION_FORWARD,false,true,&ihandle)==NO_ERROR)
{
AfxMessageBox("Step 1 Done");
BYTE ip[4]={202,165,250,50};
BYTE msk[4]={255,255,255,0};
BYTE dm[4]={0,0,0,0};
inFilter.dwFilterFlags = FD_FLAGS_NOSYN; //always this value
inFilter.dwRule = 0; //always this value
inFilter.pfatType = PF_IPV4; //using ipV4 addresses
inFilter.SrcAddr = (PBYTE)dm;
inFilter.SrcMask = (PBYTE)dm; //mask for local ip
inFilter.DstAddr = (PBYTE)ip; //any destination
inFilter.DstMask = (PBYTE)msk;
inFilter.wSrcPort =0;//any source port
inFilter.wSrcPortHighRange=4000;
inFilter.wDstPort = 80; //destination port 80(http service)
inFilter.wDstPortHighRange=80;
inFilter.dwProtocol =(DWORD)FILTER_PROTO_ANY; //Tcp protocol
DWORD err;
err=::PfAddFiltersToInterface(ihandle,1,&inFilter,0,NULL,&fHandle);
if(err==NO_ERROR)
{
if(::PfBindInterfaceToIPAddress(ihandle,PF_IPV4,(PBYTE)ip)==NO_ERROR)
{
AfxMessageBox("IP Adress Binded");
}
}
}
i think input according to input filter it should block on port 80 but i can browse internet through that port.
either i m misunderstanding the src and destination or these api not working.
Tasleem Arif
|
|
|
|
|
Hi guys,
When I am doing my development on MFC, a weird case puzzled me.
I implemented a control which will send a windows message to its parent in some case. For example, it will send its parent a message about 'ON_KEYDOWN' using GetParent()->SendMessage(WM_KEYDOWN.... And I override OnPreTranslateMessage method in its parent control, which I suppose it can catch the WM_KEYDOWN mesasge and do some work on the message.
The code in OnPreTranslateMessage looks like:
void CParentControl::OnPreTranslateMessage(...)
{
if( pMsg->Message == WM_KEYDOWN)
{
....// do some thing here
}
CWnd::OnPreTranslateMessage(..);
}
However, the fact is, in most case OnPreTranslateMessage do can catch the message. But in some case it can not -- and I can sure that the child control absolutely had sent the message.
And I also tried another approach: I use message map to catch ON_KEYDOWN . This time the message can also be catched.
Is there any difference with this 2 approach? In what cases the message will escape from OnPreTranslateMessage but still can be catched by message map?
Sorry I may not describe the question clearly because the limitation of my English.
I really appreciate any one who can give me any clues.
Thanks!
Findekano@Shanghai, China
|
|
|
|
|