求教文件的成块读写函数fwrite和fread

chenzuzhang 2012-03-09 07:20:41
运行结果,输出显示没有问题,但是文件employee.txt中却是乱码:zhang3 烫烫烫烫烫烫? m烫蘬i4 烫烫烫烫烫烫烫烫# m烫


#include<stdio.h>
#include<string.h>
#define MAX_EMPLOYEE 100
struct Person
{
char m_strName[20]; //人员姓名
int m_nAge; //人员年龄
char m_nSex; //人员性别
};
struct Person Register(char *name,int age,char sex)
{
struct Person x;
strcpy(x.m_strName,name);
x.m_nAge=age;
x.m_nSex=((sex=='m')?'m':'f');
return x;
}
void ShowPerson(struct Person x)
{
printf("\n%s %d %c\n",x.m_strName,x.m_nAge,x.m_nSex);
}
int WriteRecord(FILE *file,struct Person *emp)
{
return fwrite(emp,sizeof(struct Person),1,file);
}
int ReadRecord(FILE *file,struct Person *emp)
{
return fread(emp,sizeof(struct Person),1,file);
}
void main()
{
struct Person personlist[MAX_EMPLOYEE],x;
int count=0;
int i;
FILE *emp_file;
personlist[count]=Register("zhang3",32,'m');
ShowPerson(personlist[count]);
count++;
personlist[count]=Register("li4",35,'m');
ShowPerson(personlist[count]);
count++;
emp_file=fopen("employee.txt","wt");
if(!emp_file)
{
printf("Error open file!\n");
return ;
}
for(i=0;i<count;i++)
WriteRecord(emp_file,&(personlist[i]));
fclose(emp_file);
emp_file=fopen("employee.txt","rt");
if(!emp_file)
{
printf("Error open file!\n");
return ;
}
ReadRecord(emp_file,&x);
ShowPerson(x);
fclose(emp_file);
}
...全文
88 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
chenzuzhang 2012-03-09
  • 打赏
  • 举报
回复
谢谢,我是新手,字符串读写学会了,想学一下成块读写,请大侠指教![Quote=引用 3 楼 ls176 的回复:]
C/C++ code


#include<stdio.h>
#include<string.h>

#define MAX_EMPLOYEE 100


typedef struct person
{
char m_strName[20]; //人员姓名
int m_nAge; //人员年龄
char m_nSex; ……
[/Quote]
自信男孩 2012-03-09
  • 打赏
  • 举报
回复
感觉还是用fscanf()和fprintf()进行文件读写比较好。
ls176 2012-03-09
  • 打赏
  • 举报
回复

#include<stdio.h>
#include<string.h>

#define MAX_EMPLOYEE 100


typedef struct person
{
char m_strName[20]; //人员姓名
int m_nAge; //人员年龄
char m_nSex; //人员性别
} Person;

Person Register( char *name , int age , char sex )
{
Person x;
strcpy( x.m_strName , name );
x.m_nAge = age;
x.m_nSex = ( ( sex == 'm' ) ? 'm' : 'f' );
return x;
}

void ShowPerson( Person x )
{
printf( "\n%s %d %c\n" , x.m_strName , x.m_nAge , x.m_nSex );
}

int WriteRecord( FILE *file , Person *emp )
{
//return fwrite( emp , sizeof( Person ) , 1 , file );
char row[ sizeof( Person ) ];
sprintf( row , "%s %d %c\n" , emp->m_strName , emp->m_nAge , emp->m_nSex );
return fputs( row , file );
}

int ReadRecord( FILE *file , Person *emp )
{
//return fread( emp , sizeof( Person ) , 1 , file );
char row[ sizeof( Person ) ];
fgets( row , sizeof( Person ) , file );
strcpy( emp->m_strName , strtok( row , " " ) );
emp->m_nAge = atoi( strtok( NULL , " " ) );
emp->m_nSex = strtok( NULL , " " )[0];
return 0;
}

void main()
{
Person personlist[MAX_EMPLOYEE] , x;
int count = 0;
int i;
FILE *emp_file;

personlist[ count ] = Register( "zhang3" , 32 , 'm' );
ShowPerson( personlist[ count ] );
count++;
personlist[ count ] = Register( "li4" , 35 , 'm' );
ShowPerson( personlist[ count ] );
count++;

emp_file=fopen( "employee.txt" , "wt" );
if( !emp_file )
{
printf( "Error open file!\n" );
return ;
}
for( i = 0 ; i < count ; i++ )
{
WriteRecord( emp_file , &personlist[i] );
}
fclose( emp_file );

emp_file = fopen( "employee.txt" , "rt" );
if( !emp_file )
{
printf( "Error open file!\n" );
return ;
}
ReadRecord( emp_file , &x );
ShowPerson( x );
fclose( emp_file );
}

改了下代码,改成读取字符串方式的了,这样文本文件中的内容就不是乱码了。
LZ如果是想成块保存结构(也就是序列化),那文件中就肯定是乱码。
如果想内容不是乱码,就按文本方式读取保存就可以了。
ProgrammingRing 2012-03-09
  • 打赏
  • 举报
回复
fwrite是二进制写入的
ProgrammingRing 2012-03-09
  • 打赏
  • 举报
回复
用十六进制工具,如WinHEX打开那文件看下你就知道了

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧