2

首先,我并没有要求任何人为我编写程序或为我做作业......我有一个错误,我无法弄清楚是什么导致它,以及在哪里寻找修复它。

因此,我正在尝试创建一个程序,该程序将从命令行获取命令和输入文件,并根据命令选择执行以下几个功能之一:要么计算输入文件的行数,要么计算输入文件,或检查输入文件的回文,然后将回文(如果有)与字典文件进行比较。

这是代码:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>

void usage(char *prog_name) //usage file if input format is wrong
{
    printf("Usage: %s, <-h>|<-l>|<-w>|<-p>, <filename>\n", prog_name);
    exit(0);
}

void help(char *prog_name, char *filename) //help file for -h option
{
    printf("Welcome to the help file\n");
    printf("For number of lines, enter: <%s>, <-l>, <%s>.\n", prog_name, filename);
    printf("For number of words, enter: <%s>, <-w>, <%s>.\n", prog_name, filename);
    printf("To list the palindromes in alphabetical order, print the number of
    palindromes, and to check them against the dictionary, enter: <%s>, <-p>, <%s>\n",
    prog_name, filename);
    exit(0);
}

void fatal(char *);  //function for fatal errors
void *ec_malloc(unsigned int);  //error-checked malloc wrapper

void linecount(char *filename)  // counts the number of lines of input file
{
    char *infile;
    infile = (char *) ec_malloc(strlen(filename));
    strcpy(infile, filename);
    FILE *fp = fopen(infile, "r");

    if (fp == NULL)         
         fatal("input file does not exist");

    int ch;
    int count = 0;                                  

    do {             
    ch = fgetc(fp);
    if( ch== '\n')
            count++;
    }   
    while( ch != EOF );                                 

    printf("Total number of lines %d\n",count);
    exit(0);            
}

int main(int argc, char *argv[])
{
    if (argc < 3) //if there aren't enough arguments, display the usage file
    {
            usage(argv[0]);
    }
    else if (argv[1] == "-h") //this is the help option
    {
        help(argv[0], argv[2]);
    }
    else if (argv[1] == "-l") //this is the line count option
    {
        linecount(argv[2]);
    }
    else
    {

            fatal("skipped if functions");
    }
    return 0;
}
void fatal(char *message) //this function displays an error message and then exits
{   
    char error_message[100];
    strcpy(error_message, "[!!] Fatal Error ");
    strncat(error_message, message, 83);
    perror(error_message);
    exit(-1);
}

void *ec_malloc(unsigned int size) //wrapper function for an error checked malloc
{
    void *ptr;
    ptr = malloc(size);

    if(ptr == NULL)
        fatal("in ec_malloc() on memory allocation");

    return ptr;
}   

所以,当我运行时:

gcc -o fr filereader.c

./fr -h fevbwervrwe.txt

我收到错误消息,指出 if 语句被跳过。

如果我尝试跑步也一样

./fr -l vrweqvervvq.txt

似乎编译器跳过了我的 if 语句,老实说,我不知道为什么。任何帮助都将不胜感激。

4

7 回答 7

10

尝试strcmp()代替==.

于 2011-10-03T21:18:01.430 回答
6
if (argv[1] == "-h")

该语句比较两个指针,而不是两个字符串。尝试:

if (strcmp(argv[1], "-h") == 0)
于 2011-10-03T21:18:33.660 回答
5

在 C 中,您需要使用以下函数来比较字符串值

strcmp(str1, str2);

不要使用==运算符比较字符串值;它检查完全不同的东西。

于 2011-10-03T21:19:12.257 回答
3

使用“gcc -g -o fr filereader.c”编译,并在调试器(例如 gdb)下单步执行。

我向您保证,编译器不会“忽略”您的“if”语句。

不要使用 "argv[1] == "-h"。使用 "if (strcmp (argv[1], "-h") == 0" 代替......

于 2011-10-03T21:19:18.260 回答
2

在 C 中,您不会将字符串与==运算符进行比较。相反,您使用以下成语:

if (strcmp(s1, s2) == 0) { ... }
于 2011-10-03T21:20:01.053 回答
1

如前所述,只需了解 "argv[1]" == "-h" 意味着您正在比较指针。请记住这一点。

我给你的另一个建议是看看getopt(). 这用于解析 unix 中的命令行参数,将使您的生活更轻松,并帮助您编写健壮的代码。

于 2011-10-03T21:40:53.103 回答
1

尝试 strcmp() 而不是 == 因为 == 比较存储字符串值的地址 但是 strcmp 比较我认为需要的内容(字符串值)

例如 int main(){char *a="anshul";char *b="anshul";if(a==b){ printf("ok");} / commapres 存储 anshul 的地址/ if(strcmp( a,b)){printf("ok"); /* 这将比较值,即字符串 anshul 因此总是相等,但在以前的情况下,这取决于两个 anshul 是否存储在同一内存中,所以不建议这样做

于 2011-10-04T03:46:02.030 回答