Tuesday, 4 September 2012

File Handling


Text File Reading:
static void textFileRead(Args _args)
{
    TextBuffer tb = new TextBuffer();
    ;

    if(WinApi::fileExists("c:\\Sample.txt"))
    {
        tb.fromFile("c:\\Sample.txt");
        info(tb.getText());
    }
    else
    {
        info("No file exists");
    }
}

Text File Writing:


static void textFileWrite(Args _args)
{
    TextBuffer tb = new TextBuffer();
    customert ct;
    ;
    tb.appendText("Hello World!");
    tb.appendText("\nWelcome to MIT");
    tb.appendText("\nHyderabad");

    tb.toFile("c:\\Sample.txt");
}

CSV File Writing:

static void cSVFileWrite(Args _args)
{
    CommaIO commaIO;
    Container readCon;
    FileIOPermission fio;
    CustomerT ct;
    ;

    commaIO = new CommaIO("c:\\Customers.csv", "w");
    commaIO.outFieldDelimiter(",");
    commaIO.outRecordDelimiter("\r\n");

    if(commaIO)
    {
        while select ct
        {
            commaIO.write(ct.CustomerId, enum2str(ct.PayMode), ct.TransDate);
        }
    }
}

CSV File Writing

static void cSVFileRead(Args _args)
{
    CommaIO commaIO;
    Container readCon;
    FileIOPermission fio;
    CustomerT ct;
    ;
    if(!WinAPI::fileExists("c:\\Customers.csv"))
    {
        throw error("File not available");
    }

    fio = new FileIOPermission("c:\\Customers.csv", "r");
    fio.assert();

    commaIO = new CommaIO("c:\\Customers.csv", "r");
    commaIO.inFieldDelimiter(",");
    commaIO.inRecordDelimiter("\r\n");

    if(commaIO)
    {
        while(CommaIO.status() == IO_Status::Ok)
        {
            readCon = commaIO.read();
            if(conlen(readcon) > 0)
            {
                ct.CustomerId = conpeek(readcon, 1);
                ct.PayMode = conpeek(readcon, 2);
                ct.insert();
            }
        }
    }
}


XML File Writing:


static void writeXML(Args _args)
{
    XMLTextWriter xw;
    CustomerT ct;
    ;

    xw = XMLTextWriter::newFile("c:\\Customers.xml");
    xw.writeStartDocument();

    xw.writeStartElement("Details");

    while select ct
    {
        xw.writeStartElement("CustomerDetails");
        xw.writeElementString("CustomerId", ct.CustomerId);
        xw.writeElementString("Name", enum2str(ct.PayMode));
        xw.writeEndElement();
    }

    xw.writeEndElement();
    xw.writeEndDocument();
    xw.close();
}

Reading XML File


static void readingXML(Args _args)
{
    XMLDocument xmlDoc = XMLDocument::newFile("c:\\Customers.xml");
    int i, nooftags;
    ;

    nooftags = xmlDoc.getElementsByTagName("CustomerDetails").length();

    for(i=0; i<nooftags; i++)
    {
        info(strfmt("%1", xmlDoc.getElementsByTagName("CustomerId").item(i).text()));
    }
}




No comments:

Post a Comment