各位LZ帮我看下这个打开文件函数的问题
//我这个就是把姓名还有电话号码写入到文件,然后在从文件读去出来,编译能同过,运行的是后就出问题了//[local]1[/local]
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
typedef struct People People;
#define MAX 20
struct People
{
char str[MAX];
char number[MAX];
};
FILE* pfile=NULL;
char* filename="D:\\蒋臣健\\chen.bin";
void get_name(char* str,size_t i);
void show_people(People people);
void file_mode(FILE* PF, char* filename1, char* mode);
void file_mode(FILE* PF, char* filename1, char* mode)
{
if(!(PF=fopen(filename1,mode)))
{
printf("failed to open");
exit(1);
}
if(setvbuf(PF,NULL,_IOFBF,BUFSIZ))
{
printf("file buffering failed");
exit(2);
}
}
void get_name(char* str,size_t i)
{
size_t str_len=0;
printf("请输入姓名(不超过%d个字符):",MAX);
fgets(str,i,stdin);
str_len=strlen(str);
if(str[str_len-1]=='\n')
str[str_len-1]='\0';
}
int main()
{
People my_people;
char ch='a';
size_t i=3;
file_mode(pfile,filename,"wb");
do
{
fflush(stdin);
get_name(my_people.str,MAX);
printf("请输入电话号码");
fgets(my_people.number,MAX,stdin);
if(my_people.number[strlen(my_people.number)-1]=='\n');
my_people.number[strlen(my_people.number)-1]='\0';
printf("到这步了");
fwrite(&my_people,sizeof my_people,1,pfile);
printf("到这步了");
printf("还想输入吗(Y继续N结束):");
scanf("%c",&ch);
}while((char)toupper(ch)=='Y');
fclose(pfile);
pfile=NULL;
show_people(my_people);
fclose(pfile);
pfile=NULL;
return 0;
}
void show_people(People people)
{
int i=0;
size_t str_len=0;
file_mode(pfile,filename,"rb");
while( fread(&people,sizeof(People),1,pfile))
{
str_len=sizeof(people);
printf("%d",str_len);
printf("姓名:%s 电话号码:%s\n",people.str,people.number);
}
}
我用printf语句去最终,发现在fwrite(&my_people,sizeof my_people,1,pfile)这条语句通不过,删掉它就行,后来我是把void file_mode(FILE* PF, char* filename1, char* mode)这个函数的FILE*参数去掉了,也就是把这个函数改成void file_mode( char* filename1, char* mode)运行就成功了,我想问下为什么把函数写成void file_mode(FILE* PF, char* filename1, char* mode)这样就不行呢,难道文件指针不能做参数吗