C代码,似乎跳过了if语句

发布于 2024-12-07 14:15:20 字数 2971 浏览 7 评论 0原文

首先也是最重要的,我不是要求任何人为我编写程序或为我做作业……我有一个错误,但我不知道是什么原因造成的,也不知道在哪里修复它。

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

下面是代码:

#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 语句,老实说我不明白为什么。任何帮助将不胜感激。

First and foremost, I am not asking anyone to write a program for me or to do my homework for me... I have an error and I can't figure out what's causing it, and where to look to fix it.

So I'm attempting to create a program that will take a command and an input file from the command line, and, based on the command selection, perform one of several functions: either coutn the lines of the input file, count the words of the input file, or check the input file for palindromes and then compare the palindromes (if any) to a dictionary file.

Here is the code as it stands:

#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;
}   

So, when I run:

gcc -o fr filereader.c

./fr -h fevbwervrwe.txt

I get the error message stating that the if statements were skipped.

Same if I try running

./fr -l vrweqvervvq.txt

It seems the compiler is skipping over my if statements, and I honestly can't figure out why. Any help at all would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

清晨说晚安 2024-12-14 14:15:20

尝试使用 strcmp() 而不是 ==

Try strcmp() instead of ==.

つ低調成傷 2024-12-14 14:15:20
if (argv[1] == "-h")

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

if (strcmp(argv[1], "-h") == 0)
if (argv[1] == "-h")

This statement compares two pointers, not two strings. Try:

if (strcmp(argv[1], "-h") == 0)
森林散布 2024-12-14 14:15:20

在 C 中,您需要使用类似

strcmp(str1, str2);

Don't Compare string value using the == 运算符的函数来比较字符串值;它检查完全不同的东西。

In C, you need to compare string values using functions like

strcmp(str1, str2);

Don't compare string values using the == operator; which checks something completely different.

赏烟花じ飞满天 2024-12-14 14:15:20

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

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

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

Compile with "gcc -g -o fr filereader.c", and step through under the debugger (e.g. gdb).

I assure you the compiler isn't "ignoring" your "if" statements.

Do NOT use "argv[1] == "-h". Use "if (strcmp (argv[1], "-h") == 0" instead...

寒江雪… 2024-12-14 14:15:20

在 C 中,您不使用 == 运算符来比较字符串。相反,您可以使用以下习惯用法:

if (strcmp(s1, s2) == 0) { ... }

In C you don't compare strings with the == operator. Instead you use the following idiom:

if (strcmp(s1, s2) == 0) { ... }
倥絔 2024-12-14 14:15:20

正如已经提到的,只需理解“argv[1]”==“-h”意味着您正在比较指针。请记住这一点。

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

As already mentioned, just understand that "argv[1]" == "-h" would mean you are comparing pointers. Keep this in mind.

And another suggestion I have for you is to look at getopt(). This is used to parse command line arguments in unix, and will make your life easier and help you in writing robust code.

不必了 2024-12-14 14:15:20

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

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

Try strcmp() instead of ==
as == compares the address where string values are stored
But strcmp compares the content (string values) which is required i think

e.g.
int main(){char *a="anshul";char *b="anshul";if(a==b){ printf("ok");} /comapres address where anshul is stored/ if(strcmp(a,b)){printf("ok"); /* this will compare values i.e string anshul therefore always equal but in previous case it depends whether both anshul stored on same memory or not
so not a advisable thing to do

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文