[习题24]C语言 I/O函数

2018-12-26  本文已影响21人  AkuRinbu

使用教材

《“笨办法” 学C语言(Learn C The Hard Way)》
https://www.jianshu.com/p/b0631208a794

https://github.com/zedshaw/learn-c-the-hard-way-lectures/tree/master/ex24

完整源码 ex24.c

#include <stdio.h>
#include "dbg.h"

#define MAX_DATA 100

typedef enum EyeColor {
    BLUE_EYES, GREEN_EYES, BROWN_EYE,
    BLACK_EYES, OTHER_EYES
} EyeColor;

const char *EYE_COLOR_NAMES[] = {
    "Blue","Green","Brown","Black","Other"
};

typedef struct Person {
    int age;
    char first_name[MAX_DATA];
    char last_name[MAX_DATA];
    EyeColor eyes;
    float income;
} Person;

int main(int argc, char *argv[]) 
{
    Person you = {.age = 0};
    int i = 0;
    char *in = NULL;

    printf("What's your First Name? ");
    in = fgets(you.first_name, MAX_DATA-1, stdin);
    check(in != NULL, "Failed to read first name");

    printf("What's your Last Name? ");
    in = fgets(you.last_name, MAX_DATA-1, stdin);
    check(in != NULL, "Failed to read last name");

    printf("How old are you?");
    int rc =fscanf(stdin, "%d", &you.age);
    check(rc > 0, "You have to enter a number.")

    printf("What color are your eyes:\n");
    for(i = 0;i <= OTHER_EYES; i++) {
        printf("%d)  %s\n",i+1, EYE_COLOR_NAMES[i]);
    }
    printf("> ");

    int eyes = -1;
    rc = fscanf(stdin, "%d", &eyes);
    check(rc > 0, "You have to enter a number");

    you.eyes = eyes -1;
    check(you.eyes <= OTHER_EYES && you.eyes >= 0, "Do it right , that's not an option.");

    printf("How much do you make an hour? ");
    rc = fscanf(stdin, "%f", &you.income);
    check(rc > 0, "Enter a floating point number");

    printf("------ RESULTS ------ \n");

    printf("First Name: %s", you.first_name);
    printf("Last Name: %s", you.last_name);
    printf("Age: %d\n", you.age);
    printf("Eyes: %s\n", EYE_COLOR_NAMES[you.eyes]);
    printf("Icome: %f\n", you.income);

    return 0;
error:


    return -1;
}

Makefile

CC=clang

clean:
    rm ex24

运行

$ make ex24
clang     ex24.c   -o ex24
$ ls
dbg.h  ex24  ex24.c  Makefile
$ ./ex24
What's your First Name? Zed
What's your Last Name? Shaw
How old are you?37
What color are your eyes:
1)  Blue
2)  Green
3)  Brown
4)  Black
5)  Other
> 1
How much do you make an hour? 1.2345
------ RESULTS ------ 
First Name: Zed
Last Name: Shaw
Age: 37
Eyes: Blue
Icome: 1.234500

说明

使用 fgets

typedef + struct

typedef DEFINITION IDENTIFIER;
typedef struct [STRUCT_NAME] {
ELEMENTS;
} IDENTIFIER;

I/O 函数

https://zh.cppreference.com/w/cpp/io/c/fscanf
https://zh.cppreference.com/w/cpp/io/c/fgets
https://zh.cppreference.com/w/cpp/io/c/fopen
https://zh.cppreference.com/w/cpp/io/c/freopen
https://zh.cppreference.com/w/cpp/io/c/fdopen
https://zh.cppreference.com/w/cpp/io/c/fclose
https://zh.cppreference.com/w/cpp/io/c/fcloseall
https://zh.cppreference.com/w/cpp/io/c/fgetspos
https://zh.cppreference.com/w/cpp/io/c/fseek
https://zh.cppreference.com/w/cpp/io/c/ftell
https://zh.cppreference.com/w/cpp/io/c/rewind
https://zh.cppreference.com/w/cpp/io/c/fprintf
https://zh.cppreference.com/w/cpp/io/c/fwrite
https://zh.cppreference.com/w/cpp/io/c/fread

上一篇 下一篇

猜你喜欢

热点阅读