Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Find if a shell file exists
Forum Updated to NodeBB v4.3 + New Features

Find if a shell file exists

Scheduled Pinned Locked Moved Unsolved General and Desktop
15 Posts 3 Posters 3.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Sylas

    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;
    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;
    }

    S Offline
    S Offline
    Sylas
    wrote on last edited by
    #5

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

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #6

      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?

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

      S 1 Reply Last reply
      0
      • mrjjM mrjj

        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?

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

        S Offline
        S Offline
        Sylas
        wrote on last edited by
        #7

        @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

        1 Reply Last reply
        0
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #8

          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";
          }

          S 2 Replies Last reply
          0
          • mrjjM mrjj

            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";
            }

            S Offline
            S Offline
            Sylas
            wrote on last edited by
            #9

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

            1 Reply Last reply
            0
            • mrjjM mrjj

              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";
              }

              S Offline
              S Offline
              Sylas
              wrote on last edited by
              #10

              @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.

              1 Reply Last reply
              0
              • mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #11

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

                S 2 Replies Last reply
                0
                • mrjjM mrjj

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

                  S Offline
                  S Offline
                  Sylas
                  wrote on last edited by
                  #12

                  @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.

                  1 Reply Last reply
                  0
                  • mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by
                    #13

                    @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() ) {
                    ....

                    1 Reply Last reply
                    0
                    • mrjjM mrjj

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

                      S Offline
                      S Offline
                      Sylas
                      wrote on last edited by
                      #14

                      @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

                      1 Reply Last reply
                      0
                      • mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on last edited by
                        #15

                        hi
                        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
                        http://www.cplusplus.com/reference/fstream/ifstream/is_open/

                        so it will never work.

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved