Вывести всех мужчин из файла на экран - C (СИ)

Узнай цену своей работы

Формулировка задачи:

Доброго времени суток! Подскажите, пожалуйста, есть структура ФИО, пол, возраст. Записи вводятся с клавиатуры. Необходимо сохранить эту информацию в файл, а потом вывести всех мужчин на экран и в отдельный файл.
Есть код для чтения из файла структуры, но не пойму как добавить условие...
Листинг программы
  1. #include "stdafx.h"
  2. #include <cstdio>
  3.  
  4. int _tmain(int argc, _TCHAR* argv[])
  5. {
  6. FILE *file;
  7. struct food {
  8. char name[20];
  9. unsigned qty;
  10. float price;
  11. };
  12. struct food shop[10];
  13. char i=0;
  14. file = fopen("e:\\Test\\tst.txt", "r");
  15. while (fscanf (file, "%s%u%f", shop[i].name, &(shop[i].qty), &(shop[i].price)) != EOF) {
  16. printf("%s %u %.2f\n", shop[i].name, shop[i].qty, shop[i].price);
  17. i++;
  18. }
  19. file = fopen("e:\\Test\\fprintf.txt", "w");
  20. while (scanf ("%s%u%f", shop[i].name, &(shop[i].qty), &(shop[i].price)) != EOF) {
  21. fprintf(file, "%s %u %.2f\n", shop[i].name, shop[i].qty, shop[i].price);
  22. i++;
  23. }
  24. fread;
  25. return 0;
  26. }
Листинг программы
  1. #include "stdafx.h"
  2. #include <cstdio>
  3.  
  4. int _tmain(int argc, _TCHAR* argv[])
  5. {
  6. FILE *file;
  7. struct Person {
  8. char name[20];
  9. char gender[20];
  10. unsigned age;
  11. };
  12. struct Person SinglePerson[10];
  13. char i=0;
  14. file = fopen("e:\\Test.txt", "r");
  15. while (fscanf (file, "%s%s%u", SinglePerson[i].name, &(SinglePerson[i].gender), &(SinglePerson[i].age)) != EOF) {
  16. printf("%s %s %u\n", SinglePerson[i].name, SinglePerson[i].gender, SinglePerson[i].age);
  17. i++;
  18. }
  19. file = fopen("e:\\fprintf.txt", "w");
  20. while (scanf ("%s%s%u", SinglePerson[i].name, &(SinglePerson[i].gender), &(SinglePerson[i].age)) != EOF) {
  21. fprintf(file, "%s %s %u\n", SinglePerson[i].name, SinglePerson[i].gender, SinglePerson[i].age);
  22. i++;
  23. }
  24. fread;
  25. return 0;
  26. }
Модераторы, первый код не относится к теме о_0 Удалите его!

Решение задачи: «Вывести всех мужчин из файла на экран»

textual
Листинг программы
  1. #include<stdio.h>
  2.  
  3. struct st{
  4.     int number;
  5.     struct st* prev;
  6.     struct st* next;
  7. };
  8.  
  9. struct st* findelem(struct st*,int);
  10.  
  11. int main(void){
  12.     struct st *head=NULL,*z,*t;
  13.     int n,m;
  14.     char a,w;
  15.     clrscr();
  16.     printf("a-add\nf-find\nd-delete\nc-clear\ns-show\nm-print menu\ne-exit");
  17.     w=1;
  18.     while(w){
  19.         a=getch();
  20.         switch(a){
  21.             case'a':
  22.                 printf("\ninput number:");
  23.                 scanf("%d",&n);
  24.                 z=(struct st*)malloc(sizeof(struct st));
  25.                 z->number=n;
  26.                 printf("select place for elem:\n f-first\n l-last\n b-before number\n a-after number\n r-replase\n c-cancel");
  27.                 a=getch();
  28.                 switch(a){
  29.                     case'l':
  30.                         if(head!=NULL){
  31.                             t=head;
  32.                             while(t->next!=NULL)
  33.                                 t=t->next;
  34.                             t->next=z;
  35.                             z->prev=t;
  36.                             z->next=NULL;
  37.                             break;
  38.                         }
  39.                     case'f':
  40.                         if(head!=NULL){
  41.                             z->next=head;
  42.                             head->prev=z;
  43.                             head=z;
  44.                         }
  45.                         else{
  46.                             head=z;
  47.                             head->next=NULL;
  48.                             head->prev=NULL;
  49.                         }
  50.                     break;
  51.                     case'b':
  52.                     case'a':
  53.                         printf("\ninput number-point for insert:");
  54.                         scanf("%d",&m);
  55.                         t=findelem(head,m);
  56.                         if(t!=NULL){
  57.                             if(a=='b'){
  58.                                 if(t==head){
  59.                                     head->prev=z;
  60.                                     z->next=head;
  61.                                     head=z;
  62.                                     printf("%d inserted",n);
  63.                                     break;
  64.                                 }
  65.                                 t=t->prev;
  66.                             }
  67.                             //insert after t
  68.                             t->next->prev=z;
  69.                             z->next=t->next;
  70.                             t->next=z;
  71.                             z->prev=t;
  72.                             printf("%d inserted",n);
  73.                         }
  74.                         else
  75.                             printf("%d not found\ncanceled",m);
  76.                     break;
  77.                     case'r':
  78.                         free(z);
  79.                         printf("\ninput number for replace:");
  80.                         scanf("%d",&m);
  81.                         t=findelem(head,m);
  82.                         if(t){
  83.                             t->number=n;
  84.                             printf("%d changed to %d",m,n);
  85.                         }
  86.                         else
  87.                             printf("%d not found for replace");
  88.                     break;
  89.                     default:
  90.                         free(z);
  91.                         printf("\ncanceled");
  92.                 }
  93.                 printf("\naccepted");
  94.             break;
  95.             case'f':
  96.                 if(head!=NULL){
  97.                     printf("\ninput number for find:");
  98.                     scanf("%d",&n);
  99.                     if(findelem(head,n)!=NULL)
  100.                         printf("%d exist",n);
  101.                     else
  102.                         printf("%d not found",n);
  103.                 }
  104.                 else
  105.                     printf("\nempty");
  106.             break;
  107.             case'd':
  108.                 if(head!=NULL){
  109.                     printf("\ninput key for delete:");
  110.                     scanf("%d",&n);
  111.                     t=findelem(head,n);
  112.                     if(t==NULL)
  113.                         printf("%d not found for delete",n);
  114.                     else{
  115.                         if(t==head)
  116.                             head=head->next;
  117.                         else{
  118.                             t->next->prev=t->prev;
  119.                             t->prev->next=t->next;
  120.                         }
  121.                         free(t);
  122.                         printf("%d deleted",n);
  123.                     }
  124.                 }
  125.                 else
  126.                     printf("\nempty");
  127.             break;
  128.             case's':
  129.                 printf("\n");
  130.                 z=head;
  131.                 while(z!=NULL){
  132.                     printf(" %d ",z->number);
  133.                     z=z->next;
  134.                 }
  135.                 printf(" end");
  136.             break;
  137.             case'm':
  138.                 printf("\na-add\nf-find\nd-delete\nc-clear\ns-show\nm-print menu\ne-exit");
  139.             break;
  140.             case'e':
  141.                 w=0;
  142.             case'c':
  143.                 if(head!=NULL){
  144.                     while(head->next!=NULL){
  145.                         head=head->next;
  146.                         free(head->prev);
  147.                     }
  148.                     free(head);
  149.                     head=NULL;
  150.                 }
  151.                 printf("\nmemory cleared");
  152.             break;
  153.         }
  154.     }
  155.     return 0;
  156. }
  157.  
  158. struct st* findelem(struct st *s,int elem){
  159.     while(s!=NULL && s->number!=elem)
  160.         s=s->next;
  161.     if(s!=NULL)
  162.         return s;
  163.     else
  164.         return NULL;
  165. }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

8   голосов , оценка 3.625 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы