yunchow 发表于 2013-2-5 01:51:20

UC++之目录和文件操作

* UC
+ 文件系统
- 目录操作
- 文件操作
+ 多进程
- 进程间关系
- 进程间通信
+ 网络通信
- 基础知识
- 实践(tcp/udp)

QT,KDE: Linux环境下的图形环境编程

=================================================================
* 环境变量:

+ 如何在程序中取得环境变量的内容?

//取得所有的环境变量- #include <iostream>using namespace std;int main(int argc, char* argv[], char* env[]){      for( int i=0; env; i++ ){                if(i<9) cout << 0;                cout<< i+1 <<":   " << env << endl;                if(i%8==7) cin.get();      }      cout << endl;}
在进程中设置的环境变量只在本进程与子进程里使用
因此我们几乎不在程序里修改环境变量.
//得到确定的环境变量#include <iostream>#include <cstdlib>using namespace std;int main(){      char* name = "HOME";      char* value = NULL;      value = getenv(name);      cout << "value=" << value << endl;}//设置环境变量#include <iostream>using namespace std;#include <cstdlib>int main(){      char* var = "baby=beauty";      char* name="baby";      putenv(var);      char* value = getenv(name);      cout << "value=" << value << endl;}


* 编程习惯:
1, 向函数传递指针数组的时候,如果没有说明元素个数,总是用一个NULL放在末尾.以

NULL作为结束标志.
2, 返回指针的函数,正常返回一个非空指针,出错返回NULL.
3, 返回整数函数,正常返回一个非负整数(通常是0),出错返回负数(通常是-1)
perror("提示");//显示内容为: 提示:错误信息文字描述
-----------------------------------------------------------
* 用户信息:
设置-用户权限:chmod u+s a.out//把用户ID符加到文件上
//指权限沾符到这个文件上,让使用这个文件的用户都有拥有者一样的权限
XXX_t 表示是一个整数
struct passwd* getpwuid (int userid)//得到一个指向passwd结构的指针,该结构

包括用户相关信息记录.
#include <iostream>using namespace std;#include <pwd.h>int main(){passwd* p = NULL;string name;int uid;cout << "input username or id:";char ch;ch = cin.peek();if(ch>='0'&&ch<='9'){cin >> uid;p = getpwuid(uid);}else{cin >> name;p = getpwnam(name.c_str());}if(p==NULL){cout << "error!" << endl;}else{cout << p->pw_name << endl;cout << p->pw_uid << endl;cout << p->pw_gid << endl;cout << p->pw_dir << endl;cout << p->pw_shell << endl;}}

---------------------------------------------------------
* 文件系统:
+ 目录
pwd:print working directory
/char* getcwd(char* buf,size_t size);//取得当前工作目录,出错返回NULL// buf是放目录名字符串的地方,一般是一个字符数组名.// size 为了避免越界,会把字符串长度也传过去// 凡是要自已准备字符数组来保存字符串的时候,也要把长度传过去.// 数组越界访问会导致破坏其它数据或程序崩溃.// 根据返回值判断是否出错// 函数里有一条语句: return buf;// 因此可以直接 cout << getcwd(buf,256);// getcwd.cc#include <iostream>using namespace std;#include <unistd.h>int main(){char buf = "(unknow)";// 一定要用char数组cout << getcwd(buf,256) << endl;sssscout << buf << endl;}DIR* opendir(const char* dirname)//打开目录/不能定义一个DIR对象,通常也不会定义DIR*类型变量struct dirent* readdir(DIR* dirp)//读取目录int closedir(DIR* dirp)//关闭目录流//dir.cc#include <iostream>using namespace std;#include <dirent.h>#include <sys/types.h>#include <list>#include <string>#include <iterator>#include <algorithm>int main(int argc,char* argv[]){      char* dir = NULL;      if(argc==1)                dir = ".";      else                dir = argv;      DIR* p = opendir(dir);//....      if(p==NULL){                cout << "not directory or not exitst" << endl;                return -1;//....      }      list<string> ls;      dirent* pd = NULL;      while( (pd=readdir(p))){                if(pd->d_name=='.') continue;                ls.push_back(pd->d_name);      }      closedir(p);//.....      ls.sort();      //copy(ls.begin(),ls.end(),ostream_iterator<string>(cout,"\n"));      list<string>::iterator it;      it = ls.begin();      while(it!=ls.end())                cout << *it++ << ' '<< endl;      cout << endl;      return 0;//....} //end of main

int stat(const char* path/*文件名*/,stat* buf)函数;//用来取得一个文件的信


//stat.cc#include <iostream>using namespace std;#include <sys/stat.h>#include <sys/types.h>#include <fcntl.h>int main(int argc, char* argv[]){      if(argc==1) {                cout << "no filename" << endl;                return 0;      }      int res;      struct stat s;      res = stat(argv,&s);      if(res <0){                cout << "no file"<<endl;                return -1;      }      cout << s.st_uid <<','<< s.st_size<<endl;      if(S_ISDIR(s.st_mode))                cout << "directory" << endl;      else                cout << "file" << endl;}//end main

int mkdir("新目录名",权限);//创建目录
int rmdir("目录名");//删除目录
getcwd(字符数组名,字符数组长度);//取得当前目录名
chdir("目录名");//设置当前目录名,在执行期间有效,程序结束失效

//目录操作示例#include <iostream>using namespace std;#include <sys/stat.h>#include <unistd.h>#include <string>int main(){      string cmd;      string dirname;      for(;;){                cout << "cmd:";                cin >> cmd;                if(cmd=="mkdir"){                        cin >> dirname;                        mkdir(dirname.c_str(),0777);                }                else if(cmd=="rmdir"){                        cin >> dirname;                        rmdir(dirname.c_str());                }                else if(cmd=="chdir"){                        cin >> dirname;                        chdir(dirname.c_str());                }                else if(cmd=="pwd"){                        char buf;                        getcwd(buf,256);                        cout << buf << endl;                }                else if(cmd=="bye"){                        cout << "C U" << endl;                        break;                }                else                        cout << "unknow command!"<< endl;      }// end for}// end main
rename("旧名","新名");//给目录或文件改名.

===================================================
+ 文件的操作02:03:17

文件权限的差别
int access(const char *path/*文件名*/,int amode)
amode:R_OK,W_OK,X_OK,F_OK(是否存在)
如果有指定权限,返回0.没有返回小于0
if(!access..)
用open()函数打开一个文件
ofstream fout;
fout.open("文件名");
int open(const char* pathname,int oflag,...);
<O_RDONLY|O_WRONLY|O_RDWR>这三个中必选一个,也只能选一个.
O_APPEND O_CREAT O_EXCL 可写可不写.
如果有O_CREAT,要传第三个参数就是创建文件权限,一般是8进制0755或0644
O_CREAT|O_EXCL表示必须创建新文件
返回文件描述符fd
进程使用文件描述符来操作文件.

文件描述符
shell产生三个标准文件描述符
0,1,2对应于cin,cout,cerr

删除文件:
unlink("文件名");//
remove("文件名");//

在UC里都是用文件描述符来描述文件
在对文件读的时候,如果读到的字节数小于要求的字节数,表示读完了
write(fd,内存地址,字节数)//向文件写
读和写都是顺序的.

//文件读写例程#include <iostream>using namespace std;#include <iostream>#include <fcntl.h>#include <unistd.h>int main(){      int fd = open("rw.txt",O_RDWR);      if( fd<0 ){                cout << "file not exist";                return -1;      }      int len;      char bu;      len = read(fd,bu,4);      for( int i=0;i<4;i++)                cout << bu;      cout << endl;      char* str = "xxxx";      write(fd,str,4);      close(fd);}
off_t lseek(int fd, off_t offset, int whene);
whene:SEEK_SET SEEK_CUR SEEK_END
对应core C++ 中的fin.seekg(offset);
fout.seekp(offset);
<>,<sys/stat.h>,<dirent.h>//目录操作头文件
//lseek.cc#include <iostream>using namespace std;#include <fcntl.h> //.......#include <unistd.h>//.......int main(){      int fd = open("rw.txt",O_RDWR);      if( fd<0 ){                cout << "file not exist" << endl;                return -1;      }      char ch;      for(int i=0;i<3;i++){                read(fd,&ch,1);                cout << ch;                lseek(fd,1,SEEK_CUR);      }      cout << endl;      lseek(fd,0,SEEK_SET);      char buf;      read(fd,buf,8);      buf = '\0';      cout << buf << endl;      lseek(fd,0,SEEK_END);      write(fd,"bye\n",4);      close(fd);}//因此我们可以用lseek函数来写一个小型的数据库
页: [1]
查看完整版本: UC++之目录和文件操作