| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 855 人关注过本帖
标题:求怎么把数据存入文件中
只看楼主 加入收藏
ldj34089850
Rank: 2
等 级:论坛游民
帖 子:22
专家分:12
注 册:2013-11-10
结帖率:60%
收藏
 问题点数:0 回复次数:1 
求怎么把数据存入文件中
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define MAXSIZE 100

typedef struct
{     
    int score;
    char no[10];
    char name[10];
    char sex[10];
    char born[15];
    char worktime[10];
    char address[20];
    char tel[15];
}Student;

typedef struct
{     
    int last;     
    Student data[MAXSIZE];
}Seqlist;

void displayAll(Seqlist *L)
{   
    int n;
    int i=0;   
    n=L->last;   
    printf("学号    姓名        性别    出生年月    工作时间    家庭地址    电话号码    数据结构成绩\n");
    while(i<=n)   
    {        
        printf("%-8s",L->data[i].no);
        printf("%-12s",L->data[i].name);
        printf("%-8s",L->data[i].sex);
        printf("%-12s",L->data[i].born);
        printf("%-12s",L->data[i].worktime);
        printf("%-12s",L->data[i].address);
        printf("%-12s",L->data[i].tel);
        printf("%-12d",L->data[i].score);
        printf("\n");
        i=i+1;  
    }
}   
void display(Seqlist *L,int i)
{     
    printf("学号    姓名        性别    出生年月    工作时间    家庭地址    电话号码    数据结构成绩\n");     
    printf("%-8s",L->data[i].no);
    printf("%-12s",L->data[i].name);
    printf("%-8s",L->data[i].sex);
    printf("%-12s",L->data[i].born);
    printf("%-12s",L->data[i].worktime);
    printf("%-12s",L->data[i].address);
    printf("%-12s",L->data[i].tel);
    printf("%-12d",L->data[i].score);
    printf("\n");
}

Student* inputdata()
{      
    Student s1;  
    Student *s=&s1;   
    char no[10];   
    printf("\n请输入学号:");   
    scanf("%s",&no);   
    if(no[0]=='#')
        return NULL;   
    strcpy(s->no,no);  
    printf("\n请输入姓名:");
    scanf("%s",s->name);
    printf("\n请输入性别:");
    scanf("%s",s->sex);
    printf("\n请输入出生年月:");
    scanf("%s",s->born);
    printf("\n请输入工作时间:");
    scanf("%s",s->worktime);
    printf("\n请输入家庭地址:");
    scanf("%s",s->address);
    printf("\n请输入电话号码:");
    scanf("%s",s->tel);
    printf("\n请输入数据结构成绩:");   
    scanf("%d",&(s->score));
    return s;
}

Seqlist* initseq()
{   
    FILE *fp=NULL;
    fp=fopen("student.dat","r");
    Seqlist *L;   
    L=(Seqlist *)malloc(sizeof(Seqlist));   
    L->last=-1;  
    Student *s;     
    printf("\n请输入学生信息,按#结束输入!");   
    s=inputdata();   
    while(s)  
    {
        L->last++;
        L->data[L->last]=*s;     
        s=inputdata();  
        fwrite(&L->data[L->last],sizeof(Seqlist),1,fp);
    }
    fclose(fp);
    return L;
}

int deleteElem(Seqlist *L,char ch[10])
{
    int j,i;
    for(i=0;i<=L->last+1;i++)
    {
        if(strcmp(L->data[i].no,ch)==0||strcmp(L->data[i].name,ch)==0)
        {
            if (L->last == 0)
            {
                free(L);
            }
            else
            {
                for(j=i;j<=L->last+1;j++)
                {
                    if (j+1 <= L->last+1)
                    {
                        L->data[j]=L->data[j+1];
                    }
                }
            }
        }
    }
    L->last--;  
    return 1;
}

void locateElem(Seqlist *L,char ch[10])
{
    int i;
    for(i=0;i<=L->last+1;i++)
        if(strcmp(L->data[i].no,ch)==0||strcmp(L->data[i].name,ch)==0)
        {  
            display(L,i);  
            break;
        }
}

int lengthList(Seqlist *L)
{
    return L->last+1;
}

void insertsort(Seqlist *L)
{
    Seqlist *L1=(Seqlist *)malloc(sizeof(Seqlist));  
    Student temp;
    int len,i,j;   
    len=L->last;
    for(i=0;i<=len;i++)  
    {         
        L1->data[i]=L->data[i];   
    }      
    L1->last=L->last;     
    for(i=1;i<=len;i++)  
    {         
        if(L1->data[i].score<L1->data[i-1].score)   
        {         
            temp.score=L1->data[i].score;           
            strcpy(temp.no,L1->data[i].no);      
            strcpy(temp.name,L1->data[i].name);  
            strcpy(temp.sex,L1->data[i].sex);
            strcpy(temp.address,L1->data[i].address);
            strcpy(temp.born,L1->data[i].born);
            strcpy(temp.tel,L1->data[i].tel);
            strcpy(temp.worktime,L1->data[i].worktime);
            L1->data[i]=L1->data[i-1];         
            for(j=i-2;(temp.score>L1->data[j].score)&&(j>=0);j--)     
                L1->data[j+1]=L1->data[j];        
            L1->data[j+1].score=temp.score;        
            strcpy(L1->data[j+1].no,temp.no);     
            strcpy(L1->data[j+1].name,temp.name);  
            strcpy(L1->data[j+1].sex,temp.sex);
            strcpy(L1->data[j+1].address,temp.address);
            strcpy(L1->data[j+1].born,temp.born);
            strcpy(L1->data[j+1].tel,temp.tel);
            strcpy(L1->data[j+1].worktime,temp.worktime);
        }     
    }   
    printf("\n排序后为:\n");  
    displayAll(L1);
}

void insertsort1(Seqlist *L)
{
    Seqlist *L1=(Seqlist *)malloc(sizeof(Seqlist));  
    Student temp;
    int len,i,j;   
    len=L->last;
    for(i=0;i<=len;i++)  
    {         
        L1->data[i]=L->data[i];   
    }      
    L1->last=L->last;     
    for(i=1;i<=len;i++)  
    {         
        if(L1->data[i].no<L1->data[i-1].no)   
        {                  
            temp.score=L1->data[i].score;
            strcpy(temp.no,L1->data[i].no);      
            strcpy(temp.name,L1->data[i].name);
            strcpy(temp.sex,L1->data[i].sex);
            strcpy(temp.address,L1->data[i].address);
            strcpy(temp.born,L1->data[i].born);
            strcpy(temp.tel,L1->data[i].tel);
            strcpy(temp.worktime,L1->data[i].worktime);
            L1->data[i]=L1->data[i-1];         
            for(j=i-2;(temp.no>L1->data[j].no)&&(j>=0);j--)     
                L1->data[j+1]=L1->data[j];
            L1->data[j+1].score=temp.score;
            strcpy(L1->data[j+1].no,temp.no);         
            strcpy(L1->data[j+1].name,temp.name);  
            strcpy(L1->data[j+1].sex,temp.sex);
            strcpy(L1->data[j+1].address,temp.address);
            strcpy(L1->data[j+1].born,temp.born);
            strcpy(L1->data[j+1].tel,temp.tel);
            strcpy(L1->data[j+1].worktime,temp.worktime);
        }     
    }   
    printf("\n排序后为:\n");  
    displayAll(L1);
}

int ChangeStudent(Seqlist *L,char ch[10])
{
    int i;
    for(i=0;i<L->last+1;i++)
        if(strcmp(L->data[i].no,ch)==0||strcmp(L->data[i].name,ch)==0)
        {
            printf("\n请输入学号:");
            scanf("%s",L->data[i].no);
            printf("\n请输入姓名:");
            scanf("%s",L->data[i].name);
            printf("\n请输入性别:");
            scanf("%s",L->data[i].sex);
            printf("\n请输入出生年月:");
            scanf("%s",L->data[i].born);
            printf("\n请输入工作时间:");
            scanf("%s",L->data[i].worktime);
            printf("\n请输入家庭地址:");
            scanf("%s",L->data[i].address);
            printf("\n请输入电话号码:");
            scanf("%s",L->data[i].tel);
            printf("\n请输入数据结构成绩:");   
            scanf("%d",&L->data[i].score);
            break;
        }
        return 1;
}

void main()
{         
    Seqlist *L;
    char c;
    char ch[10];
    int i,res,n,b=1;
    printf("\t\t\t仲************************仲\n");
    printf("\t\t\t**恺********************恺**\n");
    printf("\t\t\t****农****************农****\n");
    printf("\t\t\t******业************业******\n");
    printf("\t\t\t********工********工********\n");
    printf("\t\t\t**********程****程**********\n");
    printf("\t\t\t************学学************\n");
    printf("\t\t\t************院院************\n");
    printf("\t\t\t**********学****学**********\n");
    printf("\t\t\t********生********生********\n");
    printf("\t\t\t******管************管******\n");
    printf("\t\t\t****理****************理****\n");
    printf("\t\t\t**系********************系**\n");
    printf("\t\t\t统************************统\n");
    FILE *fp=fopen("student.dat","r");
    if(fp==NULL)
    {
        printf("是否创建一个文件用于保存数据(y/n)");
        scanf("%c",&c);
        fflush(stdin);
        if(c=='y'||c=='Y')
        {
            fp=fopen("student.dat","wb");
            fclose(fp);
        }
        else if(c=='n'||c=='N')
            exit(0);
    }
    else
    {
        fp=fopen("student.dat","ab+");
        fread(&L->data,sizeof(Seqlist),1,fp);
        fclose(fp);
    }   
   
    while(b)   
    {        
        printf("\n\n");  
        printf("1.添加学生信息\n");
        printf("2.删除学生信息\n");
        printf("3.查询学生信息\n");
        printf("4.修改学生信息\n");
        printf("5.按学号排序\n");
        printf("6.按成绩排序\n");
        printf("7.显示学生信息\n");
        printf("8.显示学生总数\n");
        printf("0.退出程序\n");
    //    system("cls");
        printf("\n\n请选择你所需要的功能:\n");
        scanf("%d",&n);         
        switch(n)   
        {            
        case 1:   
            L=initseq();
            displayAll(L);                    
            break;
        case 2:  
            printf("\n输入删除学生的学号或姓名:");           
            scanf("%s",&ch);                     
            res=deleteElem(L,ch);              
            if(res==1)                     
                displayAll(L);            
            break;
        case 3:
            printf("请输入姓名或学号:");      
            scanf("%s",ch);                  
            locateElem(L,ch);
            break;  
        case 4:
            printf("请输入学生的学号或姓名:");
            scanf("%s",&ch);
            res=ChangeStudent(L,ch);
            if(res==1)
                displayAll(L);
            break;
        case 5:   
            insertsort(L);
            break;  
        case 6:
            insertsort1(L);
            break;           
        case 7:  
            displayAll(L);
            break;
            case 8:   
            printf("\n学生总数为%d:\n",lengthList(L));  
            break;  
        case 0:  
            printf("已退出");        
            b=0;                  
            break;  
            
        }   
    }
}
搜索更多相关主题的帖子: address include 姓名 
2014-01-03 16:28
qiqi1314520
Rank: 2
等 级:论坛游民
帖 子:2
专家分:20
注 册:2014-1-18
收藏
得分:0 
去看看C语言里面的文件操作的那章节,
2014-01-18 20:35
快速回复:求怎么把数据存入文件中
数据加载中...
 
   



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

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