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;
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;
} -
Why do you post same question again?
One is enough: http://forum.qt.io/topic/65004/find-if-a-shell-file-exists -
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? -
@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 -
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";
} -
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. -
@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 -
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.