• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
我的小小家园
            管理     
在xcode中写一个unix文件读取的程序

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]代表你自己传入的参数.

posted on 2012-01-13 11:20  暧鹅  阅读(924)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3