运行时程序终止~~没有报错~~求指教~~
#include<stdio.h>程序没有指针,无法分配内存。但是运行后程序终止,求指点!!
#include<ctype.h>
#include<string.h>
#include<stdlib.h>
#define MAXLEN 30
void listfile(char *filename);
int main(void)
{
char *filename="E:\\temp\\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];
sprintf(format,"\n%%-%ds Age:%%4d",MAXLEN);
FILE *pFile=fopen(filename,"rb");
printf("\nThe contents of %s are:",filename);
while(fread(&length,sizeof(length),1,pFile)==1)
{
if(length+1>MAXLEN)
{
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);
}
}
scanf(" %d",age);
void listfile(char *filename)
{
size_t length=0;
char name[MAXLEN];
int age=0;
char format[20];
sprintf(format,"\n%%-%ds Age:%%4d",MAXLEN);
FILE *pFile=fopen(filename,"rb");
printf("\nThe contents of %s are:",filename);
while(fread(&length,sizeof(length),1,pFile)==1)
{
if(length+1>MAXLEN)
{
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);
}
}
#include<stdio.h>
#include<ctype.h>
#include<string.h>
#include<stdlib.h>
#define MAXLEN 30
void listfile(char *filename);
int main(void)
{
char *filename="/tmp/test_dir/write.log"; //要有读写权限
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];
sprintf(format,"\n%%-%ds Age:%%4d",MAXLEN);
FILE *pFile=fopen(filename,"rb");
if(NULL != pFile){ //判断返回指针是否为NULL
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);
}
}
if(length+1>MAXLEN)
{
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);
}
scanf(" %d",age);