0

I created a C++ console application with QT creator 4.1.0 This application was originally created wiht Code Blocks and works fine.

this is the code

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
string nomeFile="";

cout << "\nNome file: ";
    cin >> nomeFile;                                // inserire nome file  ROF.txt da leggere

//ifstream leggiROF(nomeFile.c_str());              // apre in lettura il file ROF.txt
ifstream leggiROF("C:\\Users\\Massimo Di Natale\\Documents\\Programmi C++ 11\\Programmi_QT\\prova\\a.txt");

string riga="";
if(leggiROF)                                        // se lo trova
{
    while(!leggiROF.eof())                          // finché non raggiunge la fine del file
    {
        getline(leggiROF, riga);                    // legge la riga
        cout << "Riga: " << riga << endl;
    } // end while
} // end if

else                                                // altrimenti
{
    cout << "\nIl nome file inserito non e' stato trovato!!!" << endl;
    cout << "Controllare che sia stato inserito nella cartella corretta.\n" << endl;

    exit (EXIT_SUCCESS);                            // termina il programma
} // end else


leggiROF.close();
return 0;
}

If i specify the file name with its path on ifstream it work. If i want to insert the file name by cin and then use ifstream filenam.c_str() it don't find the file. I want to read the .txt file from the directory were the project or the .exe are

Massimo D. N.
  • 316
  • 1
  • 6
  • 17

1 Answers1

0

Try getline(cin,nomeFile); instead of cin >> nomeFile. Answers at "std::cin.getline( ) vs. std::cin" question describes the difference between them.

Community
  • 1
  • 1
Nikita
  • 6,270
  • 2
  • 24
  • 37
  • ok, it works but only including the path (C:\\Users\\Massimo Di Natale\\Documents\\Programmi C++ 11\\Programmi_QT\\prova\\a.txt), while in code blocks i only insert the file name (a.txt) at the request – Massimo D. N. Oct 05 '16 at 16:49
  • @MassimoD.N. The difference can be caused by working folder configuration. *"a.txt"* file path means that during execution `ifstream leggiROF("a.txt")` tries to search file in the working folder. Probably in Code Blocks you have special configuration for it or *"a.txt"* lays near executable. – Nikita Oct 05 '16 at 17:09
  • I have my projects under "Documents" folder. Code Blocks projects under "Documents\Programmi C++ 11\Programmi_OOP\Project name" and there, there is also my a.txt file. The generated .exe is under "Documents\Programmi C++ 11\Programmi_OOP\Ericsson v2_1\bin\Debug". QT Creator projects are under "Documents\Programmi C++ 11\Programmi_QT\Project name" and there, there is also my a.txt file. The generated .exe is under "Documents\Programmi C++ 11\Programmi_QT\build-prova-Desktop_Qt_5_7_0_MinGW_32bit-Debug\debug". – Massimo D. N. Oct 06 '16 at 07:26
  • @MassimoD.N. As I see in documentation, default Working directory for [Code Blocks](http://wiki.codeblocks.org/index.php/Project_file#Working_directory) is project directory. [Qt Creator](http://doc.qt.io/qtcreator/creator-run-settings.html) has the other default option for Working directory, it's the directory of the build result. Because of this in Code Blocks configuration *"a.txt"* found by relative path and in Qt Creator config it's not found. Check links in this comment for more details. – Nikita Oct 06 '16 at 07:55
  • Thanks for your help. Sorry but my english its not so good so i don't understand what to do reading Qt Creator docs. Do i have to use something like this? QDir::setSearchPaths("prova", QStringList("C:\\Users\\Massimo Di Natale\\Documents\\Programmi C++ 11\\Programmi_QT\\prova")); ....it don't work – Massimo D. N. Oct 06 '16 at 09:16
  • @MassimoD.N. Nope, it's not what you are looking for. [`QDir::setSearchPaths`](http://doc.qt.io/qt-4.8/qdir.html#setSearchPaths) give you ability to create `prefix` for directory, e.g. you will be able to open `QFile file("prova:a.txt");` – Nikita Oct 06 '16 at 16:09
  • @MassimoD.N. Check ["Specifying Run Settings"](http://doc.qt.io/qtcreator/images/qtcreator-pprunsettings.png) picture. Set "Working directory" to `C:\Users\Massimo Di Natale\Documents\Programmi C++ 11\Programmi_QT\prova` in your run settings. – Nikita Oct 06 '16 at 16:12
  • I can't find any place to do that. Only under Tool>Options i can set something but not that. Also, i wold like to read the .txt file from the directory were the project or the .exe files are (this project and/or others, that can have a different directory so i can't set a fix directory). It must read from current directory – Massimo D. N. Oct 06 '16 at 17:10