关于#c语言#的问题:编程先定义一整型数组a[6],然后给数组a[6]由键盘赋值,最后将数组中元素值为奇数的元素值写入到a.txt文件中

用c语言,编程先定义一整型数组a[6],然后给数组a[6]由键盘赋值,最后将数组中元素值为奇数的元素值写入到a.txt文件中。


 
#include <stdio.h>
void main()
{
    FILE *fp;
    int i, a[6]; //i为数组元素下标
    fp = fopen("a.txt", "w");
    if (fp == NULL)
    {
        printf("Can't open the file!\n");
    }
    printf("请输入6个数:\n");
    for (i = 0; i < 6; i++)
    {
        scanf("%d", &a[i]);
        if (a[i] % 2 == 1)
            fprintf(fp, "%d ", a[i]);
    }
}

楼上代码最后再加个fclose即可