---
title: "UC++之目录和文件操作"
author: "yunchow"
date: "2009-06-05T11:55:15Z"
tags: ["C", "C#", "C++", "Qt", "编程"]
categories: ["编程语言"]
slug: "uc"
draft: false
lastmod: "2009-06-05T11:55:55Z"
id: 402416
url: "https://yunchow.iteye.com/blog/402416"
---

\* UC + 文件系统 - 目录操作 - 文件操作 + 多进程 - 进程间关系 - 进程间通信 + 网络通信 - 基础知识 - 实践(tcp/udp) QT,KDE: Linux环境下的图形环境编程 ================================================================= * 环境变量: + 如何在程序中取得环境变量的内容? \[code="c++"]//取得所有的环境变量 - #include using namespace std; int main(int argc, char* argv\[], char* env\[]) { for( int i=0; env\[i]; i++ ){ if(i&lt;9) cout &lt;&lt; 0; cout&lt;&lt; i+1 &lt;&lt;": " &lt;&lt; env\[i] &lt;&lt; endl; if(i%8==7) cin.get(); } cout &lt;&lt; endl; }\[/code] 在进程中设置的环境变量只在本进程与子进程里使用 因此我们几乎不在程序里修改环境变量. \[code="c++"]//得到确定的环境变量 #include #include using namespace std; int main() { char* name = "HOME"; char* value = NULL; value = getenv(name); cout &lt;&lt; "value=" &lt;&lt; value &lt;&lt; endl; } //设置环境变量 #include using namespace std; #include int main() { char* var = "baby=beauty"; char* name="baby"; putenv(var); char* value = getenv(name); cout &lt;&lt; "value=" &lt;&lt; value &lt;&lt; endl; }\[/code] * 编程习惯: 1, 向函数传递指针数组的时候,如果没有说明元素个数,总是用一个NULL放在末尾.以 NULL作为结束标志. 2, 返回指针的函数,正常返回一个非空指针,出错返回NULL. 3, 返回整数函数,正常返回一个非负整数(通常是0),出错返回负数(通常是-1) perror("提示");//显示内容为: 提示:错误信息文字描述 ----------------------------------------------------------- * 用户信息: 设置-用户权限:chmod u+s a.out//把用户ID符加到文件上 //指权限沾符到这个文件上,让使用这个文件的用户都有拥有者一样的权限 XXX\_t 表示是一个整数 struct passwd* getpwuid (int userid)//得到一个指向passwd结构的指针,该结构 包括用户相关信息记录. \[code="c++"]#include using namespace std; #include int main() { passwd* p = NULL; string name; int uid; cout &lt;&lt; "input username or id:"; char ch; ch = cin.peek(); if(ch&gt;='0'&&ch&lt;='9'){ cin &gt;&gt; uid; p = getpwuid(uid); } else{ cin &gt;&gt; name; p = getpwnam(name.c\_str()); } if(p==NULL){ cout &lt;&lt; "error!" &lt;&lt; endl; } else{ cout &lt;&lt; p-&gt;pw\_name &lt;&lt; endl; cout &lt;&lt; p-&gt;pw\_uid &lt;&lt; endl; cout &lt;&lt; p-&gt;pw\_gid &lt;&lt; endl; cout &lt;&lt; p-&gt;pw\_dir &lt;&lt; endl; cout &lt;&lt; p-&gt;pw\_shell &lt;&lt; endl; } }\[/code] --------------------------------------------------------- * 文件系统: + 目录 pwd:print working directory \[code="c++"]/char* getcwd(char* buf,size\_t size);//取得当前工作目录,出错返回NULL // buf是放目录名字符串的地方,一般是一个字符数组名. // size 为了避免越界,会把字符串长度也传过去 // 凡是要自已准备字符数组来保存字符串的时候,也要把长度传过去. // 数组越界访问会导致破坏其它数据或程序崩溃. // 根据返回值判断是否出错 // 函数里有一条语句: return buf; // 因此可以直接 cout &lt;&lt; getcwd(buf,256); // getcwd.cc #include using namespace std; #include int main() { char buf\[256] = "(unknow)";// 一定要用char数组 cout &lt;&lt; getcwd(buf,256) &lt;&lt; endl;ssss cout &lt;&lt; buf &lt;&lt; endl; } DIR* opendir(const char* dirname)//打开目录 /不能定义一个DIR对象,通常也不会定义DIR\*类型变量 struct dirent* readdir(DIR* dirp)//读取目录 int closedir(DIR* dirp)//关闭目录流 //dir.cc #include using namespace std; #include #include #include #include #include #include int main(int argc,char* argv\[]) { char* dir = NULL; if(argc==1) dir = "."; else dir = argv\[1]; DIR* p = opendir(dir);//.... if(p==NULL){ cout &lt;&lt; "not directory or not exitst" &lt;&lt; endl; return -1;//.... } list ls; dirent* pd = NULL; while( (pd=readdir(p))){ if(pd-&gt;d\_name\[0]=='.') continue; ls.push\_back(pd-&gt;d\_name); } closedir(p);//..... ls.sort(); //copy(ls.begin(),ls.end(),ostream\_iterator(cout,"\\n")); list::iterator it; it = ls.begin(); while(it!=ls.end()) cout &lt;&lt; \*it++ &lt;&lt; ' ' &lt;&lt; endl; cout &lt;&lt; endl; return 0;//.... } //end of main\[/code] int stat(const char* path/\*文件名\*/,stat* buf)函数;//用来取得一个文件的信 息 \[code="c++"]//stat.cc #include using namespace std; #include #include #include int main(int argc, char* argv\[]) { if(argc==1) { cout &lt;&lt; "no filename" &lt;&lt; endl; return 0; } int res; struct stat s; res = stat(argv\[1],&s); if(res &lt;0){ cout &lt;&lt; "no file"&lt; using namespace std; #include #include #include int main() { string cmd; string dirname; for(;;){ cout &lt;&lt; "cmd:"; cin &gt;&gt; cmd; if(cmd=="mkdir"){ cin &gt;&gt; dirname; mkdir(dirname.c\_str(),0777); } else if(cmd=="rmdir"){ cin &gt;&gt; dirname; rmdir(dirname.c\_str()); } else if(cmd=="chdir"){ cin &gt;&gt; dirname; chdir(dirname.c\_str()); } else if(cmd=="pwd"){ char buf\[256]; getcwd(buf,256); cout &lt;&lt; buf &lt;&lt; endl; } else if(cmd=="bye"){ cout &lt;&lt; "C U" &lt;&lt; endl; break; } else cout &lt;&lt; "unknow command!"&lt;&lt; endl; }// end for }// end main\[/code] 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\_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,内存地址,字节数)//向文件写 读和写都是顺序的. \[code="c++"]//文件读写例程 #include using namespace std; #include #include #include int main() { int fd = open("rw.txt",O\_RDWR); if( fd&lt;0 ){ cout &lt;&lt; "file not exist"; return -1; } int len; char bu\[4]; len = read(fd,bu,4); for( int i=0;i&lt;4;i++) cout &lt;&lt; bu\[i]; cout &lt;&lt; endl; char* str = "xxxx"; write(fd,str,4); close(fd); }\[/code] 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); &lt;&gt;,,//目录操作头文件 \[code="c++"]//lseek.cc #include using namespace std; #include //....... #include //....... int main() { int fd = open("rw.txt",O\_RDWR); if( fd&lt;0 ){ cout &lt;&lt; "file not exist" &lt;&lt; endl; return -1; } char ch; for(int i=0;i&lt;3;i++){ read(fd,&ch,1); cout &lt;&lt; ch; lseek(fd,1,SEEK\_CUR); } cout &lt;&lt; endl; lseek(fd,0,SEEK\_SET); char buf\[9]; read(fd,buf,8); buf\[8] = '\\0'; cout &lt;&lt; buf &lt;&lt; endl; lseek(fd,0,SEEK\_END); write(fd,"bye\\n",4); close(fd); } //因此我们可以用lseek函数来写一个小型的数据库\[/code]