| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1049 人关注过本帖
标题:这个程序怎样改呢?
只看楼主 加入收藏
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
mengcan555
Rank: 4
等 级:业余侠客
帖 子:104
专家分:253
注 册:2011-9-17
收藏
得分:0 
creat(int n)
{   少了一个右括号}
2011-09-24 19:35
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
mengcan555
Rank: 4
等 级:业余侠客
帖 子:104
专家分:253
注 册:2011-9-17
收藏
得分:0 
运行通过的代码如下:哥们以后注意细节啊,浪费个那么多时间
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
typedef struct student
{
    int num;
    char name;
    int age;
    int sex;
    struct student *link;
}stud;

stud*creat(int n)
{
    stud *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;
        
    }
    return h;
}
    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(2);
    save(h);
}

2011-09-24 19:48
zyx1989
Rank: 1
等 级:新手上路
帖 子:86
专家分:2
注 册:2011-9-17
收藏
得分:0 
打开是乱码,能进一步指教么
2011-09-24 20:00
mengcan555
Rank: 4
等 级:业余侠客
帖 子:104
专家分:253
注 册:2011-9-17
收藏
得分:0 
就该是乱码啊,你用专门的编辑器才可以看到具体的字符
2011-09-24 21:59
爱德华
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:2
帖 子:183
专家分:536
注 册:2011-5-29
收藏
得分:0 
回复 14楼 mengcan555
倒数第三局需要强制类型转换吗!?

算法,数据结构,windows核心编程.
2011-09-24 22:17
mengcan555
Rank: 4
等 级:业余侠客
帖 子:104
专家分:253
注 册:2011-9-17
收藏
得分:0 
回复 17楼 爱德华
我是在他原来函数的基础上更改的,他原来函数返回值不是指针,现在就不用强制类型转换了
2011-09-24 23:23
mengcan555
Rank: 4
等 级:业余侠客
帖 子:104
专家分:253
注 册:2011-9-17
收藏
得分:0 
回复 15楼 zyx1989
frite()是以二进制格式写入的,肯定是乱码啊,这不是错误,你若看看写入的数据,那就用fread读出来在屏幕上显示
2011-09-24 23:27
快速回复:这个程序怎样改呢?
数据加载中...
 
   



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

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