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")

このステートメントは、2つの文字列ではなく、2つのポインタを比較します。試す:

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-ofr 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");} / anshulが格納されているcomapresアドレス/if(strcmp( a、b)){printf( "ok"); / *これは値を比較します。つまり、文字列anshulは常に等しいのですが、前のケースでは、両方のanshulが同じメモリに格納されているかどうかによって異なります。

于 2011-10-04T03:46:02.030 に答える