|
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
|
|
|
|
|
You are going to have to stop passing these strings around like they are normal strings, they are NOT!! They are classes and come with their own methods of accessing the data within them. You should also look at your error messages before you start asking for help. Look:
cSentenceList.cpp(70): error C2664: 'strstr' : cannot convert parameter 2 from 'const std::string *' to 'const char *'
The important words here are cannot convert parameter 2 from. It also gives you the line number (70), so you should go to that line and look at the second paramater to that function. The error then goes on to tell you more from 'const std::string *' to 'const char *'. The function wants a char * but you gave it a std::string* . Like I said the std::string has methods to access the data, and like I told you yesterday, use the .c_str() to get a const char* .
|
|
|
|
|
Why not just use CString from MFC? I think it contains the proper overloads to address that situation. However, I would listen to the poster "Waldermot". I think what he recommends would enable you to understand some important principles that you can successfully apply later.
|
|
|
|
|
I've got nothing against CString but all things being equal you're better off using std::string . For one it means you've got less porting to do if you decide to move the application to another platform.
Steve
|
|
|
|
|
|
I would like to include a validation that excludes carriage returns, on the lines of,
...
{ fileToRead.getline(strLine, MAX_SENTENCE_LENGTH);
if (strLine != CRLF)
{Sentences.push_back(strLine);
Size++;}
}
...
How do I validate strLine, perhaps through the ASCII code?
Jon
|
|
|
|