REading comma separated value from a text file(*.txt)

nyepie

New Member
Hi,

i'm a new comer in this programming, and I have aproblem.
my probelm is.
I don't know how to read comma separated value (CSV) from a text file (*.txt).

Pleas, I really need help. and I'll be glad if anyone help me.

regard from

Nyepie
 

Dan Dragut

New Member
Hi,

I hope it helps:

<PRE>
DEFINE VARIABLE cTextLine AS CHARACTER.
DEFINE VARIABLE iCounter AS INTEGER.

INPUT FROM VALUE("file.txt").

REPEAT:
/* read the whole text line... */
IMPORT UNFORMATTED cTextLine.

/* walk the line, default delimiter is "," as of ENTRY, we are so lucky :jump:... */
DO iCounter = 1 TO NUM-ENTRIES(cTextLine):
MESSAGE ENTRY(iCounter, cTextLine)
VIEW-AS ALERT-BOX INFO BUTTONS OK.
END.
END.

INPUT CLOSE.
</PRE>

Have a nice day :wavey:
Dan
 

Crittar

Member
There are also other methids you could use depending on what you want to acheive.

Could you supply a little more background as to what exactly you want to do please?
 

Crittar

Member
An alternative way of handling it would be:

define variable v1 as character no-undo.
define variable v2 as character no-undo.
.
.
.
define variable vn as character no-undo.

/*
Variable types and formats should match expected
input from the csv file.

an alternative to using variables would be to set up a
temp-table with the appropriate layout.
*/

input from csvfile.csv no-echo. /* specify path and filename */

repeat:

import delimiter ","
v1
v2
.
.
.
vn.

/*
do whatever you want with the variables here.
each loop through the repeat will read in one record
from the input file.
*/
end.

Hope this is of assistance to you.
 

nyepie

New Member
Hi Cris and Dan,

What I'm try to do is to capture user log that place on "history.txt". The log data delimited by ";" character.

so thanks for your concern. I'll try the response from all of you. and give you the report then, wheter it is work or not.

Nyepie
 

nyepie

New Member
hi Crish and Dan,

I've try the code you gave me. but it generate error message like image that I attached.

the text file that I try to use, using ";" as the delimiter for each value.
 

Attachments

  • errormsg.gif
    errormsg.gif
    6.6 KB · Views: 74

Crittar

Member
Could you publish the code you are using here (or e-mail it to me) please.

I will have a look to see if I can spot the error.
 

Yamen

New Member
Re: Nyepie

Nyepie...

Consider the following text file:

"Nyepie";16/01/2004;"PC-Nywpie";16/01/2004
"Yamen";16/01/2004;"PC-Yamen";16/01/2004

The above data structure of the file belongs to the following fields:

Name (Char) ex Nyepie
Date Logged IN (date) ex 01/01/2004
PC Name (char) ex PC-Nywpie
Date Logged out (char) ex 01/01/2004

1- Study your data file and observe the records and come up with general field names similar to the example above and make sure to determine the data variable types.
2- Follow the following code (I will use the previos example):

DEF TEMP-TABLE T-Main NO-UNDO
FIELD T-Name AS CHAR
FIELD T-DateIn AS DATE
FIELD T-PCName AS CHAR
FIELD T-DateOut AS DATE.

INPUT FROM "C:\FileName.txt".
REPEAT :
CREATE T-Main.
IMPORT DELIMITER ";" /* note that you can change the delimiter */
T-Main. /* to "," or " " etc.... */
END.
INPUT CLOSE.

3- Now All data is saved on a progress temp-table. you can now edit,add, or delete any record you want
 
Top