首先老师简单介绍了下这门课程的重要性,紧接着带我们回顾了Linux的一些相关知识。最后让我们自己动手实践,在Ubuntu下使用Codeblocks进行C语言编程。
二、编程案例 1.部分函数 open()函数功能:打开或者创建一个文件
代码实现:
int open(const char *pathname , int flags[,mode_t mode]);read()函数
功能:从已打开的设备或者文件中读取数据
代码实现:
ssize_t read(int fd,void * buf,size_t count);lseek()函数
功能:一个偏移量,决定文件读写的起始位置
代码实现:
off_t lseek(int fd, off_t offset, int whence);write()函数
功能:从已打开的设备或者文件中读写数据
代码实现:
ssize_t write(int fd,const void * buf,size_t count);close()函数
功能:当对已打开文件的操作结束后释放缓存
代码实现:
int close(int fd);2、综合运用
#include三、运行截图 四、总结#include #include #include #include int main(){ int tempFd = 0; char tempFileName[20] = "wangzihan.txt"; tempFd = open(tempFileName, O_RDWR|O_EXCL|O_TRUNC, S_IRWXG); if(tempFd == -1){ perror("file open error.n"); exit(-1); }//of if int tempLen = 0; char tempBuf[100] = {0}; scanf("%s", tempBuf); tempLen = strlen(tempBuf); write(tempFd, tempBuf, tempLen); close(tempFd); tempFd = open(tempFileName, O_RDONLY); if(tempFd == -1){ perror("file open error.n"); exit(-1); }//of if off_t tempFileSize = 0; tempFileSize = lseek(tempFd, 0, SEEK_END); lseek(tempFd, 0, SEEK_SET); while(lseek(tempFd, 0, SEEK_CUR)!= tempFileSize){ read(tempFd, tempBuf, 1024); printf("%sn", tempBuf); }//of while close(tempFd); return 0; }//of main
1.初步了解Ubuntu的用法,同时了解了几个C语言中的新函数——open、read、lseek、write、close,对系统级程序设计这门课程有了个初步的了解。
2.C语言代码规范
int main(){ // 左大括号紧跟main函数
}
for(){
} // of for, if, while同理
3.规范化变量名命名——驼峰命名法,对一些临时的变量用temp+**会使代码可读性增强



