| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1049 人关注过本帖
标题:这个程序怎样改呢?
取消只看楼主 加入收藏
zyx1989
Rank: 1
等 级:新手上路
帖 子:86
专家分:2
注 册:2011-9-17
结帖率:83.33%
收藏
 问题点数:0 回复次数:7 
这个程序怎样改呢?
#include<stdio.h>
#include<malloc.h>
struct student
{
    int num;
    char name;
    int age;
    int sex;
    struct student *link;
}stud;
  
 creat(int n)
{
    struct student *h,*p;
    int i;
    h=(struct student *)malloc(sizeof(stud));
    if(h==NULL)
    {
        printf("not enough memory!");
        return(0);
    }
    h=NULL;
    for(i=0;i<=n;i++)
    {
         
         
        if((p=(struct student *)malloc(sizeof(stud)))==NULL)

            
            
        {
            printf("内存不足!");
            return(0);
        }
        scanf("%d",&p->num);
        scanf("%s",&p->name);
        scanf("%d",&p->age);
        scanf("%s",&p->sex);
        p->link=h;
        h=p;
    }
     void save(stud *h)
     {
        FILE *fP;
        stud *p;
        if((fp=fopen("d:\\123","w"))==NULL)
        {
            printf("can now open file!");
            exit(1);
        }
        printf("\n saving file...\n");
        p=h;
        while(p!=NULL)
        {
            fwrite(p,sizeof(stud),1,fp);
            p=p->next;
        }
        fclose(fp);
        printf("*******save successful!********\n");
   
   
   
    }
 }
    void main()
    {
        struct student *h;
   
        h=(struct student *)creat(4);
         save(h);
    }
   
搜索更多相关主题的帖子: age include memory return 
2011-09-24 17:15
zyx1989
Rank: 1
等 级:新手上路
帖 子:86
专家分:2
注 册:2011-9-17
收藏
得分:0 
更正一下
#include<stdio.h>
#include<malloc.h>
struct student
{
    int num;
    char name;
    int age;
    int sex;
    struct student *link;
}stud;
  
 creat(int n)
{
    struct student *h,*p;
    int i;
    h=(struct student *)malloc(sizeof(stud));
    if(h==NULL)
    {
        printf("not enough memory!");
        return(0);
    }
    h=NULL;
    for(i=0;i<=n;i++)
    {
         
         
        if((p=(struct student *)malloc(sizeof(stud)))==NULL)

            
            
        {
            printf("内存不足!");
            return(0);
        }
        scanf("%d",&p->num);
        scanf("%s",&p->name);
        scanf("%d",&p->age);
        scanf("%s",&p->sex);
        p->link=h;
        h=p;
    }
     void save(struct student *h)
     {
        FILE *fp;
        stud *p;
        if((fp=fopen("d:\\123","w"))==NULL)
        {
            printf("can now open file!");
            exit(1);
        }
        printf("\n saving file...\n");
        p=h;
        while(p!=NULL)
        {
            fwrite(p,sizeof(stud),1,fp);
            p=p->link;
        }
        fclose(fp);
        printf("*******save successful!********\n");
   
     
   
    }
 }
    void main()
    {
        struct student *h;
   
        h=(struct student *)creat(4);
        save(h);
    }
   
2011-09-24 18:40
zyx1989
Rank: 1
等 级:新手上路
帖 子:86
专家分:2
注 册:2011-9-17
收藏
得分:0 
没呀,不知道。
2011-09-24 19:04
zyx1989
Rank: 1
等 级:新手上路
帖 子:86
专家分:2
注 册:2011-9-17
收藏
得分:0 
回复 4楼 mengcan555
我是写作两句了,这个没错
2011-09-24 19:06
zyx1989
Rank: 1
等 级:新手上路
帖 子:86
专家分:2
注 册:2011-9-17
收藏
得分:0 
回复 7楼 mengcan555
强制转换为指针变量了
2011-09-24 19:14
zyx1989
Rank: 1
等 级:新手上路
帖 子:86
专家分:2
注 册:2011-9-17
收藏
得分:0 
#include<stdio.h>
#include<malloc.h>
struct student
{
    int num;
    char name;
    int age;
    int sex;
    struct student *link;
}stud;
  
 creat(int n)
{
    struct student *h,*p;
    int i;
    h=(struct student *)malloc(sizeof(stud));
    if(h==NULL)
    {
        printf("not enough memory!");
        return(0);
    }
    h=NULL;
    for(i=0;i<=n;i++)
    {
         
         
        if((p=(struct student *)malloc(sizeof(stud)))==NULL)

            
            
        {
            printf("内存不足!");
            return(0);
        }
        scanf("%d",&p->num);
        scanf("%s",&p->name);
        scanf("%d",&p->age);
        scanf("%s",&p->sex);
        p->link=h;
        h=p;
    }
    }
     void save(struct student *h)
     {
        FILE *fp;
              struct student *p;
        if((fp=fopen("d:\\123","w"))==NULL)
        {
            printf("can now open file!");
            
        }
        printf("\n saving file...\n");
        p=h;
        while(p!=NULL)
        {
            fwrite(&p,sizeof(stud),1,fp);
            p=p->link;
        }
        fclose(fp);
        printf("*******save successful!********\n");
   
     
   
    }
 
    void main()
    {
        struct student *h;
   
        h=(struct student *)creat(4);
        save(h);
    }
    编译通过了,为什么存储时会出现。。不能为read呢?
2011-09-24 19:31
zyx1989
Rank: 1
等 级:新手上路
帖 子:86
专家分:2
注 册:2011-9-17
收藏
得分:0 
#include
#include
struct student
{
    int num;
    char name;
    int age;
    int sex;
    struct student *link;
}stud;
  
 creat(int n)
{
    struct student *h,*p;
    int i;
    h=(struct student *)malloc(sizeof(stud));
    if(h==NULL)
    {
        printf("not enough memory!");
        return(0);
    }
    h=NULL;
    for(i=0;i<=n;i++)
    {
         
         
        if((p=(struct student *)malloc(sizeof(stud)))==NULL)

            
            
        {
            printf("内存不足!");
            return(0);
        }
        scanf("%d",&p->num);
        scanf("%s",&p->name);
        scanf("%d",&p->age);
        scanf("%s",&p->sex);
        p->link=h;
        h=p;
    }
    }
     void save(struct student *h)
     {
        FILE *fp;
              struct student *p;
        if((fp=fopen("d:\\123","w"))==NULL)
        {
            printf("can now open file!");
            
        }
        printf("\n saving file...\n");
        p=h;
        while(p!=NULL)
        {
            fwrite(&p,sizeof(stud),1,fp);
            p=p->link;
        }
        fclose(fp);
        printf("*******save successful!********\n");
   
     
   
    }
 
    void main()
    {
        struct student *h;
   
        h=(struct student *)creat(4);
        save(h);
    }
    编译通过了,为什么存储时会出现。。不能为read呢?
2011-09-24 19:36
zyx1989
Rank: 1
等 级:新手上路
帖 子:86
专家分:2
注 册:2011-9-17
收藏
得分:0 
打开是乱码,能进一步指教么
2011-09-24 20:00
快速回复:这个程序怎样改呢?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018853 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved