Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

    Unsolved Find if a shell file exists

    General and Desktop
    3
    15
    2873
    Loading More Posts
    • 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.
    • mrjj
      mrjj Lifetime Qt Champion last edited by

      @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 :)

      1 Reply Last reply Reply Quote 0
      • S
        Sylas @Sylas last edited by

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

        1 Reply Last reply Reply Quote 0
        • mrjj
          mrjj Lifetime Qt Champion last edited by 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 1 Reply Last reply Reply Quote 0
          • S
            Sylas @mrjj last edited by

            @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 Reply Quote 0
            • mrjj
              mrjj Lifetime Qt Champion last edited by

              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 Reply Quote 0
              • S
                Sylas @mrjj last edited by

                @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 Reply Quote 0
                • S
                  Sylas @mrjj last edited by

                  @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 Reply Quote 0
                  • mrjj
                    mrjj Lifetime Qt Champion last edited by

                    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 Reply Quote 0
                    • S
                      Sylas @mrjj last edited by

                      @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 Reply Quote 0
                      • mrjj
                        mrjj Lifetime Qt Champion last edited by

                        @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 Reply Quote 0
                        • S
                          Sylas @mrjj last edited by

                          @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 Reply Quote 0
                          • mrjj
                            mrjj Lifetime Qt Champion last edited by

                            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 Reply Quote 0
                            • First post
                              Last post