首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

运行时程序终止~没报错~求指教~

2013-12-22 
运行时程序终止~~没有报错~~求指教~~#includestdio.h#includectype.h#includestring.h#includestdl

运行时程序终止~~没有报错~~求指教~~

#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);
    }
}
程序没有指针,无法分配内存。但是运行后程序终止,求指点!!
[解决办法]
判断下fopen打开是否成功吧!

对应api都判断下返回值!使用句柄前,判断下是否有效!
[解决办法]
scanf(" %d",age);

scanf(" %d",&age);

另外fopen创建文件路径要先存在
[解决办法]
请判断每个函数调用的返回值!
[解决办法]

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);
    }
}


这函数代码很怪啊,fclose()放在循环里面,fopen放在循环外面。。。。。运行时程序终止~没报错~求指教~
[解决办法]
仅供参考:
#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);
        }

这两个if真心让人看不懂啊。。。。


scanf(" %d",age);

楼上说的这个也是个问题

热点排行