Программное обеспечение разбивает файл на три файла: мужской, женский и ошибочный. Текстовый файл форматируется:
Имя, Фамилия, Возраст, Пол... Но разделены пробелом.
пример.txt выглядит так:
Tim Smith 18 M
Jonathon Jones 26 M
Kathy Black 13 F
Sarah Saxby 28 F
Я уже разбил его на мужчин и женщин, но я изо всех сил пытаюсь заставить его работать в зависимости от возраста... Вот код, любая помощь очень ценится.
/*
* C program to split lines of text according to gender and age
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
/* Function declarations */
int isMale(char gender, int age);
int isFemale(char gender, int age);
/* Returns true (1) if the character in the last field is M and age is 4-18 */
int isMale(char gender, int age)
{
if (gender == 'M' && age<=18 && age>=5)
{
// printf("Male %i \n", age);
return 1;
}
return 0;
}
/* Returns true (1) if the character in the last field is F and age is 4-18 */
int isFemale(char gender, int age)
{
if (gender == 'F')
{
// printf("Female %i \n", age);
return 1;
}
return 0;
}
int main()
{
/* File pointer to hold reference to different files */
FILE * fPtrIn, // Input file
* fPtrMale, // Males of school age
* fPtrFemale, // Females of school age
* fPtrMisc; // Data not within the given parameters
// Open all files to perform read/write.
fPtrIn = fopen("data/example.txt", "r");
fPtrMale = fopen("data/males.txt" , "w");
fPtrFemale = fopen("data/females.txt" , "w");
fPtrMisc = fopen("data/erroneus.txt", "w");
// current_char is the current character being read
char current_char;
// hoping that too long lines won't come
char line[300], line_parse[300];
// Last field is where gender is stored, ret is the token used for strtok()
char *last_field, *ret;
// 0 or 1, if the age is outside or within the age limits
int age;
int field_count = 0;
// fopen() return NULL if unable to open file in given mode
if(fPtrIn == NULL || fPtrMale == NULL || fPtrFemale == NULL || fPtrMisc == NULL)
{
// Unable to open file, exit program print result
printf("Unable to open file.\n");
printf("Check file exists and permissions are correct.\n");
exit(EXIT_FAILURE);
}
// File open success message
printf("File opened successfully. \n\n");
// Read an integer and store read status in success.
while (fgets(line, sizeof(line), fPtrIn) != NULL)
{
// Copy the line for parsing
strcpy(line_parse, line);
// Separate the line into tokens
last_field = ret = strtok(line_parse, " ");
while (ret != NULL)
{
age = 0;
last_field = ret;
printf("%s \n", ret);
if (field_count == 2)
{
age = atoi(ret);
}
field_count++;
if (field_count == 4)
{
field_count = 0;
}
ret = strtok(NULL, " ");
}
// Get the first character of the last field
if (last_field == NULL) current_char = '\0';
else current_char = last_field[0];
// Write each line to a separate file
if (isMale(current_char, age))
fputs(line, fPtrMale);
else if (isFemale(current_char, age))
fputs(line, fPtrFemale);
else
fputs(line, fPtrMisc);
}
// Close each file
fclose(fPtrIn);
fclose(fPtrMale);
fclose(fPtrFemale);
fclose(fPtrMisc);
printf("Data written to files successfully. \n");
return(0);
}
example.txt
. - person Jabberwocky   schedule 18.07.2020fopen
s странно, поэтому я пропустил это, но вы не предоставили первые 3-4 строки файла. - person Jabberwocky   schedule 18.07.2020isFemale()
не может проверитьage
человека. И гораздо лучшим названием было бы: `isFemaleOfSchoolAge() - person user3629249   schedule 19.07.2020if (gender == 'M' && age<=18 && age>=5)
большинство детей идут в детский сад в возрасте 4, а не 5 лет - person user3629249   schedule 19.07.2020if(fPtrIn == NULL || fPtrMale == NULL || fPtrFemale == NULL || fPtrMisc == NULL) { // Unable to open file, exit program print result printf("Unable to open file.\n"); printf("Check file exists and permissions are correct.\n"); exit(EXIT_FAILURE); }
Гораздо лучше проверять возвращаемое значение при каждом вызовеfopen()
сразу после вызоваfopen()
Затем используйте такой код: if( ! fptrin ) { perror(fopen to read example.txt failed); выход(EXIT_FAILURE); } (продолжение) - person user3629249   schedule 19.07.2020fopen' failed AND outputs the text reason the system thinks the call failed, all to
stderr. Remember, after one or more of the files are open and then a call to
fopen()` завершается с ошибкой, необходимо закрыть открытые файлы перед выходом - person user3629249   schedule 19.07.2020// Read an integer and store read status in success
Это не то, что делает вызовfgets()
. Скорее он читает целую строку из входного файла - person user3629249   schedule 19.07.2020