日期:2014-05-16  浏览次数:20558 次

Linux系统调用 - 文件操作

系统调用方式创建文件  -  测试程序一: test.c

#include<stdio.h>

#include<stdlib.h>

#include<sys/types.h>

#include<sys/stat.h>

#include<fcntl.h>

 

void create_file(char *filename){

         if(creat(filename, 0755)<0){         //创建一个文件赋予755权限

                   printf("create file %s failure!\n", filename);

                   exit(EXIT_FAILURE);

         }else{

                   printf("create file %s success!\n", filename);

         }

}

int main(int argc, char *argv[]){            //argc:参数的个数,argv数组:参数的名称数组,例如命令:hello  liu,有两个参数,一个是hello命令,一个是liu,所以argc2

         int i;

         if(argc<2){                // argc小于2,即为没有参数跟在命令后面

                   perror("you haven't input the filename, please try again!\n");

                   exit(EXIT_FAILURE);