关于const变量的赋值问题
我用的是TC2.01英文版,我用const char *pfile = "f:\\maydata.bin";进行初始化时,却发现编译系统提示pfile指针悬挂了,也就是没能给*pfile赋值,我想知道什么原因,以下是我的原程序:#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#define MAXLEN 30
void listfile(char *filename);
int main(void)
{
const char *filename = "f:\\mydata.bin";
char name[MAXLEN];
size_t length = 0;
int age = 0;
char answer = 'y';
FILE *pFile = fopen(filename,"wb+");
do
{
fflush(stdin);
printf("\nenter a name less than %d characters:",MAXLEN);
gets(name);
printf("enter the age of %s:",name);
scanf(" %d",&age);
length = strlen(name);
fwrite(&length,sizeof(length), 1 ,pFile);
fwrite(name,sizeof(char),length,pFile);
fwrite(&age,sizeof(age), 1 ,pFile);
printf("do you want to enter another (y or n)?");
scanf("\n%c",&answer);
}while(tolower(answer) == 'y');
fclose(pFile);
listfile(filename);
return 0;
}
void listfile(char *filename)
{
size_t length = 0;
char name[MAXLEN];
int age = 0;
char format[20];
FILE *pFile = NULL;
sprintf(format,"\n%%-%ds age:%%4d",MAXLEN);
/*FILE *file = NULL;*/
pFile = fopen(filename,"rb");
printf("\nthe contents of %s are:",filename);
while(fread(&length,sizeof(length), 1 ,pFile) == 1)
{
if(length+1>MAXLEN)
{
printf("\nname too long.");
exit(1);
}
fread(name,sizeof(char),length,pFile);
name[length] = '\0';
fread(&age,sizeof(age), 1 ,pFile);
printf(format,name,age);
}
fclose(pFile);
}谢谢各位给我解答。