回覆列表
  • 1 # 使用者834195712159

    程式碼示例:

    #include

    <stdio.h>

    #include

    <stdlib.h>

    #include

    <string.h>

    typedef

    struct

    STU_INFO

    {

    char

    stuNo[7];

    char

    stuName[11];

    }

    STU_INFO;

    int

    main(int

    argc,

    char

    *argv[])

    {

    FILE

    *in,

    *out;

    char

    stuNo[7];

    char

    stuName[11];

    STU_INFO

    info;

    char

    *filePath

    =

    "c:/stu.dat";

    if

    ((out

    =

    fopen(filePath,

    "a+"))

    ==

    NULL)

    {

    printf("open

    file

    error");

    return

    -1;

    }

    printf("請輸bai入du0結束\n");

    while(1)

    {

    printf("請輸入學號(長度6)

    exac

    100000:\n");

    scanf("%s",

    stuNo);

    if

    (atol(stuNo)

    ==

    0)

    {

    break;

    }

    else

    if

    (atol(stuNo)

    &&

    strlen(stuNo)

    ==

    6)

    {

    printf("請輸入姓名(長度<=10)

    exac

    張三:\n");

    scanf("%s",

    stuName);

    fprintf(out,

    "%s,%s\n",

    stuNo,

    stuName);

    }

    else

    {

    printf("輸入錯誤\n");

    continue;

    }

    }

    fclose(out);

    printf("\n

    請輸入要查詢的zhi學號:dao\n");

    scanf("%s",

    stuNo);

    if

    ((in

    =

    fopen(filePath,

    "r"))

    ==

    NULL)

    {

    printf("open

    file

    error");

    return

    -1;

    }

    while

    (!feof(in))

    {

    fscanf(in,

    "%6s,%s",

    info.stuNo,

    info.stuName);

    if

    (atol(stuNo)

    ==

    atol(info.stuNo))

    {

    printf("學號:%s

    姓名:%s\n",

    info.stuNo,

    info.stuName);

    break;

    }

    }

    if

    (feof(in))

    {

    printf("不存在這個學生!!!\n");

    }

    fclose(in);

    return

    0;

    }

  • 中秋節和大豐收的關聯?
  • 電視劇《鬥破蒼穹》紫雲翼變成了金色,你們怎麼看?