|
Try CRT debug support. Check out this CP article[^]
Best,
Jun
|
|
|
|
|
Hi,
I am using Visual C++ 6.0 MFC and want to store objects into an array. I tried using CArray but once I got the copy contructor so it would not give me compile errors I then got LINK errors. Since so many people on the internet sugested to others trying to use CArray to use std::vector instead I decided to use std::vector, the compiler gives me couple of warnings, but I still get LINK errors.
If I do this:
std::string strData = "One";
std::vector<std::string> strVector;
strVector.push_back(strData);
std::string strData = "Two";
strVector.push_back(strData);
It compiles, links, and executes just fine.
The object that I am trying to store is,
class CInstrument : public CObject
{
public:
CInstrument();
CInstrument(const CInstrument &Instrument);
CInstrument& operator = (const CInstrument);
virtual ~CInstrument();
int m_dGPIBAddress;
}
The .cpp file implementation is:
CInstrument Instrument;
std::vector<cinstrument> Instruments;
Instrument.m_dGPIBAddress = 9;
Instruments.push_back(Instrument); // If I comment this lin out I do not get link errors
Does anyone see whats wrong here? Also, as a side note, Visual Studio has a popup that comes up after the ObjectName'.' so you can select the objects members. The popup does not come up for the Instruments'.' vector. This shouldn't be this difficult as I have already spent 12 hours trying to build an array of objects of class CInstrument.
Thanks,
Buck
|
|
|
|
|
You need to use the pre, code, < and > Formatting buttons at the bottom of the editor area otherwise we can't see your code correctly.
You also need to tell us the specific compiler and linker errors you are getting.
Neville Franks, Author of Surfulater www.surfulater.com "Save what you Surf" and ED for Windows www.getsoft.com
|
|
|
|
|
Need to know if there is an API
1) to get the directory listing
2) To be able to search for a particular file extension
As for eg
Say i have a file i need to search for like AM01.txt in C:\temp1\temp2
I need an API to list the folders --> temp1 and then temp2
Then an API to filter *.txt files in that folder
Any ideas ?
Engineering is the effort !
|
|
|
|
|
have you looked at CFileFind class?
|
|
|
|
|
act_x wrote: 1) to get the directory listing
Use the FindFirstFile() /FindNextFile() pair.
act_x wrote: 2) To be able to search for a particular file extension
See above, just use *.txt in the pattern.
If you know the exact name of the file, and simply want to check for existence, use _access() or GetFileAttributes() instead.
"The largest fire starts but with the smallest spark." - David Crow
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
Thanks for your responses
Engineering is the effort !
|
|
|
|
|
See
<br />
(1)<br />
TCHAR lpt[260];<br />
lpt[0]='\0';<br />
strcat(lpt,"c:\\");<br />
GetCurrentDirectory(sizeof(lpt),lpt);<br />
DlgDirList(lpt,IDC_LIST2,NULL,DDL_DIRECTORY|DDL_ARCHIVE);
-----------------------------<br />
(2)<br />
WIN32_FIND_DATA find;<br />
HANDLE handle=FindFirstFile("*.*",&find);<br />
while(FindNextFile(handle,&find)!=0)<br />
m_List2.AddString(find.cFileName);
FindClose(handle); <br /> maybe it is some helpful to you
whitesky
|
|
|
|
|
I'm having trouble grasping Binary file I/O.
I am trying to read back a binary file that includes the following example...
char type[9]="File Type"
short MajorVersion = 1
short MinorVersion = 0
Using most methods found, I can read "File Type" but it's got garbage appended to it, so I cannot compare with a known type.
Also, I'm having trouble going beyond that. Meaning, I cannot access the version shorts.
The examples I've come across doesn't go into any depth about multiple types, or how to retrieve multiple items from a single buffer read.
Do I need to flush the buffer, and reposition my pointer?
What's the best way about getting this information out of my file?
Can someone give me a quick example of how to retrieve this data, and what method is most efficient?
Jeff
|
|
|
|
|
Jumpin` Jeff wrote: Using most methods found, I can read "File Type" but it's got garbage appended to it...
Exactly how are you reading the file? If you are reading nine bytes from the file, it should work.
Try this:
struct
{
char type[9];
short MajorVersion;
short MinorVersion;
} myStruct;
FILE *pFile = fopen(..., "rb");
fread(&myStruct, sizeof(myStruct), 1, pFile);
fclose(pFile); Depending on what you do with type , you may need room for a \0 character.
"The largest fire starts but with the smallest spark." - David Crow
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
Thank you for such a quick responce David!
Using your example, I come up with similar to previous results....
The version shorts should have been Unsigned, My bad, but I used the following to display the extracted information....
cout << myStruct.type << "\n" << myStruct.MajorVersion<<"."<
|
|
|
|
|
Jumpin` Jeff wrote: File Type@ <- Actually a smiley symbol...
You will not be able to treat it as a nul-terminated string unless it actually ends with a \0 character.
"The largest fire starts but with the smallest spark." - David Crow
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
Just a recommendation:
Instead of assuming your string is X number of characters long, write the string length first, followed by the characters in the string:
struct FileData<br />
{<br />
char* String;<br />
short MajorVersion;<br />
short MinorVersion;<br />
};<br />
<br />
FileData data;<br />
FILE pFile = fopen(..., "wb");<br />
long size = strlen(data.String);<br />
fwrite(&size, sizeof(size), 1, pFile);<br />
fwrite(data.String, size, 1, pFile);<br />
fwrite(&data.MajorVersion, sizeof(short), 1, pFile);<br />
fwrite(&data.MinorVersion, sizeof(short), 1, pFile);<br />
<br />
FileData data1;<br />
FILE pReadFile = fopen(..., "rb");<br />
long readSize = 0;<br />
fread(&readSize, sizeof(readSize), 1, pReadFile);<br />
assert(readSize > 0);<br />
data1.String = new char[readSize + 1];<br />
memset(data1.String, 0, readSize + 1);<br />
fread(&data1.String, readSize, 1, pReadFile);<br />
fread(&data1.MajorVersion, sizeof(short), 1, pReadFile);<br />
fread(&data1.MinorVersion, sizeof(short), 1, pReadFile);<br />
If you write the structure as a class, you can put the serialization code as a class member and have your code just call MyClass.Read/Write to make it very simple.
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
-- modified at 16:03 Friday 23rd June, 2006
|
|
|
|
|
The string "File Type" is 9 characters long; you are not writing/reading the null terminator. (Oddly, the compiler should have warned you about an overflowed buffer.)
David's example is good, except I would make type 10 characters. I would also place #pragma pack(1) before the structure and #pragma pack(0)#pragma pack() after.
Anyone who thinks he has a better idea of what's good for people than people do is a swine.
- P.J. O'Rourke
-- modified at 16:36 Friday 23rd June, 2006
|
|
|
|
|
Thanks Joe, but there is no null terminator.
That is the way the file is previously constructed. My example is just a very simple representation of the actual file.
It is X characters, followed by a file major version, then file minor version identifier.
Can you explain what #pragma pack will do for me?
|
|
|
|
|
Jumpin` Jeff wrote: Can you explain what #pragma pack will do for me?
See here.
"The largest fire starts but with the smallest spark." - David Crow
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
Ok, I added the #pragma pack as you suggested, but left the type 9 characters, for when I changed it to 10, I came up with exactly what I did with David's example. The #pragma packs fixed the version data, but not the type. Still reads "File Type@", and I get 3 warnings...
warning C4081: expected 'newline'; found ';'
warning C4086: expected pragma parameter to be '1', '2', '4', '8', or '16'
warning C4081: expected 'newline';
Jeff
|
|
|
|
|
My mistake, it should be #pragma pack()
For the compare, try strncmp(struct.string, realString, sizeof(struct.string))
Anyone who thinks he has a better idea of what's good for people than people do is a swine.
- P.J. O'Rourke
|
|
|
|
|
Here we have a simple entry point into a DLL
<br />
AFX_EXT_CLASS bool EntryPoint( void );<br />
<br />
and:<br />
<br />
bool EntryPoint()<br />
{<br />
return true;<br />
}<br />
Which we can use with AfxLoadLibrary, and GetProcAddress. Yay and hooray! What I need is a way to do that with EXEs. See I need to be able to run some code dynamically based on files in a directory, and it has to work on both DLL's and EXE's. I thought I have read in the past about alternative entry points on EXE's, and you can load an EXE with AfxLoadLibrary, but you can't use GetProcAddress on it.
|
|
|
|
|
Like form a DLL, you can export a function from an EXE as well.
Ovidiu Cucu
Microsoft MVP - Visual C++
|
|
|
|
|
Can someone tell me what is going on in the ObjectRoot constructor and also how the ObjectRootPtr is declared. Is the pointer pointing to a new array called ms_instances of size 21 and assigns a value of NULL to all ms_instance[0], ms_instance[1],.....ms_instance[20].....
PS: WHAT DOES THE SECOND LINE MEAN .... ARE YOU MAKING THE POINTER ObjectRootPtr point to class member ms_instances of size 21. I thought it was something like ObjectRootPtr = new ms_instances;
typedef ObjectRoot* ObjectRootPtr; <br />
static ObjectRootPtr ms_instances[MAX_INSTANCES];<br />
<br />
RTI::RTIambassador* ObjectRoot::ms_rtiAmb = NULL; <br />
RTI::ObjectClassHandle ObjectRoot::ms_classId = 0;<br />
const char* ObjectRoot::ms_classIdStr = NULL;<br />
<br />
ObjectRootPtr ObjectRoot::ms_instances[MAX_INSTANCES] =
{ NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,<br />
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL };<br />
unsigned int ObjectRoot::ms_numInstances = 0;<br />
<br />
ObjectRoot::ObjectRoot()<br />
{<br />
if (ms_numInstances < MAX_INSTANCES)
ObjectRoot::ms_instances[ ObjectRoot::ms_numInstances++ ] = this;<br />
}
-- modified at 14:19 Friday 23rd June, 2006
|
|
|
|
|
I'll try to explain line by line.
static ObjectRootPtr ms_instances[MAX_INSTANCES];
Array of static pointers.
RTI::RTIambassador* ObjectRoot::ms_rtiAmb = NULL;
RTI::ObjectClassHandle ObjectRoot::ms_classId = 0;
const char* ObjectRoot::ms_classIdStr = NULL;
Initialization of static and constant static data.
ObjectRootPtr ObjectRoot::ms_instances[MAX_INSTANCES] =
{ NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL };
Initialization of the static pointers defined by the second line above.
Jay03 wrote: PS: WHAT DOES THE SECOND LINE MEAN .... ARE YOU MAKING THE POINTER ObjectRootPtr point to class member ms_instances of size 21. I thought it was something like ObjectRootPtr = new ms_instances;
No. It defines an array of pointers. No instantiation involved. It's easy to understand if you replace ObjectRootPtr with ObjectRoot*. The first line defines this replacement.
Best,
Jun
|
|
|
|
|
For the first part..... what do STATIC POINTERS DO?
In the last part. ObjectRootPtr is pointer to an ObjectRoot object. Now this ObjectRootPtr was pointing to ms_instances[21] of size 21. And finally the ms_instances[21] points to NULL
I believe this is what you meant.
The big picture is:
ObjectRootPtr ---> ms_instances[0] --- > NULL
---> ms_instances[1] --- > NULL
---> ms_instances[2] --- > NULL
---> .............. --- > NULL
---> .............. --- > NULL
---> ms_instances[20] --- > NULL
BTW Thanks for your reply JUN
|
|
|
|
|
The pointers are not static. The memory allocated to hold the pool of pointers is. Basically, they only want to have 21 instances (maximum) of this object. They created a pool to hold references for all active instances. At startup, those instances are initialized to NULL (since they don't point to anything).
The declaration static ObjectRootPtr ms_instances[21] is declaring a static array of 21 pointers to ObjectRoot objects.
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
|
|
|
|
|
You're right. It's a static array of pointers, not an array of static pointers. Thanks for pointing this out
Best,
Jun
|
|
|
|
|