简体   繁体   English

代码在 IDE 上运行,但不在 Linux 机器上运行 - C

[英]Code runs on IDE but not on Linux machine - C

The code runs in Code::Blocks environment as expected.代码按预期在 Code::Blocks 环境中运行。 However on a Linux machine it is not working as expected.然而,在 Linux 机器上,它没有按预期工作。

Problem statement: On Linux, it does not read the input file.问题陈述:在 Linux 上,它不读取输入文件。

Purpose: Read from the text file provided in the input and output two text files.目的:从输入提供的文本文件中读取 output 两个文本文件。

output_n.txt:输出_n.txt:

---List of Positive Cases---
--Flea X:+Positive+
--Anthony Kiedis:+Positive+
--Jack Irons:+Positive+
--Cliff Martinez:+Positive+

output_p.txt:输出_p.txt:

---List of Negative cases:---
--Chad Smith:-Neg-
--John Frusciante:-Neg-
--Arik Marshall:-Neg-

This is my source code:这是我的源代码:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define SIZE 7

int c = 0;
char band_diagnose[SIZE];

int read_doctor_list(char filename[], char type[], char band_names[][20], char band_diagnose[])
{
    char line[100];
    int i = 0, j = 0;
    FILE *fp = fopen(filename, type);
    if (!filename)
    {
        printf("Fail to open the file");
        return 0;
    }
    else
    {
        while (fgets(line, 100, fp))
        {
            if (c % 2 == 0)
            {
                strcpy(band_names[i], line);
                strtok(band_names[i], "\n");
                i++;
            }
            else
            {
                if (!strcmp(line, "Positive\n"))
                {
                    band_diagnose[j] = 'P';
                }
                else if (!strcmpi(line, "Negative\n"))
                {
                    band_diagnose[j] = 'N';
                }

                j++;
            }

            c++;
        }

        return 1;
    }

    fclose(fp);
}

void band_practice_list(char filename[], char status[], char band_names[][20], char band_diagnose[])
{
    int i = 0;
    FILE *fp = fopen(filename, "w");
    if (!filename)
    {
        printf("Fail to open the file!");
    }
    else
    {
        if (!strcmp(status, "Positive"))
        {
            char pos[] = "+Positive+";
            fprintf(fp, "---List of Positive Cases---\n");
            for (i = 0; i < SIZE; i++)
            {
                if (band_diagnose[i] == 'P')
                {
                    fprintf(fp, "--%s:%s\n", band_names[i], pos);
                }
            }

            fclose(fp);
        }

        if (!strcmp(status, "Negative"))
        {
            rewind(fp);
            char neg[] = "-Neg-";
            fprintf(fp, "---List of Negative cases:---\n");

            for (i = 0; i < SIZE; i++)
            {
                if (band_diagnose[i] == 'N')
                {
                    fprintf(fp, "--%s:%s\n", band_names[i], neg);
                }
            }

            fclose(fp);
        }

        printf("Creating list...\n");
        for (i = 0; i < SIZE; i++)
        {
            if (band_diagnose[i] == 'P')
            {
                printf("%s:%c...Can't go to practice!:(\n\n", band_names[i], band_diagnose[i]);
            }
            else
            {
                printf("%s:%c...Good to go to practice!:)\n\n", band_names[i], band_diagnose[i]);
            }
        }
    }
}

int main(int argc, char **argv)
{
    char band_names[SIZE][20];
    char band_diagnose[SIZE];

    int n = read_doctor_list("doctorlist.txt", "r", band_names, band_diagnose);

    if (n)
    {
        band_practice_list("output_p.txt", "Positive", band_names, band_diagnose);
        band_practice_list("output_n.txt", "Negative", band_names, band_diagnose);
    }
}

This is the input file, doctorlist.txt:这是输入文件,医生列表.txt:

Flea X
Positive
Anthony Kiedis
Positive
Chad Smith
Negative
John Frusciante
Negative
Jack Irons
Positive
Cliff Martinez
Positive
Arik Marshall
Negative

Screenshot of my program run in Code::Blocks:我的程序在 Code::Blocks 中运行的屏幕截图: 我的程序在 Code::Blocks 中运行的屏幕截图

In Linux enviromment:在 Linux 环境中:在 Linux 环境中

Code runs on IDE but not on Linux machine代码在 IDE 上运行,但不在 Linux 机器上运行

On Linux, you could edit your C code with GNU emacs or vim or gedit etc..., then compile your code with the GCC compiler invoked as gcc -Wall -Wextra -g (perhaps in a terminal) to get all warnings and debug info. On Linux, you could edit your C code with GNU emacs or vim or gedit etc..., then compile your code with the GCC compiler invoked as gcc -Wall -Wextra -g (perhaps in a terminal) to get all warnings and debug信息。

Read of course Modern C and see this reference .当然阅读现代 C并查看此参考 See also Advanced Linux Programming and man pages like syscalls(2) , fopen(3) , errno(3) etc....另请参阅 高级 Linux 编程和手册页,如syscalls(2)fopen(3)errno(3)等。

You probably want to use perror(3) when fopen fails.fopen失败时,您可能想使用perror(3)

Improve your C code to get no warnings at all from gcc改进您的 C 代码以从gcc中获得任何警告

Then, use the GDB debugger to understand the behavior of your program.然后,使用GDB调试器了解程序的行为。 You could run your program step by step, ask values of variables, etc... from the debugger.你可以一步一步地运行你的程序,从调试器中询问变量的值等等。

Consider also using the Clang static analyzer .还可以考虑使用Clang static 分析器

Be aware that in 2021 UTF-8 is used everywhere .请注意,2021 年UTF-8 到处都在使用 Maybe you want to accept some input like Gabrielles d'Estrées in your doctorlist.txt file?也许您想在您的doctorlist.txt文件中接受一些输入,例如Gabrielles d'Estrées

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM