在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
#import<Foundation/Foundation.h> int main(int argc, char *argv[]) { if (argc == 1) { NSLog(@"you need to provide a file name"); return 1; } FILE* file = fopen(argv[1], "r"); char words[100]; while (fgets(words, 100, file)) { words[strlen(words) - 1] = '\0'; NSLog(@"%s is %d character long", words, strlen(words)); } fclose(file); return 0; }
argc是参数个数,argv是参数值: 例如上述程序在终端下运行时输入: ./main.exe words.txt
若argc==1表示只输入了./main.exe,缺乏文件名。 argv[0]中存程序名字(./main.exe); argv[1]中存文件名(words.txt). |
请发表评论