×

Discussion Board

Results 1 to 5 of 5
  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    40

    Writing to a file

    Code:
    void Add::saveData()
    {
        QFile file("c://WeightTracker.csv");
        if (!file.open(QIODevice::WriteOnly)) {
            //std::cerr << "Cannot open file for writing: "
                      //<< qPrintable(file.errorString()) << std::endl;
            return;
        }
    
        QTextStream out(&file);
        qint32 weight = (ui->lineEdit->text()).toInt();
        QString date_of_weight = (ui->dateEdit->date()).toString();
    
        out << date_of_weight << "," << weight << endl;
    
    
    }
    I am using the above function to write to a file (the application is a nokia mobile application). It works correctly and writes a single line to the file.
    But I have 2 problems:
    1. The data is written when a button is clicked (saveData is a slot connected to a clicked signal). But when I click the button multiple times, the first line is always overwritten (so there is always only 1 row in the file). With every button click, I want new data to be added as a new row.

    2. If I enable the following line:
    Code:
    //std::cerr << "Cannot open file for writing: "
                      //<< qPrintable(file.errorString()) << std::endl;
    I get the following error: "cerr is not a member of std"; can you please help me resolve this error?

    The project can be found here: [link removed]

    Thanks in advance for your help.
    Last edited by ShahriarNK; 2011-01-02 at 10:28.

  2. #2
    Super Contributor
    Join Date
    Nov 2009
    Location
    Minnesota, USA
    Posts
    3,209

    Re: Writing to a file

    Try
    open(QIODevice::WriteOnly | QIODevice::Append)

  3. #3
    Regular Contributor
    Join Date
    Dec 2010
    Posts
    142

    Re: Writing to a file

    Yes, open file in append mode. If you frequently need to write into file,instead of opening file every time you can keep it open untill you are done with it.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Posts
    40

    Re: Writing to a file

    Thankyou both for your solution; it works perfectly.

    Does any one have any clue regarding the second error i am facing:

    2. If I enable the following line:
    Code:
    //std::cerr << "Cannot open file for writing: "
                      //<< qPrintable(file.errorString()) << std::endl;
    I get the following error: "cerr is not a member of std"; can you please help me resolve this error?

  5. #5
    Regular Contributor
    Join Date
    Dec 2010
    Posts
    142

    Re: Writing to a file

    Did you include <iostream> header?
    you should use qt specific things like qDebug(), qWarning(), qFatal(),qCritical(), equivalent for your code is:

    qDebug() << "Cannot open file for writing: " << qPrintable(file.errorString());
    Last edited by dahiya.vinod; 2011-01-02 at 14:46.

Similar Threads

  1. Writing into file
    By Shweta.Hegde in forum Symbian
    Replies: 4
    Last Post: 2009-03-02, 06:10
  2. writing in a file
    By mayankkodesia in forum Symbian
    Replies: 7
    Last Post: 2008-10-20, 05:37
  3. writing to a file
    By gaba88 in forum Symbian
    Replies: 15
    Last Post: 2008-06-26, 06:00
  4. After writing file in binary format the file is reable
    By Rajani_sp in forum Symbian User Interface
    Replies: 6
    Last Post: 2007-11-22, 15:37
  5. File reading & writing help, wav file redaing help
    By shubhamlahoti in forum Mobile Java General
    Replies: 6
    Last Post: 2007-06-27, 09:07

Posting Permissions

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