Thread: Display all men from a file on the screen

  1. #1
    Registered User
    Join Date
    May 2015
    Posts
    10

    Display all men from a file on the screen

    I'm a newbie in C. Could you help me please. There is a structure First name, Last Name, gender, age. Entries must be entered from the keyboard. I need to save this information in a file and then show on the screen all entries with gender == Male and save them in a separate file. Thank you!
    There is a code for reading a structure from file, but I can't understand how to check gender == Male.
    Code:
    #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;
    }

  2. #2
    Registered User
    Join Date
    May 2013
    Posts
    228
    They way you check for equality between strings in C is with a call to strcmp(). Notice that you should include string.h.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    fscanf (file, "%s%s%u", SinglePerson[i].name, &(SinglePerson[i].gender), &(SinglePerson[i].age))
    name and gender have exactly same type, why do you read them differently?

    It should be
    Code:
    fscanf (file, "%19s%19s%u", SinglePerson[i].name, SinglePerson[i].gender, &(SinglePerson[i].age))
    And I would compare the return value with 3 and not EOF - since if scanf returns 0,1, or 2 - file is not ended yet, but the reading of all the values has failed - so you have several uninitialized variables as a result, It would be not wise to use them
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User
    Join Date
    May 2015
    Posts
    10
    Thank you!
    I did it )
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define M 2 // M plus 1 is equals to number of structres
    
    /* run this program using the console pauser or add your own getch, system("pause") or input loop */
    
        FILE *file;
        struct Person {
            char name[20]; 
            char gender[20]; 
            unsigned age; 
        };
        struct Person SinglePerson[1];
        char i=0;
        char counter = 0;
    
    void InputFromKeyboard () {
        file = fopen("Input.txt", "w");
        while(scanf("%s%s%u", SinglePerson[i].name, SinglePerson[i].gender, &(SinglePerson[i].age)) == 3 && i <= M) {
            fprintf(file, "%s %s %u\n", SinglePerson[i].name, SinglePerson[i].gender, SinglePerson[i].age); 
            counter++;
            i++;
            if (i == M+1) {
                break;
            }
        }
        fclose(file);
    };
    
    void OutputToFile () {
        file = fopen("Output.txt", "w");
        for (i = 0; i < counter; i++) {
            if (strcmp(SinglePerson[i].gender, "Male") == 0) {
                fprintf(file, "%s %s %u\n", SinglePerson[i].name, SinglePerson[i].gender, SinglePerson[i].age); 
            }
        }
        fclose(file);
    }
    
    void OutputToScreen () {
        file = fopen("Output.txt", "r");
        for (i = 0; i < counter; i++) {
            if (strcmp(SinglePerson[i].gender, "Male") == 0) {
                printf("%s %s %u\n", SinglePerson[i].name, SinglePerson[i].gender, SinglePerson[i].age); 
            }
        }
        fclose(file);
    }
    
    int main(int argc, char *argv[]) {
        InputFromKeyboard();
        OutputToFile();
        OutputToScreen();
        return 0;
    }

    Do you know how to rewrite it with linked list?

  5. #5
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    You should avoid global variables. You don't need it in your code.
    And please check if the file was successfuly opened.
    Also, study how you can give parameters to functions and how functions give a value back.

    I have updated your code a little bit to show you a better approach.
    Please attempt to update the other functions by your self.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
     
    #define M 2 // M plus 1 is equals to number of structres
    
    typedef struct person_struct {
        char name[20]; 
        char gender[20]; 
        unsigned age; 
    } Person;
    
    int InputFromKeyboard (Person SinglePerson[]) {
    
        int i = 0;
        FILE *file;
    
        file = fopen("Input.txt", "w");
        if (file == NULL) {
            return -1;
        }
    
        while( i < M && scanf("%s%s%u", SinglePerson[i].name, SinglePerson[i].gender, &(SinglePerson[i].age)) == 3) {
            fprintf(file, "%s %s %u\n", SinglePerson[i].name, SinglePerson[i].gender, SinglePerson[i].age);
            i++;
        }
    
        fclose(file);
        return i;
    };
    
    
    int main(int argc, char *argv[]) {
    
        Person SinglePerson[M];
        int counter;
    
        counter = InputFromKeyboard(SinglePerson);
        if (counter < 0) {
            fprintf(stderr, "Ooops\n");
            return 1;
        }
    
        OutputToFile();
        OutputToScreen();
    
        return 0;
    }
    Last edited by WoodSTokk; 05-31-2015 at 03:25 PM.
    Other have classes, we are class

  6. #6
    Registered User
    Join Date
    May 2015
    Posts
    10
    WoodSTokk, thank you a lot!
    Really good style, thank you again!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Updating display on screen?
    By shadow1515 in forum C Programming
    Replies: 2
    Last Post: 06-06-2008, 10:24 AM
  2. pipe to file and still display to screen?
    By chrispyone in forum Tech Board
    Replies: 9
    Last Post: 03-16-2005, 03:27 PM
  3. Flash display in DOS screen
    By lordkrishna in forum C++ Programming
    Replies: 9
    Last Post: 02-23-2004, 03:10 AM
  4. display at the center of the screen
    By leinad079 in forum C Programming
    Replies: 2
    Last Post: 04-11-2003, 08:51 PM
  5. Screen display
    By neandrake in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 02-25-2002, 08:56 PM

Tags for this Thread