×

Discussion Board

Results 1 to 7 of 7

Thread: file operations

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    46

    file operations

    Hi,

    I am trying sample File read and write operations.My code is able to create a file and writing some text into it.but reading is not working properly.my code is as follows...



    #include "FileEx.h"
    #include <QFile>
    #include <QLabel>
    #include <QDataStream>
    #include<QTextStream>
    FileEx::FileEx(QWidget *parent)
    : QWidget(parent)
    {
    QFile file("c://test.txt");
    file.open(QIODevice::WriteOnly);
    QDataStream out(&file); // we will serialize the data into the file
    out << "welcome to the Qt World"; // serialize a string
    file.close();
    QFile read("c://test.txt");
    read.open(QIODevice::ReadOnly | QIODevice::Text);
    //QDataStream in(&read);
    // read the data serialized from the file

    QTextStream in(&read);
    QString line = QString(in.readLine());
    QLabel *label = new QLabel(line);
    //label->setText(line);
    label->show();



    /*QString str;
    in >> str;
    QLabel *label = new QLabel;
    label->setText(str);
    label->show(); */
    }

    FileEx::~FileEx()
    {

    }

  2. #2
    Super Contributor
    Join Date
    Oct 2009
    Posts
    4,326

    Re: file operations

    Try this test application:
    Code:
    #include <QtGui>
    
    int main (int argc, char **argv)
    {
        QCoreApplication app(argc, argv);
    
        QFile file("test.txt");
        if(!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
           qDebug("IO error");
           return 1;
        }
        QTextStream out(&file);
        out << "welcome to the Qt World";
        file.close();
    
        QFile read("test.txt");
        if(!read.open(QIODevice::ReadOnly | QIODevice::Text)) {
           qDebug("IO error");
           return 2;
        }
        QTextStream in(&read);
        QString line = in.readLine();
        qDebug() << line;
        read.close();
    }

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    46

    Re: file operations

    thaks for the reply....

    I tried with u r code...but nothing appears on the screen.

  4. #4
    Super Contributor
    Join Date
    Oct 2009
    Posts
    4,326

    Re: file operations

    Quote Originally Posted by prathibha83 View Post
    thaks for the reply....

    I tried with u r code...but nothing appears on the screen.
    Because it's a console application.

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    46

    Re: file operations

    Thanks....its working fine....

  6. #6
    Registered User
    Join Date
    Feb 2010
    Posts
    46

    Re: file operations

    if I want to see the o/p on the emulator how can I do it....
    please help me.

  7. #7
    Super Contributor
    Join Date
    Oct 2009
    Posts
    4,326

    Re: file operations

    Code:
    #include <QtGui>
    
    int main (int argc, char **argv)
    {
        QApplication app(argc, argv);
    
        QFile file("test.txt");
        if(!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
           qDebug("IO error");
           return 1;
        }
        QTextStream out(&file);
        out << "welcome to the Qt World";
        file.close();
    
        QFile read("test.txt");
        if(!read.open(QIODevice::ReadOnly | QIODevice::Text)) {
           qDebug("IO error");
           return 2;
        }
        QTextStream in(&read);
        QString line = in.readLine();
        read.close();
    
        QLabel label;
        label.setText(line);
        label.setAlignment(Qt::AlignCenter);
        label.showMaximized();
    
        return app.exec();
    }

Similar Threads

  1. File operations
    By schrecklichkeit in forum Symbian
    Replies: 6
    Last Post: 2009-11-16, 07:07
  2. Getting around midlet signing for JSR-75.
    By Timmmm3d in forum Mobile Java Networking & Messaging & Security
    Replies: 3
    Last Post: 2008-12-14, 23:19
  3. Help needed with a 3gp file...
    By joedoe_1981 in forum Streaming and Video
    Replies: 0
    Last Post: 2007-07-18, 18:58

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×