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!

Converting tab-delimited file to comma delimited 2

Status
Not open for further replies.

fbizzell

Programmer
Jul 3, 2000
217
Does anyone know of a way to convert a tab-delimited TXT file to a comma delimited file within a Clipper application. I can easily import a comma delimited file into a DBF file but doing that with a tab-delimited file is quite a different story. I know that it can be done but would require a lot of code. So I thought if I could get the file into a comma delimited format I can issue:

use myfile
append from mytext.txt delimited

 
Hi, fbizell

I use this function to convert the file to a temp file and then import that. Not elegant but it works.

Jock

static function tab2comm (cIfile, cOfile)
local cByte, nHdl
nHdl := fopen(cIfile)
cByte:= ' '
if nHdl<1
return (.f.)
endif
set alternate to &cOfile
set alternate on
set console off
do while fread(nHdl,@cByte,1)>0
if cByte = chr(9)
?? ','
else
?? cByte
endif
enddo
fclose(nHdl)
set alternate off
set alternate to
set console on
return (.t.)
 
Another way, if the file is fairly small, is

Code:
memowrit("c:\myfile.txt",strtran(memoread("c:\myfile.txt"),chr(9),","))

Regards

Griff
Keep [Smile]ing
 
Hi, GriffMG:
Could you please tell me how to do it? I have no idea what is memowrit or strtran.
memowrit("c:\myfile.txt",strtran(memoread("c:\myfile.txt"),chr(9),","))

Thanks
 
Hi

MemoWrit(filename,string) - writes a string to a filename
StrTran(string,find,replace) - replaces find with replace in string
MemoRead(filename) - reads filename into memory

So...
Code:
StrTran(MemoRead("c:\myfile.txt"),chr(9),",")

Reads the specified file, in this case c:\myfile.txt, and replaces any chr(9) with a comma - chr(9) is an ASCII tab character.

Code:
MemoWrit(...

Slings the resultant string out to the specified file, in this case c:\myfile.txt again

Does that help?

Martin

Regards

Griff
Keep [Smile]ing
 
Thank you very much for your help! However, I still do not know where should I input the code? Could you please give me the step by step instruction?:)
My file name is "shopcart.txt", where am I going from here?
Thank you
 
Weeeellll, you could use a UDF like this

Code:
MyString = ConvertTabs("shopcart.txt")

function ConvertTabs
  parameters myfilename
  private mystring,myfilename
  memowrit(myfilename,strtran(memoread(myfilename),chr(9),","))
return(memoread(myfilename))

Regards

Griff
Keep [Smile]ing
 
Thank you very much Griff
I am going to try it this morning:)
 
Hi Griff:
Can you tell me if I can use different file to write the code? I do not know what if UDF and do not know how to use it. It's very confusing!
Thanks
 
Here it gets a bit more difficult, knowing WHERE to put the function depends on your system, but - generally - you will probably be able to put this bit right at the end of the file in which the 'append from' code is going:

Code:
function ConvertTabs
  parameters myfilename
  private mystring,myfilename
  memowrit(myfilename,strtran(memoread(myfilename),chr(9),","))
return(memoread(myfilename))

Then if you put this bit, just before you do the 'append from' :

Code:
ConvertTabs("shopcart.txt")

You might need to be a bit more specific with the filename too - perhaps including the full path to it.

Code:
ConvertTabs("c:\temp\shopcart.txt")

Remember that clipper doesn't know about long file names.


Regards

Griff
Keep [Smile]ing
 
I hope I'm not coming too late for a simple solution. Why don't you write only:

use myfile
append from mytext.txt delimited with (chr(09))

Is there any reason not to work?

Ulisses
mbinfo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top