系统如何查找可执行文件
默认:在PATH路径下寻找文件文件下
执行当前目录下文件:
./:指定文件目录是当前目录
./count:执行当前目录文件
编译.c文件为运行文件
gcc -o count 0voice.c #将0voice.c编译为名字count
为什么主函数要那么写?
答案:不知道
int argc : 这一命令行的参数列数 对参数列数判断 防止输入参数过少
char* argv[]:指针数组 是个数组里面每一个元素都是char*指针
effect:获取要操作的文件名字
#include<stdio.h>
#define IN 0 //字母
#define OUT 1 //符号#define INIT OUT //默认符号状态int isWord(char c)
{if((c == ' ') || (c == ',') || (c == '.') || (c == '\'') || (c == '\'') ||(c == '+')||(c == '\n') || ('\t' == c) || (';' == c) || ('!' == c) || ('{' == c) || ('}' == c)){return 0;}return 1;
}
int count_word(char* filename)
{//1.设置开始状态int status = INIT;int countWord = 0;//2.读入文件 filename:默认当前目录的这个名字FILE *fp = fopen(filename,"r");if(fp == NULL) return -1;//3.循环获取文件中值 判断:// out状态 + 判断到了字母 单词数量+1 进入in// out状态 + 符号 out状态 不变// in状态 + 字母 in状态 不变// in状态 + 分隔符 进入out状态// //fgetc:文件中获取字符 EOF 0x800作为文件结尾char c;while((c = fgetc(fp)) != EOF){if(OUT == status){if(isWord(c)){countWord++;status = IN;}}else if(IN == status){if(!isWord(c)) status = OUT;}}return countWord;
}// argc:要接受多少个参数
//argv: ** 指向字符串名称 例如:
int main(int argc,char* argv[])
{//1.就一行不携带我们要的路径参数 那么直接错误if(argc < 2) return 1; //2.读入文件 统计数量 argv[1]:操作文件的名称字符串int count = count_word(argv[1]);printf("%d\n",count);return 0;
}
编译文件
gcc -o count count.c
gcc -o count ./count.c
使用c打开文本文档
./count b.txt
./:标明哪个目录 由于argc argv得到操作的文件