From: B.S on
I am tring to add some text in existing file but it doesn't work.I am
using this code

code:

HANDLE hfile;
DWORD din;

TCHAR *programa[256]="some text";
TCHAR KEY[2];
hfile=CreateFile("file.txt",GENERIC_WRITE,
0,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
int size=GetFileSize(hfile,0);

SetFilePointer(hfile,size,0,FILE_BEGIN);
WriteFile(hfile,programa,sizeof(programa),&din,0);

SetFilePointer(hfile,size+sizeof(programa),0,FILE_BEGIN);
WriteFile(hfile,KEY,2,&din,0);

CloseHandle(hfile);
From: Seetharam on
What do you mean by "doesn't work" ?

Here is sample code in MSDN that writes to an existing file:

http://msdn.microsoft.com/en-us/library/bb540534%28VS.85%29.aspx


-Seetharam
From: ScottMcP [MVP] on
On Oct 23, 4:58 pm, "B.S" <giobs...(a)gmail.com> wrote:
> I am tring to add some text in existing file but it doesn't work.I am
> using this code
>
> code:
>
> HANDLE hfile;
> DWORD  din;
>
> TCHAR *programa[256]="some text";

That is declared as an array of TCHAR*, not an array of char.
From: B.S on

Seetharam wrote:
> What do you mean by "doesn't work" ?
>
> Here is sample code in MSDN that writes to an existing file:
>
> http://msdn.microsoft.com/en-us/library/bb540534%28VS.85%29.aspx
>
>
> -Seetharam


I wont to write in existing file. I dont wont to creat new file.
From: r_z_aret on
On Fri, 23 Oct 2009 13:58:52 -0700 (PDT), "B.S" <giobs111(a)gmail.com>
wrote:

>I am tring to add some text in existing file but it doesn't work.I am
>using this code
>
>code:
>
>HANDLE hfile;
>DWORD din;
>
>TCHAR *programa[256]="some text";

This won't compile if UNICODE is enabled when you compile. If you
don't understand this statement than I strongly recommend avoiding
TCHAR or taking time to understand UNICODE and TCHAR. Otherwise,
you'll waste a lot of your time tracking down strange symptoms.


>TCHAR KEY[2];
> hfile=CreateFile("file.txt",GENERIC_WRITE,
>0,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);

This will also fail if UNICODE is enabled.


>int size=GetFileSize(hfile,0);
>
>SetFilePointer(hfile,size,0,FILE_BEGIN);
>WriteFile(hfile,programa,sizeof(programa),&din,0);

sizeof( programa) is _not_ what you want here. You want the number of
bytes in the string, not the size of the storage space. Especially
when you defined the array wrong (see previous post). Also, if UNICODE
is defined, number of bytes is not the same as number of characters.


>
>SetFilePointer(hfile,size+sizeof(programa),0,FILE_BEGIN);
>WriteFile(hfile,KEY,2,&din,0);
>
>CloseHandle(hfile);

-----------------------------------------
To reply to me, remove the underscores (_) from my email address (and please indicate which newsgroup and message).

Robert E. Zaret, eMVP
PenFact, Inc.
20 Park Plaza, Suite 400
Boston, MA 02116
www.penfact.com
Useful reading (be sure to read its disclaimer first):
http://catb.org/~esr/faqs/smart-questions.html