Solved

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

Posted on 2004-08-03
3
1,161 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 AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

As the title indicates, I have done this before. It chills me everytime I update the OS on my phone, (http://www.experts-exchange.com/articles/18084/Upgrading-to-Android-5-0-Lollipop.html) because one time I did this and I essentially had a bricked …
In this article we will discuss all things related to StageFright bug, the most vulnerable bug of android devices.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

770 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