Find if a shell file exists

  • The program below works very well for the file designed in the path. I should like that it works with the file I ask with a cin.
    Many thanks, Here is the 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;
    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;

  • Moderators

    Why do you post same question again?
    One is enough:

  • @jsulm I lost my post. I don't know why. My answer was: I don't think it is possible to introduce a string in the middle of a line of a program. Indeed the compiller sends me "Error".

  • Qt Champions 2017

    @Sylas said:

    introduce a string in the middle of a line of a program

    Hi its not really clear to me what u mean by that :)

  • @Sylas Look please at the commented line "ifstream". Uncomment that and you'll see computer return ERROR

  • Qt Champions 2017

    but what was the error`?
    The compiler actually tells you what is wrong.
    So to be a programmer, u MUST care for what it says. :)

    I think it just a case of NOT right type?
    ifstream monFlux(total);

    here you gives it a std::string
    total is a std::string, yes?
    does it expect that?

    ifstream monFlux(total.c_str());
    will make it more happy?

  • @mrjj It runs well but the if statement is always "inexistant file". The compiler says: Checking for existance: /home/sylvain/ESSAI/bin/Release/ESSAI.
    ESSAI is the name of the project (Code:Blocks), but the name of the file is essai.
    essai really exists as a shell file. Thanks again, because now we go forth

  • Qt Champions 2017

    and the file is there
    "/home/sylvain/essai" ?

    im not sure your code can actually check if file exits since you just create the
    stream object but dont really use it.

    maybe try
    ifstream a_file ( "/home/sylvain/essai" );

    if ( !a_file.is_open() ) {
    cout << "fichier inexistant." << endl;
    else {
    cout << "Ce fichier existe\n";

  • @mrjj I tried your little program. It runs but, unfortunately, gives always the same answer: "ce fichier existe", even if it is not true.

  • @mrjj Excuse me. Your litte program works very well. I did the mistake to not replace essai by an other file (inexistent), inside your little program.

  • Qt Champions 2017

    ok that sounds good. :)
    a_file.is_open() should only be true when it can actually open the file. (it exists)

  • @mrjj I tried that: ifstream a_file("/home/sylvain/" <<shell<<'"'); The compiler says: error no match for 'oprator <<' (operand types are 'const char [15] and 'std::string {aka std::basic_string<char>}')
    As I am a beginner that is chinese for me, but for you it is different.

  • Qt Champions 2017

    @Sylas said:

    a_file("/home/sylvain/" <<shell<<'"');

    this syntax is not valid.
    you cannot use << this way.

    you would do something like

    string filename;
    cin >> filename;
    string path="/home/sylvain/";
    string fullpath=path+filename;
    fstream a_file (fullpath );
    if ( !a_file.is_open() ) {

  • @mrjj May be the solution is much more simple. I tried that:
    cout << "rentrez nom du fichier\n";
    string a_file;
    cin >> a_file;

    if ( !a_file.is_open() ) {
    cout << "fichier inexistant." << endl;
    else {
    cout << "Ce fichier existe\n";
    My new error is: 'std::string' has no member named 'is_open'
    What do you think of that ? ? Many thanks

  • Qt Champions 2017

    The open function belong to fstream class
    and in this case the compiler is very clear :)

    'std::string' has no member named 'is_open'

    Which means that
    string a_file; <---- its a string
    do not have such function.

    fstream has it

    so it will never work.

Log in to reply

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