Re: Read and Write Files

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



For the unix vs dos format problem, I believe you need to \r\n at the end of lines in DOS files. Many editors identify files that only have \n at the end of files a unix files. Writefile doesn't fix up the line endings at all it writes the literal character.

For the ReadFile question, the TCHAR always maps to WCHAR on CE. If your files is UTF-8 or ascii, I think you want to change the TCHAR to a CHAR and use fgets instead of fgetws.

Paul Monson
Intrinsyc


Kniffel wrote:
Hi everyone

I am writing an ISAPI extension for the Windows CE Webserver.
I use "embedded Visual C++ 4.0".
So far it works, but now I try to write and read files in the ISAPI.
And I get some problems, because of Unicode and so. But I am not
familiar enough to get it to work.

WRITE A FILE:
----------------------
I have a XML file, and I transform it with a XSL to a HTML Form inside
the ISAPI, when I send the Form I write the XML file with "WriteFile"
from the WinAPI. After writing the XML file, the XML/XSL
transformation is not possible anymore! No HTML Form is showing up on
the website...

CODE:
----------
#define MAX 2048;
HANDLE hFile;
DWORD wmWritten;
char FileData[MAX] = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n
<SOMETAGS>\n </SOMETAGS>\n ";

hFile = CreateFileW(reinterpret_cast<const unsigned short *>(L"\\PATH\
\file.xml"), GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ, NULL,
OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
WriteFile(hFile,FileData,(DWORD)(sizeof(FileData)),&wmWritten,NULL);
CloseHandle(hFile);

When I open the new XML file on the desktop PC in a Texteditor, it
shows me that the file is in Unix Format, but it is UTF-8(Unicode)!
Perhaps there is a problem with the size of MAX, I define it great
enough, because I have to define the size at a moment I do not know
the size, because I append strings to FileData with "strncat". And the
strings have different length everytime I write.

Can I write so that it is not in Unix, but in DOS format?
Whats with the MAX definition, could it cause problems writing a file
with 2kb, only containg content around 1kb?

READ A FILE
--------------------
My second problem is to read files... I tryed to ways: ReadFile and
fgetws

FILE *stream;
TCHAR line[MAX_PATH];
if( (stream = fopen( "\\PATH\\test.xml", "r" )) != NULL )
{
if( fgetws( line, 300, stream ) == NULL)
printf( "fgets error\n" );
else
{
DWORD ReadArrayLen;
WCHAR ReadArrayValue;
ReadArrayLen = sizeof(&line) - 1;
pECB->WriteClient(pECB->ConnID,(LPVOID)line, &ReadArrayLen, 0);
}
fclose( stream );
}

OR with CreateFile
----------------------------
hFile = CreateFile(reinterpret_cast<const unsigned short *>(L"\\NOR
Flash\\http\\test.xml"), GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
bTest= ReadFile(hFile, dwBuffer, sizeof(DWORD)*256, &dwNumRead,NULL);
bTest=CloseHandle(hFile);

Both variants are showing some non-readable characters on the
websites!?


Somebody can help me getting the file to read and to write? Some
needful links or advises?

Thanks
Thomas

.



Relevant Pages

  • Re: (DELETE-FILE open-file-stream)
    ... > How should DELETE-FILE behave on open file streams? ... I would have thought that, on Unix and Unixoid systems, this is a great ... *not* closing the stream, this is the semantics you'll get for free. ...
    (comp.lang.lisp)
  • Re: Binary or Ascii Text?
    ... You mean, before Unix developed a uniform notation for text streams, ... because the C stream is never ... and structured files -- if not always the same terminators. ... Many of those reasons have been subsumed into much ...
    (comp.lang.c)
  • Re: Next project, C programming problem.
    ... both UNIX and Windows consider all files to be ... SAMBA, like some FTP clients, can probably be told to make ... unless it something about the COBOL Compiler. ... > program to be a Stream file and I have figured out how to do that. ...
    (comp.os.vms)
  • Re: Unable to execute shell script
    ... |> Sorry for wasting your time... ... My file was in dos format. ... converted it to unix and ... cretinous error message in the case of an interpreter problem, ...
    (comp.unix.solaris)
  • Re: Happy christmas
    ... Among the three systems, Apple, PC and Unix there are three line endings. ... Chuck said "Just open the file in text mode and wait until you detect a '\n' in the stream". ... My response to Chuck was that Hell will freeze over before a single CR in a text stream will be converted to LF for me to see. ...
    (comp.lang.c)