Thanks, Carl
Please see my code as below:
//---------------------------------------
1: CString s;
2: int i_num=2;
3: s.Format("%d",i_num);
4: char *strHead = new char[3];
5: strHead ="XX";
6: strcat(strHead,s.GetBuffer());
7: bytesin= (DWORD)strlen(strHead);
8: WriteFile(hReadFile, strHead,bytesin,&bytesout,NULL);
9: delete strHead;
//------------------------------------
When I execute line 6 and 9, it will turn out an unprocessed exception like
the following format:
ReadDatFiles.exe in 0x10215657 (msvcr71d.dll) unprocessed exception:
0xC0000005: writing location 0x0043209e access confliction.
Why?
Thanks in advance
Joseph
>> Hi, all
>> I use the following codes to create a char array with only three
[quoted text clipped - 22 lines]
>
> -cd
www.fruitfruit.com - 18 Jul 2006 06:35 GMT
error 1: strHead ="XX";
correction: strcpy(strHead, "XX");
error 2: strcat(strHead,s.GetBuffer());
correction: char *strHead = new char[3 + 1];
please note that you need a char to store the "\0".
> Thanks, Carl
>
[quoted text clipped - 48 lines]
>>
>> -cd
Joseph Lu - 18 Jul 2006 06:50 GMT
Many thanks to u & Carl!
Joseph
> error 1: strHead ="XX";
> correction: strcpy(strHead, "XX");
[quoted text clipped - 59 lines]
>>>
>>> -cd
Carl Daniel [VC++ MVP] - 18 Jul 2006 16:18 GMT
> Thanks, Carl
>
> Please see my code as below:
in addition to other errors previously noted:
> //---------------------------------------
>
[quoted text clipped - 7 lines]
> 8: WriteFile(hReadFile, strHead,bytesin,&bytesout,NULL);
> 9: delete strHead;
delete [] strHead;
But why mix in a char array when you're already using CString?
Better yet, why not use the C++ standard string class?
-cd
Mihajlo Cvetanović - 19 Jul 2006 09:53 GMT
> 1: CString s;
> 2: int i_num=2;
[quoted text clipped - 5 lines]
> 8: WriteFile(hReadFile, strHead,bytesin,&bytesout,NULL);
> 9: delete strHead;
You want to write "XX2" to a file?
CString s;
int i_num=2;
s.Format("%s%d", "XX", i_num);
WriteFile(hReadFile, s, s.GetLength(), &bytesout, NULL);
Joseph Lu - 20 Jul 2006 01:28 GMT
Yes, thanks Mihajlo!
My new question is : when I use the following questions to write to a file,
it will replace the string already in that file, but I do want to insert a
string to that file , not just write and replace. Could any body tell me the
best solution, thanks in advance!
Joseph
"Mihajlo Cvetanoviæ" <mac@RnEeMtOsVeEt.co.yu> ????
news:OCU6tBxqGHA.1852@TK2MSFTNGP03.phx.gbl...
>> 1: CString s;
>> 2: int i_num=2;
[quoted text clipped - 12 lines]
> s.Format("%s%d", "XX", i_num);
> WriteFile(hReadFile, s, s.GetLength(), &bytesout, NULL);
Carl Daniel [VC++ MVP] - 20 Jul 2006 02:17 GMT
> Yes, thanks Mihajlo!
>
> My new question is : when I use the following questions to write to a
> file, it will replace the string already in that file, but I do want to
> insert a string to that file , not just write and replace. Could any body
> tell me the best solution, thanks in advance!
You can't insert into a file. You can only append to the end, or overwrite.
To append, you just need to seek to the end of the file before writing to it
(see SetFilePointer if you're using WriteFile to write).
If you truly need to insert other than at the end, then you need to re-write
the entire file (or at least all of it that comes after the starting point
of your insertion). Typically, editing programs (e.g. notepad) that need to
support insert/delete write an entirely new file every time you click
"Save".
-cd
Joseph Lu - 20 Jul 2006 02:28 GMT
Got it, thanks Carl!
>> Yes, thanks Mihajlo!
>>
[quoted text clipped - 16 lines]
>
> -cd