以下程序应该从命令行读取(使用 argv[])并执行三个函数之一:
right(根据边长确定三角形是否为直角三角形:通过键入 mu -r [sidea] [sideb] [sidec]
findtext:(在给定文件中查找字符串的序列:(ie,"hello") 并列出找到它的行号。
count:(计算制表符、单词和退格符的数量)。
Findtext.c 正常运行:
例如,如果我输入 ./mu -f [string] [file.txt]
它成功地列出了文件和找到它的文件中的行号。
但是当我使用 -r(right) 选项运行相同的代码时,它会给我以下分段错误:
程序名称:./mu 3 分段错误(核心转储)
我在这段代码中哪里出错了?
#include <stdio.h>
#include <stdlib.h>
//#include "count.h"
//#include "right.h"
//#include "findtext.h"
#define STAND_ALONE 1
void right(int, char **);
void count(int, char **);
void findtext(int, char **);
#ifdef STAND_ALONE
int main(int argc, char *argv[])
{
printf("\n");
printf("Can only use one option(-f, -c, -r) at once. sorry!\n");
printf("\n");
printf("Program name: %s\n", argv[0]);
while ((argc > 1) && (argv[1][0] == '-'))
{
switch (argv[1][1])
{
case 'f': // findtext.c
printf("%s\n",&argv[1][2]);
findtext(argc, argv);
break;
case 'r': // right.c
printf("%s\n",&argv[1][2]);
right(argc, argv);
break;
case 'c': // count.c
printf("%s\n",&argv[1][2]);
count(argc,argv);
break;
default:
printf("Wrong Argument: %s\n", argv[1]);
}
++argv;
--argc;
}
return (0);
}
#endif
void right(int argc, char *argv[]){
int a;
int b;
int c;
int largest;
int a2;
int b2;
int c2;
/*if(argc != 4){
printf("please enter 3 sides, only \n");
} */
a = atoi(argv[2]);
b = atoi(argv[3]);
c = atoi(argv[4]);
//printf("argv2:%s ",argv[2]);
if((a <= 0 )|| (b <= 0) || (c <= 0))
printf("Only positive values allowed\n"); exit(0);
a2 = (a*a);
b2 = (b*b);
c2 = (c*c);
if((c > a) && (c > b))
largest = c;
if((b > a) && (b > c))
largest = b;
if((a > b) && (a > c))
largest = a;
if(largest == a){
printf("HEy hey hey!");
if((b2 + c2) == a2){ printf("%s %s %s is a right triangle\n",argv[2],argv[3],argv[4]); }
else{printf("%s %s %s is not a right triangle\n",argv[3],argv[2],argv[4]);}
}
if(largest == b){
printf("HEy");
if((a2 + c2) == b2){ printf("%s %s %s is a right triangle\n",argv[2],argv[3],argv[4]); }
else{printf("%s %s %s is not a right triangle\n",argv[2],argv[3],argv[4]);}
}
if(largest == c){
printf("yo");
if((a2 + b2) == c2){ printf("%s %s %s is a right triangle\n",argv[2],argv[3],argv[4]); }
else{printf("%s %s %s is not a right triangle\n",argv[2],argv[3],argv[4]);}
}
} /* end method right() */
void findtext(int argc, char *argv[]){
FILE *fin;
char buffer[100];
int counter;
char *ptr = buffer;
char *result;
//if(argc != 3) {printf("Usage: %s filename argc:%d\n", argv[0], argc); exit(1);}
fin = fopen(argv[3], "r");
if(!fin) {printf("Unable to open %s\n", argv[2]); exit(1); }
counter = 0;
while (fgets(buffer, 99, fin)){
counter = counter + 1;
if(strstr(ptr,argv[2])){
printf("%d. %s", counter, ptr);
printf("\n");
}
}
fclose (fin);
}
void count(int argc, char **argv){
FILE *fin;
int lcounter = 0;
int count = 0;
char name[100];
char ch;
int word = 0;
int nchar = 0;
fin = fopen(argv[1],"r"); // open file
if(fin==0){
printf("Could not find specified file.\n");
exit(0);
}
while((ch = getc(fin)) != EOF)
{
nchar++;
if(ch == '\n')
lcounter++;
if(isspace(ch) || ch == '\t' || ch == '\n')
word++;
}
printf("number of characters: %d\n",nchar);
printf("Lines: %d\n",lcounter);
printf("words: %d\n",word);
printf("\n");
fclose(fin);
}