Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Does STRTOFILE() actually create an ASCII file? 2

Status
Not open for further replies.

Rajesh Karunakaran

Programmer
Sep 29, 2016
543
MU
Hi Team!

I have to create an ASCII file (as the person who needs the file said) with values from 5-6 columns. Each column is TAB separated. I used the STRTOFILE() function to write the values with TAB character in between to a file (without the .TXT extension). The file gets created smoothly and we can see that the values are appearing in the proper format and all columns are properly in alignment when we compare it with a sample file provided to us.

But, does the file created by STRTOFILE() conforms to the so called format 'ASCII'?

I searched and came across many write-ups but were not able to come to a conclusion!

Thanks in advance
Rajesh
 
In short: Halfways yes, as whatever strings you write in STRTOFILE commands in your source code as the 1st parameter are ANSI Codepage, that is what is written as bytes 1:1 into the file.

But there is no such thing as a UTF-8 or ASCII or Unicode file. Only Microsofts Notepad makes a codepage distinction with a so-called BOM, the first 2 or three bytes determine what Notepad uses as codepage(encoding of the file. But this is specific to notepad.exe only. If you use Notepad++ (not from MS) it'll show what BOM it detects and you can use an encoding without BOM.

STRTOFILE() allows you to create a file with a specific BOM, but you should not do so.

Whatever you write as bytes into the file is written, if you don't use string literals but CHR(x) the byte value x is written, whatever is later reading the file is getting these bytes, so no matter what Foxpro displays for CHR(128) in the ASCII table that is Ç, while in European ANSI this is the € Euro currency symbol, if you limit your output to the range CHR(0) to CHR(127) you get normal ASCII output. You get 1:1 the bytes you specify in the first parameter via a VFP string, therefore STRTOFILE in conjunction with FILETOSTR() can even copy binary files, as VFPs string type is not codepage specific either, only its displays. The string type in VFP is also a structure with Length and Bytes-Array, not like C strings 0 terminated, which makes it impossible to have CHR(0) in a C string. But in C++ like in VFP ANSI is the norm for Strings. in C# and other languages that codepage is UTF-16 by the way, and is also neither a file property not some bytes at the start of a c# source code file, it's just convention.

Using VFP in India has other defaults for ANSI than in Europe or US, so when you write some Indian characters in code this is what you also get back displayed, notepad will also show it that way as a file without a BOM is interpreted as ANSI, if you want to look at the content as ASCII you'd need to use a specific editor enabling to set display of content to ASCII.

Bye, Olaf.

Olaf Doschke Software Engineering
 
Rajesh,

You say that the person who needs the file requires it to be ASCII. In that context, it's safe to assume that your file is indeed ASCII.

Strictly speaking, the term ASCII refers to the specific character set used in a string of text. But most people use the term to mean simply any plain text as opposed to binary data. In other words, if you can read the text in a text editor, then the text is ASCII.

So, although your file might or might not technically contain ASCII text, in this case you are safe to assume that it meets your colleague's requirements.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Dear Olaf & Mike,

Olaf, thanks for that elaborated explanation. In fact, I learned many things. Your posts are always like sitting in a pin-drop silent classroom listening to those serious lectures!!! (By the way, I am serious)

Mike, as usual, damn simple and straight! May be I wrote my question looking at your answer??!!!

Now, the values/characters we write to the text files are only
Alphabets, Digits, <TAB> character, Hyphen ("-") and period (".").
So, as both of you said, the file I am creating, in all meanings whatever my colleague refers,
IS an ASCII file.

Thanks,
Rajesh
 
If you mean the 26 letter Latin alphabet, sure.

If your colleague picks on the fact notepad (and notepad++) talk of ANSI encoding, I don't even know an editor verifying strictly for ASCII, but if there are problems with single characters you can surely address that.

Aa Mike said the term ASCII also is often used to refer to plain text files and that means ANSI on Windows, UTF-8 mostly everywhere else and also UTF-8 first 128 characters are ASCII.

Bye, Olaf.


Olaf Doschke Software Engineering
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top