c - 代码在 IDE 上运行,但不在 Linux 机器上运行 - C
问题描述
代码按预期在 Code::Blocks 环境中运行。但是在 Linux 机器上,它没有按预期工作。
问题陈述:在 Linux 上,它不读取输入文件。
目的:从输入和输出两个文本文件中提供的文本文件中读取。
输出_n.txt:
---List of Positive Cases---
--Flea X:+Positive+
--Anthony Kiedis:+Positive+
--Jack Irons:+Positive+
--Cliff Martinez:+Positive+
输出_p.txt:
---List of Negative cases:---
--Chad Smith:-Neg-
--John Frusciante:-Neg-
--Arik Marshall:-Neg-
这是我的源代码:
#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);
}
}
这是输入文件,医生列表.txt:
Flea X
Positive
Anthony Kiedis
Positive
Chad Smith
Negative
John Frusciante
Negative
Jack Irons
Positive
Cliff Martinez
Positive
Arik Marshall
Negative
我的程序在 Code::Blocks 中运行的屏幕截图:
在 Linux 环境中:
解决方案
代码在 IDE 上运行,但不在 Linux 机器上运行
在 Linux 上,您可以使用GNU emacs或vim或gedit等编辑您的 C 代码,然后使用作为(可能在终端中)调用的GCCgcc -Wall -Wextra -g
编译器编译您的代码以获取所有警告和调试信息。
当然阅读Modern C并查看此参考。另请参阅高级 Linux 编程和手册页,如syscalls(2)、fopen(3)、errno(3)等。
失败时您可能想使用perror(3) 。fopen
改进您的 C 代码,使其完全不会收到任何警告gcc
然后,使用GDB调试器了解程序的行为。你可以一步一步地运行你的程序,从调试器中询问变量的值等等。
还可以考虑使用Clang 静态分析器。
请注意,在 2021 年,UTF-8 无处不在。也许您想接受文件Gabrielles d'Estrées
中的一些输入doctorlist.txt
?
推荐阅读
- oracle - Oracle SQL Loader 和日期字段的问题
- sql - Big Query - 背靠背计算开始和结束日期
- javascript - 搜索 json 对象(反应)
- python - 如何避免此代码中的“列表索引超出范围”?
- html - 上传我的徽标后标题高度太大
- java - org.springframework.data.domain.Sort 为什么报错
- java - 尽管 TypeToken 不适合解析的字符串,但 Gson.fromJson 方法如何成功解析 Json 字符串?
- arrays - SwiftUI - 如何创建从属性中选择值的选择器?
- python - 在 sql 数据库上使用 python、sqlalchemy 轻松检查表是否存在
- flutter - Flutter:没有为“RetryOptions”类定义吸气剂“Pestawait”