是否有办法将输入的一串数字就以数字的形式保存在文件中?

有没有办法将输入的一窜数字就以数字的方式储存在文件中?我尝试了几个打开文件的时候看到的都是乱码。


#include<stdio.h>
#include<stdlib.h>

int main()
{
    FILE *pt;
    int num[10]={1,2,3,4,5,6,7,8,9,10};
    pt=fopen("file3.dat","wb");
    fwrite(num,sizeof(int),10,pt);
    fclose(pt);
    return 0;
}

#include<stdio.h>
#include<stdlib.h>
#define N 3

struct student{
    char name[10];            //学生的姓名
    int num;                       //学生的学号
    double score;              //学生的成绩
}stu[N];

void save()
{
    FILE *pt;
    int i;
    if((pt=fopen("file1.dat","w"))==NULL){
        printf("ERROR!");
        exit(0);
    }
    for(i=0;i<N;i++){
        if(fwrite(&stu[i],sizeof(struct student),1,pt)!=1){
            printf("WRITE ERROR!");
        }
    }
    fclose(pt);
}

int main()
{
    int i;
    for(i=0;i<N;i++){
        scanf("%s %d %lf",stu[i].name,&stu[i].num,&stu[i].score);      //输入学生的信息
    }
    save();
    return 0;
}

供参考:

#include<stdio.h>
#include<stdlib.h>
#define N 3
struct student{
    char name[10];            //学生的姓名
    int num;                       //学生的学号
    double score;              //学生的成绩
}stu[N];
void save()
{
    FILE *pt;
    int i;
    if((pt=fopen("file1.dat","w"))==NULL){
        printf("ERROR!");
        exit(0);
    }
    for(i=0;i<N;i++){
        //if(fwrite(&stu[i],sizeof(struct student),1,pt)!=1){
        //    printf("WRITE ERROR!");
        //}
        fprintf(pt,"%s %d %f\n",stu[i].name,stu[i].num,stu[i].score);
    }
    fclose(pt);
}
int main()
{
    int i;
    for(i=0;i<N;i++){
        scanf("%s %d %lf",stu[i].name,&stu[i].num,&stu[i].score);      //输入学生的信息
    }
    save();
    return 0;
}

你把它转换成文本,以文本方式写入应该就不会乱码了。因为你是用文本编辑器打开的这个文件,当然只能识别文本了