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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Blank line when writing data to TEXT file?

Status
Not open for further replies.

HarrisD1200

Technical User
Sep 24, 2004
106
US
Hi! I'm using DDE to pull data from a spreadsheet, and write it to a text file. This works perfectly, with only one problem. There is always a blank line at the bottom of the data I write. This throws off my program when I read the data back in. Is there an easy way to remove this blank line?

PS, this blank line appears to be created when a file is opened. The text file is deleted before I run the script.

My program is pretty much pulled straight from the DDE example on aspectscripting.com. TIA!

_____________________________________________________

Proc RunDDE
long LinkVar, SystemVar ;* Variables containing DDE Id's.

fopen 1 "C:\Files\Excel_Text.txt" Create ;* RAW MODE

if ddeinit SystemVar "excel" "system"
ddeexecute SystemVar "[APP.MINIMIZE()]" ;* Minimize Excel
if ddeinit LinkVar "excel" "Sheet1" ;* Link to Sheet 1

While Ro < 67 ;* Last Row in Excel

strfmt sZer "%s%d%s%d" sStrR Ro sStrC C0
strfmt sOne "%s%d%s%d" sStrR Ro sStrC C1
strfmt sTwo "%s%d%s%d" sStrR Ro sStrC C2
strfmt sThr "%s%d%s%d" sStrR Ro sStrC C3

;*** GET THE DATA FROM THE ROWS AND COLUMNS IN EXCEL ****

Dderequest LinkVar sZer sTok0
fputs 1 sTok0
Dderequest LinkVar sOne sTok1
fputs 1 sTok1
Dderequest Linkvar sTwo sTok2
fputs 1 sTok2
Dderequest Linkvar sThr sTok3
fputs 1 sTok3

Ro++ ;* Increment Row Number
Endwhile
fclose 1
 
The best way (in my opinion) is to test for a blank line after reading the text in. This way and blank lines are a null issue going forward.
 
Blank Lines often seem to contain NewLine (`n) or Carriage Return (`r) Characters. I often strip them out if I need the info on that line or skip/ignore it if I don't.

I read a thread ages ago where knob suggested stripping these characters by...

strreplace TextStr "`n" "" ;strip New line
strreplace TextStr "`r" "" ;strip Carriage return



 
Is it possible you are reading one line more than you think you are from the spreadsheet, resulting in blank cells being read from the spreadsheet and then written to the text file?

 
Thanks for the tips! I'm 99% certain that I'm not reading an extra cell from the spreadsheet as I don't have any blank cells. The real issue I'm having is when I run a script on the data file I create, it sees the extra line as a reason to run thru again (I'm using a while not eof statement). If I manually delete the extra line, this doesn't happen. If I can figure out what the character is, I can test and end off it, but so far, `n and `r do not match it. I will check both ways tho (reading in and stopping, or seeing if I'm writing an extra line), and let you know. Thanks all!
 
I might have confused you. On your code to read the text file in, test for a blank line, and using an if right after reading the line, bypass your code if it's a blank.

Rough psuedo code:

read sLine
if sLine <> "" then
Do something here
else
close file, continue on

If you post up some of the code from where you read the data file in, we might be able to help you a little better.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top