Solved

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

Posted on 2004-08-03
3
1,158 Views
Last Modified: 2013-12-27
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
Comment
Question by:hansw77041
  • 2
3 Comments
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 500 total points
ID: 11712053
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
 

Author Comment

by:hansw77041
ID: 11712249
Works...  Can't see why my version failed...
Thanks...
BTW

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

0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11716846
Sorry about my bug, writting from my memory without testing.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I think the most popular question in our Windows Mobile Programming zone is about the backlight - we all want to keep it on when our application is running. Few years ago the function SystemIdleTimerReset() did this job. On our side we had to detec…
Preface: This article is part of a series focused on cross platform mobile app development (specifically Android and iOS) using the Alloy framework and Titanium Studio made by Appcelerator (https://www.appcelerator.com/). This article presumes a wor…
A short film showing how OnPage and Connectwise integration works.
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now