Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Writing to a file in Win32 application

Status
Not open for further replies.

alonex

Programmer
Jan 10, 2006
14
US
Hi,

I'm trying to write a string to a file using win32 function "WriteFile".
The file is written with partial data (4 x'c' insted of 10).
What am i doing wrong?

Thanks,
Alonex


My code:
========

const char ostream[] = "c:\\test.txt";
hFile = CreateFile(ostream,GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_READ,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORM AL,NULL);
for (int i=0;i<10;i++)
{
ip[index++]='c';
}
char *ipR=NULL;
ipPtr = (char *) malloc(index);
int j;
for (j=0;j<index;j++)
{
ipPtr[j]=ip[j];
}
ipR[index+1]='\0';
WriteFile(hFile,ipPtr,(DWORD)(sizeof(ipPtr)),&wmWritten,NULL);
CloseHandle(hFile);


========================
 
(DWORD)(sizeof(ipPtr)) - it's a size of a pointer (4 on Win32). It's not a size of data pointed to...

 
Code:
ipPtr = (char *) malloc(index);
Also, why are you using malloc() in a C++ program??
You should be using new and delete.

Also, C-style casting is discuraged in favor of C++ casts:
static_cast, dynamic_cast, const_cast & reinterpret_cast.
 
Please use the [tt][ignore]
Code:
[/ignore][/tt]
tags when posting code.

--
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top