Function (关闭)状态:始终检查函数的返回
当我构造一个字符串时Function (关闭)状态:始终检查函数的返回,function,return,verification,stat,Function,Return,Verification,Stat,当我构造一个字符串时 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); } 使用此功能: bool is_file(string &path) { 结构已
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);
}
使用此功能:
bool is_file(string &path) {
结构已定义
struct stat buf;
stat(path.c_str(), &buf);
你知道它是不是一个文件
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
我在控制台上得到了这个结果
文件是否为e/rep\u A/1.txt 1
否是_文件e/2.txt 0
PROB fopen e/2.txt
程序以退出代码结束:1
但是当我评论的时候
// FILE*f=fopen(path.c_str(), "r");
// if( f == NULL){
// cerr<<"PROB fopen "<<path<<endl;
// exit (EXIT_FAILURE);
// }
// else{
// fclose(f);
// }
没关系
我明白了
否是_文件e/2.txt 0
PROB fopen e/2.txt
程序以退出代码结束:1
为了得到好的字符串,我必须打印字符串s或打印一些东西。这就像并发编程
这不是玩笑
你见过那个吗
谢谢当文件不存在时,stat将失败并出现enoint错误,其struct stat参数保持不变。您的程序没有检查stat调用的返回值;即使调用失败,程序也会兴高采烈地继续检查以前函数遗留在堆栈上的随机垃圾
这就是为什么程序的行为会根据您在stat调用之前所做的操作而发生变化——您只是观察到不同的垃圾集。代码中的某些地方有未定义的行为。不可能猜出是什么或在哪里。有一次,当字符串s=rb.rep+a.chemin.substrep.size时,编译器说s未初始化。。。不是这样的。但是有一次我被告知,所以现在我做字符串s=;s=rb.rep+a.chemin.substrrep.size;如果s是空的,我会试试{cerr文件不存在由stat failing with enoint error表示。仅当调用成功时,查看buf才有意义-否则,您将检查以前调用遗留在堆栈上的随机垃圾。使用struct stat buf;//statpath.c_str,&buf;if statpath.c_s检查stat.waaouuuuuuuh Yes!的返回值tr,&buf==-1{perrorstat;printfile name:%s\n,path.c_str;exit1;}我得到了一个好结果:stat:没有这样的文件或目录文件名:e/2.txt。我已经看到了,谢谢!!
FILE*f=fopen(path.c_str(), "r");
if( f == NULL){
cerr<<"PROB fopen "<<path<<endl;
exit (EXIT_FAILURE);
}
else{
fclose(f);
}
return S_ISREG(buf.st_mode);
}
// FILE*f=fopen(path.c_str(), "r");
// if( f == NULL){
// cerr<<"PROB fopen "<<path<<endl;
// exit (EXIT_FAILURE);
// }
// else{
// fclose(f);
// }
cout << "s:" << s << endl;
if(is_file(s)){
a.s="ok";
}
else{