用C语言打开文本文件,然后以二进制保存

来源:百度知道 编辑:UC知道 时间:2024/06/27 05:45:23
请哪位高手给个程序

我给你段代码:(带注释)包括了读文件和写文件的操作过程
#include "stdafx.h"//若为工程则必须,若为单纯的一个cpp文件没必要
#include <stdio.h>
#include <stdlib.h>

#define NameNum 15//名字的最大长度
#define N 2//这里的数可以改,意思为要输入的人的个数

typedef struct node
{
char name[NameNum];
int number;
int age;
}Node;//定义一个结构体存储人的信息

Node s[N], m[N];//全局变量

void main(void )
{
int i;
FILE *fp;

fp = fopen("wuqianhu.txt","w");//以写的方式打开文件
if(!fp)
{
printf("Can't open the file!\n");
exit(-1);
}

printf("Please input the information of people:\n");
for(i = 0; i< N ; i++)//输入人的信息
{
scanf("%s",s[i].name);
scanf("%d",&s[i].number);
scanf("%d",&s[i].age);
}

for(i = 0; i< N ; i++)//格式化输入人的信息到文件中
{
fprintf(fp,"%20s",s