Find if a shell file exists



  • Here is my C++ file:
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    int main()
    {
    cout << "rentrez nom du shell\n";//give me the name of the shell
    string shell("");//empty string
    cin >> shell;
    string path="/home/sylvain/";
    string total=path+shell;
    total='"'+path+shell+'"';
    cout << total <<endl;// Ok jusque là
    //ifstream monFlux(total);
    ifstream monFlux("/home/sylvain/essai"); //On essaye d'ouvrir le fichier
    if(monFlux) cout << "Ce fichier existe\n";
    else cout << "fichier inexistant." << endl;
    }
    Impossible to replace (in the ifstream line) what is in the parenthesis by the string "total". What shall i do if I want that the above file works well , with a whatever shell file ? Many thanks for your answer.


  • Moderators

    Why this line:

    total='"'+path+shell+'"';
    

    ?



  • @jsulm I thought it was possible to put that inside the parenthesis of ifstream. Now i Think it is impossile. I cannot introduce introduce a string in the middle of a program line. Isnt'it ?


  • Moderators

    It does not make any sense to add " to a string containing a path - it will become invalid path! Because " is not allowed in paths.
    If you want the path to be enclosed in " if you print it out then do:

    cout << '"' << total << '"' <<endl;
    

    This one I don't understand:

    I cannot introduce introduce a string in the middle of a program line. Isnt'it ?
    

    What do you mean?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.