1.New Empty Project
2.Add command tool target
3.Add main.c
4.Add main.c to compile source
#include "stdio.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <errno.h>
#define BUFFER_SIZE 1024
int main(int argc,char **argv)
{
int fd;
int bytes_read;
char buffer[BUFFER_SIZE];
printf("File Read Started\n");
printf("the file you enter is: %s\n",argv[1]);
//open the file
fd=open(argv[1],O_RDONLY);
//read
while(bytes_read=read(fd,buffer,BUFFER_SIZE))
{
printf("%s",buffer);
}
printf("\n file read end \n");
//close
close(fd);
}
在scheme,Run Tab里Arguments页里边,有一个Arguments Passed On Launch.
这里边可以加入程序执行时传入的参数.
不过XCODE会自动传入第一个参数argv[0];
所以程序里边要使用argv[1]代表你自己传入的参数.