Solved

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

Posted on 2004-08-03
3
1,156 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

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…
I recently asked a question (http://www.experts-exchange.com/Programming/Smartphones/Android/Q_28684946.html) about Computer Inventory applications for Mobile Devices.  I was specifically interested in an app I could use on my android phone.  The be…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

706 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

20 Experts available now in Live!

Get 1:1 Help Now