Вывести всех мужчин из файла на экран - C (СИ)
Формулировка задачи:
Доброго времени суток!
Подскажите, пожалуйста, есть структура ФИО, пол, возраст. Записи вводятся с клавиатуры. Необходимо сохранить эту информацию в файл, а потом вывести всех мужчин на экран и в отдельный файл.
Есть код для чтения из файла структуры, но не пойму как добавить условие...
Листинг программы
- #include "stdafx.h"
- #include <cstdio>
- int _tmain(int argc, _TCHAR* argv[])
- {
- FILE *file;
- struct food {
- char name[20];
- unsigned qty;
- float price;
- };
- struct food shop[10];
- char i=0;
- file = fopen("e:\\Test\\tst.txt", "r");
- while (fscanf (file, "%s%u%f", shop[i].name, &(shop[i].qty), &(shop[i].price)) != EOF) {
- printf("%s %u %.2f\n", shop[i].name, shop[i].qty, shop[i].price);
- i++;
- }
- file = fopen("e:\\Test\\fprintf.txt", "w");
- while (scanf ("%s%u%f", shop[i].name, &(shop[i].qty), &(shop[i].price)) != EOF) {
- fprintf(file, "%s %u %.2f\n", shop[i].name, shop[i].qty, shop[i].price);
- i++;
- }
- fread;
- return 0;
- }
Листинг программы
- #include "stdafx.h"
- #include <cstdio>
- int _tmain(int argc, _TCHAR* argv[])
- {
- FILE *file;
- struct Person {
- char name[20];
- char gender[20];
- unsigned age;
- };
- struct Person SinglePerson[10];
- char i=0;
- file = fopen("e:\\Test.txt", "r");
- while (fscanf (file, "%s%s%u", SinglePerson[i].name, &(SinglePerson[i].gender), &(SinglePerson[i].age)) != EOF) {
- printf("%s %s %u\n", SinglePerson[i].name, SinglePerson[i].gender, SinglePerson[i].age);
- i++;
- }
- file = fopen("e:\\fprintf.txt", "w");
- while (scanf ("%s%s%u", SinglePerson[i].name, &(SinglePerson[i].gender), &(SinglePerson[i].age)) != EOF) {
- fprintf(file, "%s %s %u\n", SinglePerson[i].name, SinglePerson[i].gender, SinglePerson[i].age);
- i++;
- }
- fread;
- return 0;
- }
Модераторы, первый код не относится к теме о_0 Удалите его!
Решение задачи: «Вывести всех мужчин из файла на экран»
textual
Листинг программы
- #include<stdio.h>
- struct st{
- int number;
- struct st* prev;
- struct st* next;
- };
- struct st* findelem(struct st*,int);
- int main(void){
- struct st *head=NULL,*z,*t;
- int n,m;
- char a,w;
- clrscr();
- printf("a-add\nf-find\nd-delete\nc-clear\ns-show\nm-print menu\ne-exit");
- w=1;
- while(w){
- a=getch();
- switch(a){
- case'a':
- printf("\ninput number:");
- scanf("%d",&n);
- z=(struct st*)malloc(sizeof(struct st));
- z->number=n;
- 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");
- a=getch();
- switch(a){
- case'l':
- if(head!=NULL){
- t=head;
- while(t->next!=NULL)
- t=t->next;
- t->next=z;
- z->prev=t;
- z->next=NULL;
- break;
- }
- case'f':
- if(head!=NULL){
- z->next=head;
- head->prev=z;
- head=z;
- }
- else{
- head=z;
- head->next=NULL;
- head->prev=NULL;
- }
- break;
- case'b':
- case'a':
- printf("\ninput number-point for insert:");
- scanf("%d",&m);
- t=findelem(head,m);
- if(t!=NULL){
- if(a=='b'){
- if(t==head){
- head->prev=z;
- z->next=head;
- head=z;
- printf("%d inserted",n);
- break;
- }
- t=t->prev;
- }
- //insert after t
- t->next->prev=z;
- z->next=t->next;
- t->next=z;
- z->prev=t;
- printf("%d inserted",n);
- }
- else
- printf("%d not found\ncanceled",m);
- break;
- case'r':
- free(z);
- printf("\ninput number for replace:");
- scanf("%d",&m);
- t=findelem(head,m);
- if(t){
- t->number=n;
- printf("%d changed to %d",m,n);
- }
- else
- printf("%d not found for replace");
- break;
- default:
- free(z);
- printf("\ncanceled");
- }
- printf("\naccepted");
- break;
- case'f':
- if(head!=NULL){
- printf("\ninput number for find:");
- scanf("%d",&n);
- if(findelem(head,n)!=NULL)
- printf("%d exist",n);
- else
- printf("%d not found",n);
- }
- else
- printf("\nempty");
- break;
- case'd':
- if(head!=NULL){
- printf("\ninput key for delete:");
- scanf("%d",&n);
- t=findelem(head,n);
- if(t==NULL)
- printf("%d not found for delete",n);
- else{
- if(t==head)
- head=head->next;
- else{
- t->next->prev=t->prev;
- t->prev->next=t->next;
- }
- free(t);
- printf("%d deleted",n);
- }
- }
- else
- printf("\nempty");
- break;
- case's':
- printf("\n");
- z=head;
- while(z!=NULL){
- printf(" %d ",z->number);
- z=z->next;
- }
- printf(" end");
- break;
- case'm':
- printf("\na-add\nf-find\nd-delete\nc-clear\ns-show\nm-print menu\ne-exit");
- break;
- case'e':
- w=0;
- case'c':
- if(head!=NULL){
- while(head->next!=NULL){
- head=head->next;
- free(head->prev);
- }
- free(head);
- head=NULL;
- }
- printf("\nmemory cleared");
- break;
- }
- }
- return 0;
- }
- struct st* findelem(struct st *s,int elem){
- while(s!=NULL && s->number!=elem)
- s=s->next;
- if(s!=NULL)
- return s;
- else
- return NULL;
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д