Results 1 to 3 of 3

Thread: fstream not working

  1. #1
    Join Date
    Mar 2019
    Posts
    22
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows

    Question fstream not working

    Hello
    So i'm trying to read from a .txt file when the application is closed or when the user presses the X button on the top right corner.

    I tried reading from a file that is already created, but for some reason it's not opening the file, it always prints out "doesn't exist".

    Qt Code:
    1. void Game::closeEvent(QCloseEvent *event)
    2. {
    3. QString item;
    4. /*
    5.   QFile inputFile("Save.txt");
    6.   if (inputFile.open(QIODevice::ReadOnly))
    7.   {
    8.   QTextStream in(&inputFile);
    9.   while (!in.atEnd())
    10.   {
    11.   item = in.readLine();
    12.   qDebug() << item;
    13.   }
    14.   inputFile.close();
    15.   }
    16. */
    17. fstream myFile("Save.txt", ios::in);
    18. string line;
    19.  
    20. //myFile.open("Save.txt");
    21. if(myFile.is_open()){
    22. while(getline(myFile,line)){
    23. qDebug() << QString::fromStdString(line);
    24. }
    25.  
    26. }else{
    27. qDebug() << "doesnt exist";
    28. }
    29. myFile.close();
    30. qDebug() << "closed";
    31. }
    To copy to clipboard, switch view to plain text mode 

    I included <fstream>
    and added using namespace std;
    The file exists in the file and it contains 3 lines and each one of them has a string
    And i tried both examples, using fstream and using QFile ( none of them work )

    If anyone knows what could be the problem please help me out !

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,491
    Thanks
    252
    Thanked 717 Times in 707 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: fstream not working

    Your fstream declaration in line 17 assumes that the file is in your current working directory. You might be surprised to learn that the working directory may not be what you think it is. Add a call to the static method QDir::currentPath() and see if the return QString contains what you think it should.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. The following user says thank you to d_stranz for this useful post:

    MongKong (10th March 2020)

  4. #3
    Join Date
    Mar 2019
    Posts
    22
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: fstream not working

    OOOOhhhh i see, i got it working so far i think, thank you very much !!!

Similar Threads

  1. Reading Binary Files From Asset folder using "fstream"
    By saad_saadi in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 24th June 2015, 09:20
  2. Use fstream with a QDialog
    By gtnoob in forum General Programming
    Replies: 2
    Last Post: 19th October 2012, 19:57
  3. QFile -> std::fstream
    By bhs-ittech in forum Qt Programming
    Replies: 1
    Last Post: 9th June 2008, 10:02
  4. fstream open/close
    By mickey in forum General Programming
    Replies: 4
    Last Post: 13th October 2006, 12:23
  5. fstream -> stdout
    By zlatko in forum General Programming
    Replies: 11
    Last Post: 25th January 2006, 05:15

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.