|
How do I copy/convert a character array to a std::string?
Thanks for any insight.
William
Fortes in fide et opere!
|
|
|
|
|
1. Create a string object
2. Use it's member method 'operator +=' to append characters to string, specifying the start of the array as the parameter.
Example:
#include <string>
using namespace std;
<DIV>
... Later in the code...
<DIV>
<DIV>
string myString;
<DIV>
myString += &charArray[0];
<DIV>
Not completely sure if this will work, haven't tested it. But according to the Standard C++ Library Reference, it should go like this
-Antti Keskinen
----------------------------------------------
The definition of impossible is strictly dependant
on what we think is possible.
|
|
|
|
|
Thanks for replying.
I had tried the same technique you offered (plus about maybe 20 others) before posting the question, and nothing has worked!
Short of writing a routine to specifically transfer one character at a time from the array to the string, I thought the resiliency of string might have something that would do the job for me. No luck so far.
William
Fortes in fide et opere!
|
|
|
|
|
|
Thanks for replying.
While it is true the program compiles and links without any error, I believe the reason why I am NOT getting the desired behavior is due to a known bug a fix from MS doesn't seem to have corrected.
William
Fortes in fide et opere!
|
|
|
|
|
UNICODE issues?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<br />
Peter Weyzen<br />
Staff Engineer<br />
<A HREF="http://www.santacruznetworks.com">Santa Cruz Networks</A>
|
|
|
|
|
try using wstring.
Christian
I have drunk the cool-aid and found it wan and bitter. - Chris Maunder
|
|
|
|
|
I am right now in the process of trying to make the computer search for a certain string. I am looking in text files, also I'm using console(don't ask)
<marquee>Universal Project... Soon to be a .net
|
|
|
|
|
Read in all data from your file into a CString, and search for a pattern with CString::Find.
CString sString;
CFile file("c:\\pathtoyourfile.dat",CFile::modeRead);
file.Read(sString.GetBuffer(file.GetLength()),file.GetLength());
sString.ReleaseBuffer(file.GetLength());
sString.Find("Your pattern",0);
Take a look in MSDN at CFile, and CString.
Peter Molnar
|
|
|
|
|
Very well, but how can you do this in the console c++?
Old United Airlines Brochure:
If you cannot read this please ask a superintendent to help you... LOL, for real!!!
|
|
|
|
|
Use a FILE structure in combination with 'fopen', 'fclose' and 'fread' functions. Read the entire file to a LARGE char array (say 'char myArray[8000]'). Then use stricmp function to compare the file with your match string.
Of course, if your file is larger, you should use the heap memory instead of stack. Just create a 'char* ptrArray = new char[80000]' and read it there. Remember to delete it when finished.
See Run-Time Library Reference for more details on file handling.
-Antti Keskinen
----------------------------------------------
The definition of impossible is strictly dependant
on what we think is possible.
|
|
|
|
|
Yes, I will allocate some memory since the file will be in constant change and allocation is a very good memory manager.
Old United Airlines Brochure:
If you cannot read this please ask a superintendent to help you... LOL, for real!!!
|
|
|
|
|
Create a console application with MFC support, and use the above code.
If you don't use MFC: The CString is part of the Win32 library in VS 7.0 and upwards, so you only have to change the file read in operation (see CreateFile,ReadFile Win32 functions).
Peter Molnar
|
|
|
|
|
Peter Molnar wrote:
The CString is part of the Win32 library in VS 7.0 and upwards
CString is still part of MFC/ATL, though you can use it by simply including atlstr.h .
Anyone who thinks he has a better idea of what's good for people than people do is a swine.
- P.J. O'Rourke
|
|
|
|
|
Using memory mapped file functionality, map the destination file. Then search it using one of the following implementations of a Boyer-Moore text search:
const char* TextSearch(const char* pSearchStr, const char* pStr, int textLen)
{
if (!pSearchStr || !*pSearchStr)
return pStr;
if (!pStr || !*pStr)
return NULL;
if (textLen < 0)
textLen = (int)strlen(pStr);
int searchStrLen = (int)strlen(pSearchStr);
int pSkipTable[256];
int i;
for (i = 0; i < 256; i++)
pSkipTable[i] = searchStrLen;
for (i = 0; pSearchStr[i]; i++)
pSkipTable[(BYTE)pSearchStr[i]] = searchStrLen - 1 - i;
register int right = searchStrLen - 1;
while (right < textLen)
{
int j;
for (j = searchStrLen - 1, i = right; j >= 0 && pSearchStr[j] == pStr[i]; j--)
i--;
if (j == -1)
return &pStr[i + 1];
right = max(i + pSkipTable[(BYTE)pStr[i]], right + 1);
}
return NULL;
}
const char* TextSearchNoCase(const char* pSearchStr, const char* pStr, int textLen)
{
if (!pSearchStr || !*pSearchStr)
return pStr;
if (!pStr || !*pStr)
return NULL;
if (textLen < 0)
textLen = (int)strlen(pStr);
int searchStrLen = (int)strlen(pSearchStr);
int pSkipTable[256];
int i;
for (i = 0; i < 256; i++)
pSkipTable[i] = searchStrLen;
char* pSearchStrUpper = (char*) _alloca(searchStrLen);
memcpy(pSearchStrUpper, pSearchStr, searchStrLen);
CharUpperBuffA(pSearchStrUpper, searchStrLen);
char* pSearchStrLower = (char*) _alloca(searchStrLen);
memcpy(pSearchStrLower, pSearchStr, searchStrLen);
CharLowerBuffA(pSearchStrLower, searchStrLen);
for (i = 0; i < searchStrLen; i++)
{
pSkipTable[(BYTE)pSearchStrUpper[i]] =
pSkipTable[(BYTE)pSearchStrLower[i]] = searchStrLen - 1 - i;
}
register int right = searchStrLen - 1;
while (right < textLen)
{
int j;
for (j = searchStrLen - 1, i = right; j >= 0 &&
(pSearchStrLower[j] == pStr[i] || pSearchStrUpper[j] == pStr[i]); j--)
i--;
if (j == -1)
return &pStr[i + 1];
for (j = searchStrLen - 1; j >= 0; j--)
{
if (pSearchStrLower[j] == pStr[i] || pSearchStrUpper[j] == pStr[i])
{
right = max(i + pSkipTable[(BYTE)pStr[i]], right + 1);
break;
}
}
if (j < 0)
right = max(i + searchStrLen, right + 1);
}
return NULL;
}
Anyone who thinks he has a better idea of what's good for people than people do is a swine.
- P.J. O'Rourke
|
|
|
|
|
I am getting this error when a specific class reaches the destructor in debug mode.
_CrtIsValidHeapPointer(pUserData)
The class is a template class, and the memory is allocated in the constructor of the class. There are no problems in Release mode, but I don't want to write the rest of this application without using the debugger. Is there a way of either turning this off, or convincing the computer that the memory actually exists in the heap?
If this is a common error, maybe some insight as to causes for receiving it would be great. Thanks a lot for any help!
I have never failed when programming. Instead I have succeeded in finding thousands of ways of crashing a computer!
Douglas A. Wright
dawrigh3@kent.edu
|
|
|
|
|
I'm currently working on a project,wherein i have to display the Tree structure of the directories as it is in (WINDOWS EXPLORER).Can anyone tell how to access the SHELL in Win32 Api(VC++) as im working in this platform.
NO MFC please..
|
|
|
|
|
|
Thanx for the link...but i'm working in plain win32 Api "NO MFC"...anyways thanx again for the help..if u find anything that has no MFC for displaying folders using SHELL in WIn32 please send me some reference..
|
|
|
|
|
MFC is nothing but a wrapper over the win32, so if you understand what that link is trying to say you can convert that logic into win32 calls in ur application.
but if you looking for spoon feeding anwers, then wait let me get my spoon.
The World is getting smaller and so are the people.
|
|
|
|
|
How can I get this icon into an application?
Is there any way I can use SHGetFileInfo() without knowing a head of time the name of any document in particular?
Thanks
The word of the day is legs, let's go back to my house and spread the word
|
|
|
|
|
1.
The path to the file containing the HTML icon can be found in the default value of the foloowing registry key:
HKEY_CLASSES_ROOT\htmlfile\DefaultIcon
which usually "C:\Program Files\Internet Explorer\iexplore.exe,1", that is the first icon stored in the iexplore.exe. You can extract it from there.
2.
Take a screenshoot of windows explorer when it shows an HTML file, and add this bitmap to your project.
3.
Alternatively you can feed an CImageList with all the default icons, among others with the html default icon:
HIMAGELIST hSystemImageList;
SHFILEINFO ssfi;
hSystemImageList = (HIMAGELIST)SHGetFileInfo((LPCTSTR)_T("c:\\"),0,&ssfi,sizeof(SHFILEINFO),SHGFI_SYSICONINDEX | SHGFI_ICON);
m_ImageList.Attach(hSystemImageList);
pListCtrl->SetImageList(&m_ImageList,LVSIL_NORMAL);
And to find a path's index in the ImageList:
SHGetFileInfo((LPCTSTR)sPathToYourFile,0,&sfi,sizeof(SHFILEINFO),SHGFI_SYSICONINDEX | SHGFI_ICON );
pListCtrl->InsertItem(nInsertPos,sPathToYourFile,sfi.iIcon);
where sPathToYourFile should contain a valid path to an html file.
Peter Molnar
|
|
|
|
|
Peter Molnar wrote:
HKEY_CLASSES_ROOT\htmlfile\DefaultIcon
Cool, thats what I was looking for
Thanks
The word of the day is legs, let's go back to my house and spread the word
|
|
|
|
|
Hi!
I want to duplicate an object previously instanced:
<br />
CMyClass* pMyVar1=new CMyClass ();<br />
pMyVar1->Var="Value";<br />
CMyClass* pMyVar2=new CMyClass ();<br />
CopyMemory (pMyVar2, pMyVar1, sizeof(pMyVar1));<br />
This code doesn't work, because I think that the sizeof is not correct.
Is there a way to copy an object with all his values?
Thks in advance!
Appstmd
http://www.appstmd.com
|
|
|
|
|
Something along the lines of
*pMyVar2 = *pMyVar1;
I just made a simple program that does the same thing and it works.
#include <iostream>
using namespace std;
class Class1
{
public:
int var1;
int var2;
};
int main()
{
Class1 * object1 = new Class1();
Class1 * object2 = new Class1();
object1->var1 = 1;
object1->var2 = 3;
object2->var1 = 2;
object2->var2 = 4;
cout << "Object1 Var1 = " << object1->var1 << endl;
cout << "Object1 Var2 = " << object1->var2 << endl;
cout << "Object2 Var1 = " << object2->var1 << endl;
cout << "Object2 Var2 = " << object2->var2 << endl << endl;
*object2 = *object1;
cout << "Object2 Var1 = " << object2->var1 << endl;
cout << "Object2 Var2 = " << object2->var2 << endl;
delete object1;
object1 = NULL;
delete object2;
object2 = NULL;
system( "pause" );
return 0;
}
object2 was switched in the output so I don't know why it wouldn't work for a more complicated class.
The question "Do computers think?" is the same as "Can submarines swim?"
DragonFire Software
Jeryth
|
|
|
|