|
How can I do that? I tried the function replace, but it doesn't work and I also tried to add \0 to a string before i Added \r\n to the text, but it's not working(Not sure why). I'm starting to run out of idea.
|
|
|
|
|
"This is a null terminated string\0"
You can use the strtok() functions to find \r\n, though it would help if we knew how you are creating the string
char *str = new char [10];
strcpy(str,"hello\r\n");
int x = strlen(str);
str[x-2] = '\0';
x = strlen(str);
delete[] str;
Works for me.
-- modified at 17:46 Sunday 27th August, 2006
|
|
|
|
|
I'm reading the string from a binary file and i replace the null at the end of each lines by \r\n for display purpose in an editbox. After I'm taking the text from the editbox and I want to put it back into the binary file.
here is what i currently have to write it.
The vars:
CFile fVampFile;
String * strInfo = "";
char * cInfo = "";
int iEnd;
The code:
strInfo = eSpeech->Text;
strInfo->Replace("\r\n","\0");
cInfo = (char*)Marshal::StringToHGlobalAnsi(strInfo).ToPointer();
iEnd = strlen(cInfo);
fVampFile.Write(cInfo,iEnd);
-- modified at 17:58 Sunday 27th August, 2006
I forgot to mention this, but the string can have 3 /r/n in it at different places, because each string can have up to 3 sentences in it and all 3 need to end with null.
and the code you gave me removes the /r/n but null isn't added.
|
|
|
|
|
Is thst String a CString or a basic_string ? The CString class has a TrimRight() member that you might want to try.
|
|
|
|
|
It won't work, because i can have up to 3 /r/n in the string.
Also do you know if CFile.write will write the null to the file? I was wondering if it could be the reason why I can't add a null to a string.
|
|
|
|
|
This function should write everything, but the terminating '\0' is not included in string length.
So try fVampFile.Write(cInfo,iEnd + 1);
|
|
|
|
|
samkook wrote: It won't work, because i can have up to 3 /r/n in the string.
Which means you should be using strtok() and not strlen() .
samkook wrote: Also do you know if CFile.write will write the null to the file? I was wondering if it could be the reason why I can't add a null to a string.
CFile , and its methods, has no bearing on how you manipulate a string.
"Money talks. When my money starts to talk, I get a bill to shut it up." - Frank
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
samkook wrote: strInfo->Replace("\r\n","\0");
This obviously will not work. You can't use string-related functions, which expect nul-terminated strings, to add/find/replace a nul character. Doing do effectively results in nothing happening.
"Money talks. When my money starts to talk, I get a bill to shut it up." - Frank
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
One way to do it. It removes any number of trailing CR and/or LF combinations:
<br />
char str[20];
int index;<br />
<br />
strcpy(str, "hello\r\n");<br />
index = strlen(str);<br />
<br />
if(index) {<br />
index--;<br />
for(; index >= 0; index--) {<br />
if(str[index] == '\r' || str[index] == '\n') str[index] = '\0';<br />
else break;<br />
}<br />
}<br />
<br />
<br />
Else, you can use a CString or str
Alcohol. The cause of, and the solution to, all of life's problems - Homer Simpson
|
|
|
|
|
Thanks everyone for all your help, I now have everything to solve this problem. Finally, I will separate the sentences, because it will be simpler and thanks fefe for pointing to me that null wasn't included in the string length.
And one last thing, the code you gave me kakan ended with only null written in the file, i'm not sure what was wrong. Also if I understand correctly, it will write two null at the end of each sentence and it's very important that it be only one.
And sorry if there are thing that doesn't make sence in my post, i'm really tired and english is not my first language.
|
|
|
|
|
Hello.
I just debugged the code I gave you and it works as it should.
It removes any CR LF combinations from the end of the string.
The result is stored in str (assuming you do not delete the string before writing it to the file!).
My code will replace all terminating CR and LF with nulls in the buffer, that's right. But when you later uses the resulting buffer, it's the first null char that counts. Simply because a null char is seen as a string terminator.
If you want the length of the resulting string, just use strlen() (or it's variants).
PS.
English isn't my native lingo either.
Alcohol. The cause of, and the solution to, all of life's problems - Homer Simpson
|
|
|
|
|
Hello All,
I have a question. I am about to create some software. I plan on using MFC MDI with two form views. My question regards sizing it so that it looks appropriate on different screens since users tend to have different screen resolutions. What is the best strategy to ensure that my software shows up properly on different screen settings? How can I make the contents of my form view (and any view for that matter) scale properly within limits?
Any help you can provide would be very helpful.
|
|
|
|
|
You should take a look at the GetDeviceCaps() function which will give you the sizes you need to know about for the screen. A common method of creating a correct sized window is to scale it according to the size of the font used.
|
|
|
|
|
Thanks for your reply. Can you elaborate a little bit more or point me to a source that outlines that strategy.
Thanks again,
BP
|
|
|
|
|
See here[^] maybe it is some helpful to you
|
|
|
|
|
Thank you very much. It looks like that article will be of great help me.
BP
|
|
|
|
|
I glad that its good for you
|
|
|
|
|
I've heard of differences between debug and release builds, but never of with the bugger or without. I have a very simple console app printing the length of unicode strings to the screen. With a debug version, one of the values is being displayed wrong (should be 4 but shows 10). If I step through the code, it's correct, it display the correct value to the screen. If I hit the run button, the value is wrong. Any idea what could be causing this? Does the debugger make any changes to the code or any changes to the memory managment?
|
|
|
|
|
waldermort wrote: I've heard of differences between debug and release builds, but never of with the bugger or without. I have a very simple console app printing the length of unicode strings to the screen. With a debug version, one of the values is being displayed wrong (should be 4 but shows 10). If I step through the code, it's correct, it display the correct value to the screen. If I hit the run button, the value is wrong. Any idea what could be causing this? Does the debugger make any changes to the code or any changes to the memory managment?
Can you show the piece of code.
|
|
|
|
|
I managed to track down the problem, it was a simple case of not correctly nulling the memory before copying a string. But still, When running through the debugger I was not able to find it. I had to literaly follow the code line by line.
|
|
|
|
|
Hi everybody.
I want to add an Communication control on my dialog based project in VC2005.
In VC6.0 I was using Project->Add to project->Add Components and controls etc
In 2005 under the project menu I do not have Add to project
When I created new MFC project I selected Dialog based and made sure ActiveX control radio was checked.
Under Tools->Customize (Commands tab) I have Rearrange Commands->(Menu bar checked) select Project pressed Add, under categories select again Project and I can see "Add component" and "Add user control". However even if I added them they are greyish under Project menu.
Any ideas how can I get "Add to project" for VC2005?
Thanks
Marian
marian
|
|
|
|
|
ok, found if I click right on my dialog, I have "Insert ActiveX control" pressed;found my commcontrol, all ok.
but still I would like to know if I can make my "Add to project" to appear.
thanks
marian
|
|
|
|
|
If you mean to add it to your toolbox, in VC2005 right_click the toolbox and select "Choose items"
Why is common sense not common?
Never argue with an idiot. They will drag you down to their level where they are an expert.
|
|
|
|
|
This function should count the number of occurrences of strSearch within sentence. The compiler doesn't like it when a string variable is given instead of a char for functions such as strstr, strlen...
unsigned int CSentenceList::searchInSentence(const string* sentence, const string* strSearch)
{
char buffer[MAX_SENTENCE_LENGTH + 1] = {0};
memcpy(buffer, sentence, MAX_SENTENCE_LENGTH);
char* strToken = 0;
unsigned long iWordCount = 0;
strToken = strstr(buffer, strSearch);
while (strToken !=NULL)
{
if (strlen(strToken) > 0)
{iWordCount++;
strToken += strlen(strSearch);
}
strToken = strstr(strToken, strSearch);
}
return iWordCount;
}
Errors:
cSentenceList.cpp(70): error C2664: 'strstr' : cannot convert parameter 2 from 'const std::string *' to 'const char *'
SentenceList.cpp(76): error C2664: 'strlen' : cannot convert parameter 1 from 'const std::string *' to 'const char *'
I tried (char) and (char*) but they don't work...
Within the class members of stl::string, I found the following specialization that allows the class to take advantage of library functions that manipulate objects of this type char.
char_traits<char> Class A class that is a specialization of the template class char_traits<CharType> to an element of type char.
(MSDN)
Any idea how to use them?
Is it possible to cast a string to a char, somehow?
Is it possible to use other functions to search for substrings within the string class?
Jon
p.s. sorry for the numerous q's, I'm lost...
-- modified at 12:49 Sunday 27th August, 2006
|
|
|
|
|
First thing is that I would attempt this using the STL algorithms. Can't tell you how off the top of my head though as (a) I'm away from home and (b) I haven't done any C++ for over 18 months!
Second, I would pass the strings by const reference rather than pointer
(const string& strSearch.
Third, regarding the error you need to call c_str() on the string, i.e., strSearch->c_str() or strSearch.c_str() if you had a reference to strSearch.
Kevin
|
|
|
|