Anh em giúp mình với. Mình code bài này trên CodeBlock chạy ngon lành như bỏ sang compile bên Linux thì nó bị lỗi không đọc file được.
#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);
}
}
EDIT (@library): @Nham_Chanel Cậu nhớ sử dụng Markdown để format code từ các post sau nhé!