|
Hi all,
I've create a char type buffer as follows.
char tempBuffer[256] ;
Then I use some text to a file using that buffer as follows.
openFile.Write(readString, sizeof(tempBufferWrite)) ;
Now my question is this. Say I'm write something to the file which is the length is less than 256 characters. File contains the text I wrote to it and after that filled up with annoying characters until reach to 256 total characters. I want to avoid such annoying characters.
So, is there any way to initialize the buffer with empty characters, or the size define the length of the string(text I have write to the file)
Thanks
Eranga
I appreciate your help all the time...
|
|
|
|
|
char tempBuffer[256] = <code>{0}</code>;
|
|
|
|
|
Yep I have try it, but no luck. Initially I thought that letter '0' initialize in all 256 memory locations. And also I've tried this.
char tempBufferWrite[256] = {} ;
Even though print some annoying text.
For more explanation see this code,
<br />
void CSRFToolDlg::OnBnClickedWrite()<br />
{<br />
char tempBufferWrite[256] = {0} ;
openFile.Write("text of letters", sizeof(tempBufferWrite)) ;<br />
openFile.Flush() ;<br />
}<br />
Eranga
I appreciate your help all the time...
|
|
|
|
|
try strlen (tempBufferWrite) inside your write function.
Suggestion to the members:
prefix your main thread subject with [SOLVED] if it is solved.
chandu.
|
|
|
|
|
You mean set the buffer size to length of the string(text)?
Eranga
I appreciate your help all the time...
|
|
|
|
|
mmm,
you are writing "text of letters" (16 chars including '\0'), but you have actually 256 bytes in the buffer and is not a pointer so the size is fix to you init value. That means the rest is going to be written with waste values.
Try:
void CSRFToolDlg::OnBnClickedWrite()
{
char* tempBufferWrite = NULL;
tempBufferWrite = new char[256];
tempBufferWrite = "text of letters";
openFile.Write(tempBufferWrite , sizeof(tempBufferWrite)) ;
openFile.Flush() ;
}
Greetings.
--------
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
|
|
|
|
|
Seems it works. Thanks.
Now I'm going to slightly change it with dynamic text, like text from control box. I come back here if I got any issue there.
Eranga
I appreciate your help all the time...
|
|
|
|
|
Nelek wrote: tempBufferWrite = new char[256];tempBufferWrite = "text of letters"; //or something longer, it should be the same
This is for what?
- NS -
|
|
|
|
|
mmm, I just made some changes but leaving his point of view.
Anyways, if it is not correct, or it is useless... instead of just ask "this is for what?" would be better to say, "this is not right, you should use...", so I would learn as the same time I try to help
Greetings.
--------
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
|
|
|
|
|
Eranga Thennakoon wrote: void CSRFToolDlg::OnBnClickedWrite()
{
char tempBufferWrite[256] = {0} ; // or {}
openFile.Write("text of letters", sizeof(tempBufferWrite)) ;
openFile.Flush() ;
}
instead of
sizeof(tempBufferWrite)
there you put strlen(tempBufferWrite)
thats it.
Suggestion to the members:
prefix your main thread subject with [SOLVED] if it is solved.
chandu.
|
|
|
|
|
chandu004 wrote: there you put strlen(tempBufferWrite)
That would equate to 0 , which is obviously wrong. The second argument to Write() should be the number of bytes in the first argument.
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
the length of hte string should obviously be the number of bytes in that string right??!
Suggestion to the members:
prefix your main thread subject with [SOLVED] if it is solved.
chandu.
|
|
|
|
|
How you choose to say it is up to you, but the second argument to Write() must be the number of bytes in the first argument.
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
please tell me what is the first argument, here and what is its size.
iam very sure that you may be right, but please let me know what is that wrong i have done.
thank you.
Suggestion to the members:
prefix your main thread subject with [SOLVED] if it is solved.
chandu.
|
|
|
|
|
chandu004 wrote: please tell me what is the first argument...
In your example here, the first argument is the string literal "text of letters".
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
yah yah
i got it by seeing the nelek's post.
thank you very much(for both of you) for correcting me.
Suggestion to the members:
prefix your main thread subject with [SOLVED] if it is solved.
chandu.
|
|
|
|
|
In this case won't work, because the tempBufferWrite is 256 chars long, but the string that is being written in the file is "text of letters" (16 chars), and note that there is no string to have length of. The code gives the "..." directly, not going through the buffer.
char tempBufferWrite[256] = "Test of letters" ;
openFile.Write(&tempBufferWrite, strlen(tempBufferWrite)) ;
That should work. What you proposed just changing sizeof with strlength must not.
Greetings.
--------
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
|
|
|
|
|
thanks for the analysis,
Nelek wrote: char tempBufferWrite[256] = "Test of letters" ; // or {}openFile.Write(&tempBufferWrite, strlen(tempBufferWrite)) ;
That should work
i was thinking that, &tempBufferWrite is used in the write,
i didnt observe some other readstring in that place.
Suggestion to the members:
prefix your main thread subject with [SOLVED] if it is solved.
chandu.
|
|
|
|
|
Thanks,
Than mean strlen get the length of CString.
Eranga
I appreciate your help all the time...
|
|
|
|
|
anyway, just a guess: i see you're coding in C++, so why not using std::string or even CString (if using MFC) ... ?
|
|
|
|
|
Actually that readString defines as CString in my code.
Eranga
I appreciate your help all the time...
|
|
|
|
|
openFile.Write(tempBuffer, amount of data you wrote to tempBuffer); Don't pass the whole buffer length when you didn't fill that much.
DoEvents : Generating unexpected recursion since 1991
|
|
|
|
|
Actually that what I've worried about. Couldn't find any way to define the buffer length as the length of string. Actually I tried something like this.
readString.GetLength()
Eranga
I appreciate your help all the time...
|
|
|
|
|
how can i install msdn library in vc++ 6.0 ??? I'm getting a message as "Please reinstall msdn" when i click on help.....
|
|
|
|
|
Can you run MSDN individually. At the installation you have a option to combine both MSDN and Visual C++ together. If you neglect that option you have to work with them separately.
Eranga
I appreciate your help all the time...
|
|
|
|