Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1181
  • Last Modified:

How to write a file with normal ascii characters using WriteFile()

I posted question about how to add a cr/lf to a string written by WriteFile();
None of proposed solutions worked.

Ultimately the file will be read on a regular DOS system and needs to be in ASCII not unicode.
the strings are simple time values:
00:00:01
02:03:03
03:45:02

At the moment the end result is unicode.
How to write a file with normal ASCII characters using WriteFile().

Here's the code I'm using at the moment
 DWORD BW=0;
 CString T;
 T = _T("00:00:00\n");

 BOOL Ret = WriteFile ( hFile, T,  T.GetLength() * sizeof(TCHAR), &BW, NULL);

The file ends up with unicode characters, and no \n...
 
Why is something so simple so difficult !
0
hansw77041
Asked:
hansw77041
  • 2
1 Solution
 
Jaime OlivaresCommented:
You can write a function WriteAsciiText(), something like this

BOOL WriteAsciiText(CString &str, HANDLE hFile)
{
    int i, len=str.GetLength();
    DWORD BW=0;
 
    char buffer = new char[len];
    for (i=0; i<len; i++)
         buffer[i] = (char)str.GetAt(i);

    BOOL ret = WriteFile(hFile, buffer, len, &BW, NULL);
    delete buffer;
    return ret;
}

Hope there are not errors.
0
 
hansw77041Author Commented:
Works...  Can't see why my version failed...
Thanks...
BTW

should be  char buffer* = new char[len];

0
 
Jaime OlivaresCommented:
Sorry about my bug, writting from my memory without testing.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now