-3

when I construct a string

string s ="";
s = rb.rep + a.chemin.substr(rep.size());
//cout << "s:" << s << endl;

if (s.empty()){
    cerr<<"c++ string is uninitialized"<<endl;
    exit(1);
}

and doing

if(is_file(s)){
    a.s="ok";
}
else{
      SEULS.push_back(ela.first);
      a.s="SEUL";
}

with this function:

bool is_file(string &path) {

the structure is defined

    struct stat buf;
    stat(path.c_str(), &buf);

you know if it is a file or not

    if(S_ISREG(buf.st_mode)) cout<<"is_file "<<path<<"  "<<S_ISREG(buf.st_mode)<<endl;
    else cout<<"NO is_file "<<path<<"  "<<S_ISREG(buf.st_mode)<<endl

another way is trying to open the file

    FILE*f=fopen(path.c_str(), "r");
    if( f == NULL){
        cerr<<"PROB fopen "<<path<<endl;
        exit (EXIT_FAILURE);
    }
    else{
        fclose(f);
    }

the result is returned

    return S_ISREG(buf.st_mode);
}

I got this result on console

is_file e/rep_A/1.txt 1
NO is_file e/2.txt 0
PROB fopen e/2.txt
Program ended with exit code: 1

but when I comment

//    FILE*f=fopen(path.c_str(), "r");
//    if( f == NULL){
//        cerr<<"PROB fopen "<<path<<endl;
//        exit (EXIT_FAILURE);
//    }
//    else{
//        fclose(f);
//    }

I got

is_file e/2.txt 1
is_file e/rep_B/3.txt 1
lesSeuls de e et de a...
is_file a/rep_A/1.txt 1
is_file a/rep_B/3.txt 1

PROBLEM:

e/2.txt doesn't exists !!

and if I uncomment

cout << "s:" << s << endl;

before calling

if(is_file(s)){
    a.s="ok";
}
else{

That's ok !

I get

NO is_file e/2.txt 0
PROB fopen e/2.txt
Program ended with exit code: 1

To result I have to print string s or printing something to get the good string s. it is like concurrent programming.

This is not a joke

Did you meet that?

Thanks

  • 1
    Something somewhere in your code has undefined behaviour. It's impossible to guess what or where. – molbdnilo Sep 09 '18 at 17:36
  • One time with `string s = rb.rep + a.chemin.substr(rep.size())` the compiler said s is uninitialized ... this is not the way or something like that. But I was told that one time so now I do `string s =""; s = rb.rep + a.chemin.substr(rep.size());` and I try `if (s.empty()){ cerr<<"c++ string is uninitialized"< – user7058377 Sep 09 '18 at 17:41
  • The non-existence of a file is indicated by `stat` failing with `ENOENT` error. Looking at `buf` is only meaningful when the call succeeds - otherwise, you are examining random garbage left on the stack by previous calls. Check the return value of `stat`. – Igor Tandetnik Sep 09 '18 at 17:47
  • waaouuh Yes ! with struct stat buf; // stat(path.c_str(), &buf); if (stat(path.c_str(), &buf) == -1) { perror("stat"); printf("File name: %s \n",path.c_str());
    exit(1); } I get the good result : stat: No such file or directory File name: e/2.txt. I have seen https://stackoverflow.com/questions/3512434/using-struct-stat Thanks !!
    – user7058377 Sep 09 '18 at 18:13

1 Answers1

0

When the file doesn't exist, stat would fail (with ENOENT error), leaving its struct stat parameter unchanged. Your program does not check the return value of stat call; even if the call fails, the program cheerfully proceeds to examine random garbage left on the stack by previous functions.

That's why the behavior of the program changes depending on what you do before the stat call - you merely observe different sets of garbage.

Igor Tandetnik
  • 50,461
  • 4
  • 56
  • 85