Dec 3, 2013

How to Write/ Read the Comma seperated values to/from a text file ?

The following sample code gives idea that how to write or read the values to a text file

static void testWriteAndRead(Args _args)
{
CommaIo io;
container con;
FileIoPermission perm;
int i;
;
#define.MyFile(@"c:\MyFileName.txt")
#define.MyFileWrite("w")
#define.MyFileRead("r")

perm = new FileIoPermission(#MyFile, #MyFileWrite);
if (perm == null)
{
return;
}
// Grants permission to execute the CommaIo.new method.
// CommaIo.new runs under code access security.
perm.assert();

// Write
io = new CommaIo(#MyFile, #MyFileWrite);

con = [1,"MyText",1.324,"Last field"]; // Assign the entries in the container according to record layout.
io.writeExp(con); // write this record according to file format (record/field delimiters).
// End Write

//Read
io = new CommaIo(#MyFile, #MyFileRead);
if (io != null)
{
con = io.read();
for(i=1;i<=conLen(con);i++)
{
info(strFmt("%1",conpeek(con,i)));
}
}
//End Read
// Close the code access permission scope.
CodeAccessPermission::revertAssert();

}

No comments: