• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1184
  • 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:

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
 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 !
  • 2
1 Solution
Jaime OlivaresSoftware ArchitectCommented:
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.
hansw77041Author Commented:
Works...  Can't see why my version failed...

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

Jaime OlivaresSoftware ArchitectCommented:
Sorry about my bug, writting from my memory without testing.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

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