hxjnsq@hxjnsq-VirtualBox:~$ g++ stat.cc
stat.cc:19:11: error: ‘ostream’ does not name a type
stat.cc:49:1: error: ‘ostream’ does not name a type
stat.cc: In function ‘int main(int, char**)’:
stat.cc:100:1: error: ‘cerr’ was not declared in this scope
stat.cc:104:1: error: ‘cout’ was not declared in this scope
I don't know why?
Expand|Select|Wrap|Line Numbers
- //stat.cc
- #include<iostream>
- #include<string.h>
- #include<errno.h>
- #include<sys/types.h>
- #include<sys/stat.h>
- #include<ostream>
- class Stat : public stat
- {
- private:
- char *path;
- int fd;
- public:
- Stat(){path=0;fd=-1;}
- ~Stat();
- Stat & examine(const char *pathname);
- Stat & examine(int fd);
- int operator==(Stat &o);
- friend ostream & operator<<(ostream &out , Stat &o);
- };
- Stat::~Stat()
- {
- if(path)
- delete path;
- }
- Stat & Stat::examine(const char *pathname)
- {
- if(path)
- delete path;
- path=strdup(pathname);
- fd=-1;
- if(::stat(path,this)==-1)
- throw errno;
- return *this;
- }
- Stat & Stat::examine(int fd)
- {
- if(path)
- {
- delete path;
- path=0;
- }
- this->fd=fd;
- if(::fstat(fd,this)==-1)
- throw errno;
- return *this;
- }
- ostream & operator<<(ostream &out , Stat &o)
- {
- if(o.fd==-1&&!o.path)
- {
- out<<"No current infomation."
- return out;
- }
- if(o.path)
- cout<<"stat("<<o.path<<"){\n";
- else
- cout<<"fstat("<<o.fd<<"){\n";
- cout<<"\tst_dev=\t"<<o.st_dev<<";\n"
- <<"\tst_ino=\t"<<o.st_ino<<";\n";
- cout.setf(ios::oct,ios::basefield);
- cout<<"\tst_mode=\t"<<'0'<<o.st_mode<<";\n";
- cout.setf(ios::dec,ios::basefield);
- cout<<"\tst_nlink=\t"<<o.st_nlink<<";\n"
- <<"\tst_uid=\t"<<o.st_uid<<";\n"
- <<"\tst_gid=\t"<<o.st_gid<<";\n"
- <<"\tst_rdev=\t"<<o.st_rdev<<";\n"
- <<"\tst_atime=\t"<<o.st_atime<<";\n";
- return out;
- }
- int Stat::operator==(Stat &o)
- {
- if(fd==-1&&!path)
- throw EINVAL;
- if(o.fd==-1 && !o.path)
- throw EINVAL;
- if(o.st_dev!=st_dev || o.st_ino!=st_ino)
- return 0;
- return 1;
- }
- int main(int argc,char **argv)
- {
- int x;
- Stat t;
- Stat s;
- t.examine("./stat");
- for(x=1;x<argc;++x)
- {
- try{
- s.examine(argv[x]);
- }catch(int e)
- {
- cerr<<strerror(e)<<":stat(2) of"
- <<argv[x]<<"\n";
- continue;
- }
- cout<<s<<"\n";
- cout<<"'"<<argv[x]<<"' is"
- <<(s==t?"same":"not the same")
- <<"file as ./stat\n";
- }
- return 0;
- }