Generate an ASCII file with multiple spaces

I’m trying to create an ASCII file for an external party within our application. Not a problem there. The problem is that each field has a fixed length. If a field has a lesser length, I must use spaces as filler. The problem is that no matter what I try when I have to do this, is that I end up with one space. When I use a ‘^’ as filler, it all goes well, so I know that the code is OK. My question: how can I use a space as filler in an ASCII file? I’ve tried CHR(32), ’ ’ etc., but nothing seems to work. The relevant part of my code (with ^ as space):

<cffunction name="stringOpvullen" returntype="string" output="false">
	<cfargument name="stringInput" required="yes" type="string">
	<cfargument name="totaalAantalKarakters" required="yes" type="numeric">
	<cfargument name="opvulKarakter" required="yes" type="string">
		<cfreturn "#LOCAL.stringOutput##RepeatString(arguments.opvulKarakter,(arguments.totaalAantalKarakters-len(LOCAL.stringOutput)))#">
</cffunction>

<cfset variables.spatiering = '^' />

<cfsavecontent variable="variables.compleetBestand">#StringOpvullen(listlen(request.ccp_select),8,variables.spatiering)#</cfsavecontent>

<cfheader name="Content-disposition" value="attachment;filename=#variables.bestandNaam#">
<cfcontent type="windows-1252" reset="yes">#variables.compleetBestand#<cfabort>

An easier way to test this BTW is to just do this (this also doesn’t work with spaces, only with other characters):

<cfheader name="Content-disposition" value="attachment;filename=#variables.bestandNaam#">
<cfcontent type="windows-1252" reset="yes">x#Chr(32)##Chr(32)##Chr(32)#x<cfabort>

This also leaves me with just one space in the resulting ASCII file

What is your Whitespace Management setting set to in the Admin?

1 Like

windows-1252 is not a valid content type. Using text/plain instead works for me:

<cfheader name="Content-disposition" value="attachment;filename=bob.txt">
<cfcontent type="text/plain" reset="yes">x<cfoutput>#Chr(32)##Chr(32)##Chr(32)#</cfoutput>x<cfabort>

HTH

– Denny

Thank you very much! This was the way to the solution. It was set to “SMART” management, which meant that every following whitespace would be removed. Setting this to “no whitespace management” fixed the problem immediately. Thanks again!

Thank you for your answer. You probably already had you whitespace management set to “NO”, that’s why this worked for you. For me, I first needed to change this, and then it worked. You are however right. Windows-1252 is not a valid type, only a valid character encoding value.

You can also keep the Smart Whitespace Management on and wrap your preformatted text with one of the three tags that disable it temporarily: “code”, “pre”, “textarea”

So for example, wrap your preformatted text with

<pre>    white    space  
        is    preserved     
                 here
</pre>
1 Like

I used trycf.com to test, so yeah they probably have the default setting of no whitespace management, I hadn’t thought of that :wink: Good to hear you got it working!

BTW – If the tags interfere with your HTML, you can probably (I haven’t looked in a long time) open one of those tags in an HTML comment. The Whitespace Manager doesn’t care that it’s inside a comment, so putting something like

<!-- <pre> !-->

somewhere, will preserve all of the whitespace after it (unless encountered by a closing pre tag.

The browser will of course ignore it as it is in a comment.