Hi,

I am trying to port my program TunePal:

http://www.bryanduggan.com/TunePal.html

To Windows Mobile Pocket PC to run on my Palm Treo 500v smartphone. I
cant get this simple file writing code to work:

void testFile()
{
FILE * fp = NULL;

fp = fopen("\\temp\\test.txt", "w");
int r = fprintf(fp, "Hello world!!\n");
fclose(fp);


HANDLE file;
char * msg = "Hello world!";
CreateFile(L"\\temp\\test1.txt", GENERIC_WRITE, 0, 0, CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL, file);
WriteFile(file, msg, strlen(msg), 0, 0);
CloseHandle(file);
exit(0);
}

In both cases, the files are created correctly, but there is nothing
in the contents (the length of the files is 0 bytes. Also the value of
r is 0 after the call to fprintf. I have tried adding fflush before
closing the files and writing files to various locations (internal
memory and an SD card inserted in the device) In all cases, the files
are created, but there is no contents. Ive also had problems with this
program:

http://classic.pocketgear.com/software_detail.asp?id=26010&associateid=9

I dont know if its related, but it wont write a file copy of my sim
card.

Is there some strange problem writing files on this device or am I
going mad!

Thanks,

Bryan

Re: Cant write to a file!!! by Chris

Chris
Wed May 07 10:57:38 CDT 2008

Your use of CreateFile is wrong. It returns a handle to a file, and that
handle is used for subsequent calls to ReadFile and WriteFile. You've
passed it in as the "template file" which is ignored.

http://msdn.microsoft.com/en-us/library/ms923949.aspx


--

Chris Tacke, Embedded MVP
OpenNETCF Consulting
Giving back to the embedded community
http://community.OpenNETCF.com



"skooter500" <skooter500@gmail.com> wrote in message
news:4ca19654-a417-4354-a982-5ddeae402743@27g2000hsf.googlegroups.com...
> Hi,
>
> I am trying to port my program TunePal:
>
> http://www.bryanduggan.com/TunePal.html
>
> To Windows Mobile Pocket PC to run on my Palm Treo 500v smartphone. I
> cant get this simple file writing code to work:
>
> void testFile()
> {
> FILE * fp = NULL;
>
> fp = fopen("\\temp\\test.txt", "w");
> int r = fprintf(fp, "Hello world!!\n");
> fclose(fp);
>
>
> HANDLE file;
> char * msg = "Hello world!";
> CreateFile(L"\\temp\\test1.txt", GENERIC_WRITE, 0, 0, CREATE_ALWAYS,
> FILE_ATTRIBUTE_NORMAL, file);
> WriteFile(file, msg, strlen(msg), 0, 0);
> CloseHandle(file);
> exit(0);
> }
>
> In both cases, the files are created correctly, but there is nothing
> in the contents (the length of the files is 0 bytes. Also the value of
> r is 0 after the call to fprintf. I have tried adding fflush before
> closing the files and writing files to various locations (internal
> memory and an SD card inserted in the device) In all cases, the files
> are created, but there is no contents. Ive also had problems with this
> program:
>
> http://classic.pocketgear.com/software_detail.asp?id=26010&associateid=9
>
> I dont know if its related, but it wont write a file copy of my sim
> card.
>
> Is there some strange problem writing files on this device or am I
> going mad!
>
> Thanks,
>
> Bryan



Re: Cant write to a file!!! by Scott

Scott
Wed May 07 13:55:44 CDT 2008

skooter500 <skooter500@gmail.com> wrote:
>
> WriteFile(file, msg, strlen(msg), 0, 0);

As Chris pointed out, you need to pass the return from CreateFile
instead of the uninitialized value you pass in for the first parameter.
Additionally, the fourth parameter, lpNumberOfBytesWritten, can not be
NULL.

--
--------- Scott Seligman <scott at <firstname> and michelle dot net> ---------
Circular logic will only make you dizzy, Doctor.
-- Peri in Doctor Who:"The Two Doctors"

Re: Cant write to a file!!! by r_z_aret

r_z_aret
Thu May 08 14:02:37 CDT 2008

On 7 May 2008 11:55:44 -0700, "Scott Seligman" <seligman@example.com>
wrote:

>skooter500 <skooter500@gmail.com> wrote:
>>
>> WriteFile(file, msg, strlen(msg), 0, 0);
>
>As Chris pointed out, you need to pass the return from CreateFile
>instead of the uninitialized value you pass in for the first parameter.
>Additionally, the fourth parameter, lpNumberOfBytesWritten, can not be
>NULL.

More specifically, the fourth parameter must be the address of a
variable that will _receive_ the number of bytes actually written.


{
HANDLE file = CreateFile(L"\\temp\\test1.txt", GENERIC_WRITE,
0, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, file);
char * msg = "Hello world!";
DWORD dWant = strlen( msg ),
dGot;
ASSERT( file != 0 );
if (!WriteFile(file, msg, dWant, &dGot, 0))
{
DWORD de = GetLastError();
}
ASSERT( dWant == dGot );
CloseHandle(file);
exit(0);
}
-----------------------------------------
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 478
Boston, MA 02116
www.penfact.com