梵A修
2017-12-22 12:28
采纳率: 50%
浏览 1.5k
已采纳

C语言文件读取写入的问题

我用以下代码进行读写操作一次以后,文件照常写入,可是第二次读取后就有一部分莫名消失
求大神指点

typedef struct Shu{//存储站名序号// 
    char name[10];
    int x;
    struct Shu *next;
}*S,s;
void BC2(S *head){//保存所有站点编号// 
    FILE *f;
    S p=*head;
    f=fopen("Shu.txt","w+");
    while(p!=NULL){
        fwrite(p,sizeof(s),1,f);
        p=p->next;
    }
    fclose(f);
}
struct Shu *Read2(){//读取所有站点编号// 
    FILE *f;
    S head,p;
    head=(S)malloc(sizeof(s));
    head->next=NULL;
    f=fopen("Shu.txt","r+");
    p=head;
    while(1){
        Shu* q;
        q=(S)malloc(sizeof(s));
        fread(q,sizeof(s),1,f);
        if(feof(f)){
            p->next=NULL;
            free(q);
            break;
        }
        q->next=NULL;
        p->next=q;
        p=q;
    }
    fclose(f);
    return head;
}
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

8条回答 默认 最新

  • 梵A修 2017-12-22 13:55
    已采纳
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    typedef struct Shu{//存储站名序号// 
        char name[10];
        int x;
        struct Shu *next;
    }*S,s;
    typedef struct Zhan{
        char name[10];
        int sum;//存储到下一个站的距离// 
        int x;//标记序号// 
        struct Zhan *next;
    }*Z,z;
    typedef struct Lu{
        char name[10];
        struct Lu *next;
    }*L,l;
    bool meue3();
    struct Lu *Read1();
    struct Shu *Read2();
    struct Zhan *Read3(char *);
    void BC1(L *);
    void BC2(S *);
    void BC3(Z *,char *);
    void Zhan_add(L *,S *);
    void Zhan_xg(L *,S *);
    void Zhan_shan(L *);
    void put(L *,char *,Z *);
    int search(S *,char *);
    int main(){
         while(meue3()){
         }
         return 0;
    }
    int search(S *H2,char *N){//给站点编号//
        int n=0;
        S h2=*H2;
        n=h2->x;
        while(h2!=NULL){
            if(strcmp(N,h2->name)==0){
                return h2->x;
            }
            h2=h2->next;
        }
        n++;
        S h3=(S)malloc(sizeof(s));
        h3->next=NULL;
        strcpy(h3->name,N);
        h3->x=n;
        h3->next=(*H2);
        (*H2)=h3;
        return n;
    }
    void Zhan_add(L *H1,S *H2){
        char n[10],N[10];
        Z H3=NULL,h3;
        printf("输入要操作的公交线路\n");
        scanf("%s",N);
        put(&(*H1),N,&H3);
        if(H3!=NULL){
            h3=H3;
            while(h3->next->next!=NULL)
            h3=h3->next;//h3指向公交车倒数第二个站点// 
            Z h2=(Z)malloc(sizeof(z));
            h2->next=NULL;
            printf("\n输入要插入的站名\n");
            scanf("%s",&h2->name);
            int i; 
            h2->x=search(&(*H2),h2->name);//给站点编号// 
            h2->sum=0;
            printf("1.设置新的起点 2.设置新的终点 3.插入新站\n");
            scanf("%d",&i);
            switch(i){
                case 1:
                    printf("输入%s到%s的距离\n",h2->name,H3->name);
                    scanf("%d",&h2->sum);
                    h2->next=H3;
                    H3=h2;
                    break;
                case 2:
                    h3=h3->next;
                    printf("输入%s到%s的距离\n",h3->name,h2->name);
                    scanf("%d",&h3->sum);
                    h3->next=h2;
                    break;
                case 3:
                    Z h4=H3;
                    printf("输入要插入的位置的前面一个站点\n");
                    scanf("%s",n);
                    while(h4!=NULL){
                        if(strcmp(h4->name,n)==0){
                            printf("输入%s站到%s站的距离\n",h4->name,h2->name);
                            scanf("%d",&h4->sum);
                            printf("输入%s站到%s站的距离\n",h2->name,h4->next->name);
                            scanf("%d",&h2->sum);
                            h2->next=h4->next;
                            h4->next=h2;
                            break;
                        }
                        h4=h4->next;
                    }
                    break;
            }
            BC3(&H3,N);
            printf("成功添加站点\n");
        }
    }
    void Zhan_xg(L *H1,S *H2){
        char n[10],N[10];
        Z H3=NULL,h3,h4;
        printf("输入要操作的公交线路\n");
        scanf("%s",N);
        put(&(*H1),N,&H3);
        if(H3!=NULL){
            h3=H3;
            while(h3->next->next!=NULL)
            h3=h3->next;//h3指向公交车倒数第二个站点// 
            h4=H3;
            Z h2=(Z)malloc(sizeof(z));
            h2->next=NULL;
            printf("\n输入要修改的站点\n");
            scanf("%s",n);
            printf("输入新的站点名\n");
            scanf("%s",&h2->name);
            h2->sum=0;
            h2->x=search(&(*H2),h2->name);
            if(strcmp(h4->name,n)==0){//修改节点为头节点// 
                printf("输入%s站到%s站的距离\n",h2->name,h4->next->name);
                scanf("%d",&h2->sum);
                h2->next=H3->next;
                H3=h2;
            }
            else if(strcmp(h3->next->name,n)==0){//修改节点为尾节点// 
                printf("输入%s站到%s站的距离\n",h3->name,h2->name);
                scanf("%d",&h3->sum);
                h3->next=h2; 
            }
            else{
                while(h4!=NULL){
                    if(strcmp(h4->next->name,n)==0){
                        printf("输入%s站到%s站的距离\n",h4->name,h2->name);
                        scanf("%d",&h4->sum);
                        printf("输入%s站到%s站的距离\n",h2->name,h4->next->next->name);
                        scanf("%d",&h2->sum);
                        h2->next=h4->next->next;
                        h4->next=h2;
                        break;
                    }
                    h4=h4->next;
                }
            }
            printf("成功修改站点\n");
            BC3(&H3,N);
        }
    }
    void Zhan_shan(L *H1){
        char n[10],N[10];
        Z H2=NULL,h3,h2;
        printf("输入要操作的公交线路\n");
        scanf("%s",N);
        put(&(*H1),N,&H2);
        if(H2!=NULL){
            h3=H2;
            while(h3->next->next!=NULL)
            h3=h3->next;//h3指向公交车倒数第二个站点// 
            printf("\n输入要删除的站点:\n");
            scanf("%s",n);
            h2=H2;
            if(strcmp(h2->name,n)==0){//删除站点为头节点//
                H2=H2->next;
            }
            else if(strcmp(h3->next->name,n)==0){//删除站点为尾节点//
                h3->next=NULL;
                h3->sum=0;
            }
            else{
                while(h2!=NULL){
                    if(strcmp(h2->next->name,n)==0){
                        printf("输入%s站到%s站的距离:\n",h2->name,h2->next->next->name);
                        scanf("%d",&h2->sum);
                        h2->next=h2->next->next;
                        break;
                    }
                    h2=h2->next;
                }
            }
            BC3(&H2,N);
            printf("成功删除站点\n");
        }
    }
    bool meue3(){
        bool N=true;
        L H1;
        H1=Read1();
        H1=H1->next;
        S H2;
        H2=Read2();
        H2=H2->next;
        printf("1.站点添加  2.站点修改  3.站点删除  4.返回主页面\n");
        int n;
        scanf("%d",&n);
        switch(n){
            case 1:
                Zhan_add(&H1,&H2);
                BC2(&H2);
                break;
            case 2:
                Zhan_xg(&H1,&H2);
                BC2(&H2);
                break;
            case 3:
                Zhan_shan(&H1);
                break;
            case 4:
                N=false;
                break;
        } 
        BC1(&H1);
        return N;
    }
    void put(L *H1,char *N,Z *H2){
        L h1=*H1;
        while(h1!=NULL){
            if(strcmp(h1->name,N)==0)
            break;
            h1=h1->next;
        }
        if(h1==NULL)
        printf("没有找到该公交线\n");
        else{
            printf("%s公交路线如下:\n",N);
            (*H2)=Read3(N);
            (*H2)=(*H2)->next;
            Z h3=(*H2),h2=(*H2);
            printf("%s %d %d",h2->name,h2->sum,h2->x);
            h2=h2->next;
            if(h2!=NULL){
                while(h2!=NULL){//输出所有站// 
                    printf("->%s %d %d",h2->name,h2->sum,h2->x);
                    h2=h2->next;
                }
            }
        }
    }
    void BC3(Z *head,char *N){//保存公交路线的所有站点// 
        FILE *f;
        Z p=(*head);
        char F[200]="C:\\Users\\许和伟\\Desktop\\";
        strcat(F,N);
        char f1[10]=".txt";
        strcat(F,f1);
        f=fopen(F,"w+");
        while(p!=NULL){
            fwrite(p,sizeof(z),1,f);
            p=p->next;
        }
        fclose(f);
    }
    struct Zhan *Read3(char *N){//读取公交路线的所有站点// 
        FILE *f;
        char F[200]="C:\\Users\\许和伟\\Desktop\\";
        strcat(F,N);
        char f1[10]=".txt";
        strcat(F,f1);
        f=fopen(F,"r+");
        Z p,head;
        head=(Z)malloc(sizeof(z));
        head->next=NULL;
        p=head;
        while(1){
            Z q;
            q=(Z)malloc(sizeof(z));
            fread(q,sizeof(z),1,f);
            if(feof(f)){
                p->next=NULL;
                break;
            }
            p->next=q;
            p=q;
            p->next=NULL;
        }
        fclose(f);
        return head;
    }
    void BC2(S *head){//保存所有站点编号// 
        FILE *f;
        S p=*head;
        f=fopen("Shu.txt","w+");
        while(p!=NULL){
            fwrite(p,sizeof(s),1,f);
            p=p->next;
        }
        fclose(f);
    }
    struct Shu *Read2(){//读取所有站点编号// 
        FILE *f;
        S head,p;
        head=(S)malloc(sizeof(s));
        head->next=NULL;
        f=fopen("Shu.txt","r+");
        p=head;
        while(1){
            Shu* q;
            q=(S)malloc(sizeof(s));
            fread(q,sizeof(s),1,f);
            if(feof(f)){
                p->next=NULL;
                free(q);
                break;
            }
            q->next=NULL;
            p->next=q;
            p=q;
        }
        fclose(f);
        return head;
    }
    void BC1(L *head){//保存所有公交名字// 
        FILE *f;
        L p=(*head);
        f=fopen("LU.txt","w+");
        while(p!=NULL){
            fwrite(p,sizeof(l),1,f);
            p=p->next;
        }
        fclose(f);
    }
    struct Lu *Read1(){//读取所有公交名字//
        FILE *f;
        L head,p;
        head=(L)malloc(sizeof(l));
        head->next=NULL;
        f=fopen("LU.txt","r+");
        p=head;
        while(!feof(f)){
            L q;
            q=(L)malloc(sizeof(l));
            fread(q,sizeof(l),1,f);
            if(feof(f)){
                free(q);
                break;
            }
            p->next=q;
            p=q;
            p->next=NULL;
        }
        fclose(f);
        return head;
    }
    
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • ccy1994 2017-12-22 12:57

    打开一次文件,你一直就只读了文件开始sizeof(s)字节,所以永远读的第一个,写也是一样的道理!希望对你有帮助!

    评论
    解决 无用
    打赏 举报
  • threenewbee 2017-12-22 13:09

    f=fopen("Shu.txt","w+");
    ->
    f=fopen("Shu.txt","a+");

    评论
    解决 无用
    打赏 举报
  • bingdie5531 2017-12-22 13:18

    你这个程序不完整,有完整点的吗?我可以帮着调试一下

    评论
    解决 无用
    打赏 举报
  • 凭栏而望归云处 2017-12-22 15:10

    #include
    #include
    #include
    typedef struct Shu{//存储站名序号//
    char name[10];
    int x;
    struct Shu next;
    }*S,s;
    typedef struct Zhan{
    char name[10];
    int sum;//存储到下一个站的距离//
    int x;//标记序号//
    struct Zhan *next;
    }*Z,z;
    typedef struct Lu{
    char name[10];
    struct Lu *next;
    }*L,l;
    bool meue3();
    struct Lu *Read1();
    struct Shu *Read2();
    struct Zhan *Read3(char *);
    void BC1(L *);
    void BC2(S *);
    void BC3(Z *,char *);
    void Zhan_add(L *,S *);
    void Zhan_xg(L *,S *);
    void Zhan_shan(L *);
    void put(L *,char *,Z *);
    int search(S *,char *);
    int main(){
    while(meue3()){
    }
    return 0;
    }
    int search(S *H2,char *N){//给站点编号//
    int n=0;
    S h2=*H2;
    n=h2->x;
    while(h2!=NULL){
    if(strcmp(N,h2->name)==0){
    return h2->x;
    }
    h2=h2->next;
    }
    n++;
    S h3=(S)malloc(sizeof(s));
    h3->next=NULL;
    strcpy(h3->name,N);
    h3->x=n;
    h3->next=(*H2);
    (*H2)=h3;
    return n;
    }
    void Zhan_add(L *H1,S *H2){
    char n[10],N[10];
    Z H3=NULL,h3;
    printf("输入要操作的公交线路\n");
    scanf("%s",N);
    put(&(*H1),N,&H3);
    if(H3!=NULL){
    h3=H3;
    while(h3->next->next!=NULL)
    h3=h3->next;//h3指向公交车倒数第二个站点//
    Z h2=(Z)malloc(sizeof(z));
    h2->next=NULL;
    printf("\n输入要插入的站名\n");
    scanf("%s",&h2->name);
    int i;
    h2->x=search(&(*H2),h2->name);//给站点编号//
    h2->sum=0;
    printf("1.设置新的起点 2.设置新的终点 3.插入新站\n");
    scanf("%d",&i);
    switch(i){
    case 1:
    printf("输入%s到%s的距离\n",h2->name,H3->name);
    scanf("%d",&h2->sum);
    h2->next=H3;
    H3=h2;
    break;
    case 2:
    h3=h3->next;
    printf("输入%s到%s的距离\n",h3->name,h2->name);
    scanf("%d",&h3->sum);
    h3->next=h2;
    break;
    case 3:
    Z h4=H3;
    printf("输入要插入的位置的前面一个站点\n");
    scanf("%s",n);
    while(h4!=NULL){
    if(strcmp(h4->name,n)==0){
    printf("输入%s站到%s站的距离\n",h4->name,h2->name);
    scanf("%d",&h4->sum);
    printf("输入%s站到%s站的距离\n",h2->name,h4->next->name);
    scanf("%d",&h2->sum);
    h2->next=h4->next;
    h4->next=h2;
    break;
    }
    h4=h4->next;
    }
    break;
    }
    BC3(&H3,N);
    printf("成功添加站点\n");
    }
    }
    void Zhan_xg(L *H1,S *H2){
    char n[10],N[10];
    Z H3=NULL,h3,h4;
    printf("输入要操作的公交线路\n");
    scanf("%s",N);
    put(&(*H1),N,&H3);
    if(H3!=NULL){
    h3=H3;
    while(h3->next->next!=NULL)
    h3=h3->next;//h3指向公交车倒数第二个站点//
    h4=H3;
    Z h2=(Z)malloc(sizeof(z));
    h2->next=NULL;
    printf("\n输入要修改的站点\n");
    scanf("%s",n);
    printf("输入新的站点名\n");
    scanf("%s",&h2->name);
    h2->sum=0;
    h2->x=search(&(*H2),h2->name);
    if(strcmp(h4->name,n)==0){//修改节点为头节点//
    printf("输入%s站到%s站的距离\n",h2->name,h4->next->name);
    scanf("%d",&h2->sum);
    h2->next=H3->next;
    H3=h2;
    }
    else if(strcmp(h3->next->name,n)==0){//修改节点为尾节点//
    printf("输入%s站到%s站的距离\n",h3->name,h2->name);
    scanf("%d",&h3->sum);
    h3->next=h2;
    }
    else{
    while(h4!=NULL){
    if(strcmp(h4->next->name,n)==0){
    printf("输入%s站到%s站的距离\n",h4->name,h2->name);
    scanf("%d",&h4->sum);
    printf("输入%s站到%s站的距离\n",h2->name,h4->next->next->name);
    scanf("%d",&h2->sum);
    h2->next=h4->next->next;
    h4->next=h2;
    break;
    }
    h4=h4->next;
    }
    }
    printf("成功修改站点\n");
    BC3(&H3,N);
    }
    }
    void Zhan_shan(L *H1){
    char n[10],N[10];
    Z H2=NULL,h3,h2;
    printf("输入要操作的公交线路\n");
    scanf("%s",N);
    put(&(*H1),N,&H2);
    if(H2!=NULL){
    h3=H2;
    while(h3->next->next!=NULL)
    h3=h3->next;//h3指向公交车倒数第二个站点//
    printf("\n输入要删除的站点:\n");
    scanf("%s",n);
    h2=H2;
    if(strcmp(h2->name,n)==0){//删除站点为头节点//
    H2=H2->next;
    }
    else if(strcmp(h3->next->name,n)==0){//删除站点为尾节点//
    h3->next=NULL;
    h3->sum=0;
    }
    else{
    while(h2!=NULL){
    if(strcmp(h2->next->name,n)==0){
    printf("输入%s站到%s站的距离:\n",h2->name,h2->next->next->name);
    scanf("%d",&h2->sum);
    h2->next=h2->next->next;
    break;
    }
    h2=h2->next;
    }
    }
    BC3(&H2,N);
    printf("成功删除站点\n");
    }
    }
    bool meue3(){
    bool N=true;
    L H1;
    H1=Read1();
    H1=H1->next;
    S H2;
    H2=Read2();
    H2=H2->next;
    printf("1.站点添加 2.站点修改 3.站点删除 4.返回主页面\n");
    int n;
    scanf("%d",&n);
    switch(n){
    case 1:
    Zhan_add(&H1,&H2);
    BC2(&H2);
    break;
    case 2:
    Zhan_xg(&H1,&H2);
    BC2(&H2);
    break;
    case 3:
    Zhan_shan(&H1);
    break;
    case 4:
    N=false;
    break;
    }
    BC1(&H1);
    return N;
    }
    void put(L *H1,char *N,Z *H2){
    L h1=*H1;
    while(h1!=NULL){
    if(strcmp(h1->name,N)==0)
    break;
    h1=h1->next;
    }
    if(h1==NULL)
    printf("没有找到该公交线\n");
    else{
    printf("%s公交路线如下:\n",N);
    (*H2)=Read3(N);
    (*H2)=(*H2)->next;
    Z h3=(*H2),h2=(*H2);
    printf("%s %d %d",h2->name,h2->sum,h2->x);
    h2=h2->next;
    if(h2!=NULL){
    while(h2!=NULL){//输出所有站//
    printf("->%s %d %d",h2->name,h2->sum,h2->x);
    h2=h2->next;
    }
    }
    }
    }
    void BC3(Z *head,char *N){//保存公交路线的所有站点//
    FILE *f;
    Z p=(*head);
    char F[200]="C:\Users\许和伟\Desktop\";
    strcat(F,N);
    char f1[10]=".txt";
    strcat(F,f1);
    f=fopen(F,"w+");
    while(p!=NULL){
    fwrite(p,sizeof(z),1,f);
    p=p->next;
    }
    fclose(f);
    }
    struct Zhan *Read3(char *N){//读取公交路线的所有站点//
    FILE *f;
    char F[200]="C:\Users\许和伟\Desktop\";
    strcat(F,N);
    char f1[10]=".txt";
    strcat(F,f1);
    f=fopen(F,"r+");
    Z p,head;
    head=(Z)malloc(sizeof(z));
    head->next=NULL;
    p=head;
    while(1){
    Z q;
    q=(Z)malloc(sizeof(z));
    fread(q,sizeof(z),1,f);
    if(feof(f)){
    p->next=NULL;
    break;
    }
    p->next=q;
    p=q;
    p->next=NULL;
    }
    fclose(f);
    return head;
    }
    void BC2(S *head){//保存所有站点编号//
    FILE *f;
    S p=*head;
    f=fopen("Shu.txt","w+");
    while(p!=NULL){
    fwrite(p,sizeof(s),1,f);
    p=p->next;
    }
    fclose(f);
    }
    struct Shu *Read2(){//读取所有站点编号//
    FILE *f;
    S head,p;
    head=(S)malloc(sizeof(s));
    head->next=NULL;
    f=fopen("Shu.txt","r+");
    p=head;
    while(1){
    Shu
    q;
    q=(S)malloc(sizeof(s));
    fread(q,sizeof(s),1,f);
    if(feof(f)){
    p->next=NULL;
    free(q);
    break;
    }
    q->next=NULL;
    p->next=q;
    p=q;
    }
    fclose(f);
    return head;
    }
    void BC1(L *head){//保存所有公交名字//
    FILE *f;
    L p=(*head);
    f=fopen("LU.txt","w+");
    while(p!=NULL){
    fwrite(p,sizeof(l),1,f);
    p=p->next;
    }
    fclose(f);
    }
    struct Lu *Read1(){//读取所有公交名字//
    FILE *f;
    L head,p;
    head=(L)malloc(sizeof(l));
    head->next=NULL;
    f=fopen("LU.txt","r+");
    p=head;
    while(!feof(f)){
    L q;
    q=(L)malloc(sizeof(l));
    fread(q,sizeof(l),1,f);
    if(feof(f)){
    free(q);
    break;
    }
    p->next=q;
    p=q;
    p->next=NULL;
    }
    fclose(f);
    return head;
    }

    评论
    解决 无用
    打赏 举报
  • ccjjtt20 2017-12-23 02:15

    #include
    #include
    #include
    typedef struct Shu{//存储站名序号//
    char name[10];
    int x;
    struct Shu next;
    }*S,s;
    typedef struct Zhan{
    char name[10];
    int sum;//存储到下一个站的距离//
    int x;//标记序号//
    struct Zhan *next;
    }*Z,z;
    typedef struct Lu{
    char name[10];
    struct Lu *next;
    }*L,l;
    bool meue3();
    struct Lu *Read1();
    struct Shu *Read2();
    struct Zhan *Read3(char *);
    void BC1(L *);
    void BC2(S *);
    void BC3(Z *,char *);
    void Zhan_add(L *,S *);
    void Zhan_xg(L *,S *);
    void Zhan_shan(L *);
    void put(L *,char *,Z *);
    int search(S *,char *);
    int main(){
    while(meue3()){
    }
    return 0;
    }
    int search(S *H2,char *N){//给站点编号//
    int n=0;
    S h2=*H2;
    n=h2->x;
    while(h2!=NULL){
    if(strcmp(N,h2->name)==0){
    return h2->x;
    }
    h2=h2->next;
    }
    n++;
    S h3=(S)malloc(sizeof(s));
    h3->next=NULL;
    strcpy(h3->name,N);
    h3->x=n;
    h3->next=(*H2);
    (*H2)=h3;
    return n;
    }
    void Zhan_add(L *H1,S *H2){
    char n[10],N[10];
    Z H3=NULL,h3;
    printf("输入要操作的公交线路\n");
    scanf("%s",N);
    put(&(*H1),N,&H3);
    if(H3!=NULL){
    h3=H3;
    while(h3->next->next!=NULL)
    h3=h3->next;//h3指向公交车倒数第二个站点//
    Z h2=(Z)malloc(sizeof(z));
    h2->next=NULL;
    printf("\n输入要插入的站名\n");
    scanf("%s",&h2->name);
    int i;
    h2->x=search(&(*H2),h2->name);//给站点编号//
    h2->sum=0;
    printf("1.设置新的起点 2.设置新的终点 3.插入新站\n");
    scanf("%d",&i);
    switch(i){
    case 1:
    printf("输入%s到%s的距离\n",h2->name,H3->name);
    scanf("%d",&h2->sum);
    h2->next=H3;
    H3=h2;
    break;
    case 2:
    h3=h3->next;
    printf("输入%s到%s的距离\n",h3->name,h2->name);
    scanf("%d",&h3->sum);
    h3->next=h2;
    break;
    case 3:
    Z h4=H3;
    printf("输入要插入的位置的前面一个站点\n");
    scanf("%s",n);
    while(h4!=NULL){
    if(strcmp(h4->name,n)==0){
    printf("输入%s站到%s站的距离\n",h4->name,h2->name);
    scanf("%d",&h4->sum);
    printf("输入%s站到%s站的距离\n",h2->name,h4->next->name);
    scanf("%d",&h2->sum);
    h2->next=h4->next;
    h4->next=h2;
    break;
    }
    h4=h4->next;
    }
    break;
    }
    BC3(&H3,N);
    printf("成功添加站点\n");
    }
    }
    void Zhan_xg(L *H1,S *H2){
    char n[10],N[10];
    Z H3=NULL,h3,h4;
    printf("输入要操作的公交线路\n");
    scanf("%s",N);
    put(&(*H1),N,&H3);
    if(H3!=NULL){
    h3=H3;
    while(h3->next->next!=NULL)
    h3=h3->next;//h3指向公交车倒数第二个站点//
    h4=H3;
    Z h2=(Z)malloc(sizeof(z));
    h2->next=NULL;
    printf("\n输入要修改的站点\n");
    scanf("%s",n);
    printf("输入新的站点名\n");
    scanf("%s",&h2->name);
    h2->sum=0;
    h2->x=search(&(*H2),h2->name);
    if(strcmp(h4->name,n)==0){//修改节点为头节点//
    printf("输入%s站到%s站的距离\n",h2->name,h4->next->name);
    scanf("%d",&h2->sum);
    h2->next=H3->next;
    H3=h2;
    }
    else if(strcmp(h3->next->name,n)==0){//修改节点为尾节点//
    printf("输入%s站到%s站的距离\n",h3->name,h2->name);
    scanf("%d",&h3->sum);
    h3->next=h2;
    }
    else{
    while(h4!=NULL){
    if(strcmp(h4->next->name,n)==0){
    printf("输入%s站到%s站的距离\n",h4->name,h2->name);
    scanf("%d",&h4->sum);
    printf("输入%s站到%s站的距离\n",h2->name,h4->next->next->name);
    scanf("%d",&h2->sum);
    h2->next=h4->next->next;
    h4->next=h2;
    break;
    }
    h4=h4->next;
    }
    }
    printf("成功修改站点\n");
    BC3(&H3,N);
    }
    }
    void Zhan_shan(L *H1){
    char n[10],N[10];
    Z H2=NULL,h3,h2;
    printf("输入要操作的公交线路\n");
    scanf("%s",N);
    put(&(*H1),N,&H2);
    if(H2!=NULL){
    h3=H2;
    while(h3->next->next!=NULL)
    h3=h3->next;//h3指向公交车倒数第二个站点//
    printf("\n输入要删除的站点:\n");
    scanf("%s",n);
    h2=H2;
    if(strcmp(h2->name,n)==0){//删除站点为头节点//
    H2=H2->next;
    }
    else if(strcmp(h3->next->name,n)==0){//删除站点为尾节点//
    h3->next=NULL;
    h3->sum=0;
    }
    else{
    while(h2!=NULL){
    if(strcmp(h2->next->name,n)==0){
    printf("输入%s站到%s站的距离:\n",h2->name,h2->next->next->name);
    scanf("%d",&h2->sum);
    h2->next=h2->next->next;
    break;
    }
    h2=h2->next;
    }
    }
    BC3(&H2,N);
    printf("成功删除站点\n");
    }
    }
    bool meue3(){
    bool N=true;
    L H1;
    H1=Read1();
    H1=H1->next;
    S H2;
    H2=Read2();
    H2=H2->next;
    printf("1.站点添加 2.站点修改 3.站点删除 4.返回主页面\n");
    int n;
    scanf("%d",&n);
    switch(n){
    case 1:
    Zhan_add(&H1,&H2);
    BC2(&H2);
    break;
    case 2:
    Zhan_xg(&H1,&H2);
    BC2(&H2);
    break;
    case 3:
    Zhan_shan(&H1);
    break;
    case 4:
    N=false;
    break;
    }
    BC1(&H1);
    return N;
    }
    void put(L *H1,char *N,Z *H2){
    L h1=*H1;
    while(h1!=NULL){
    if(strcmp(h1->name,N)==0)
    break;
    h1=h1->next;
    }
    if(h1==NULL)
    printf("没有找到该公交线\n");
    else{
    printf("%s公交路线如下:\n",N);
    (*H2)=Read3(N);
    (*H2)=(*H2)->next;
    Z h3=(*H2),h2=(*H2);
    printf("%s %d %d",h2->name,h2->sum,h2->x);
    h2=h2->next;
    if(h2!=NULL){
    while(h2!=NULL){//输出所有站//
    printf("->%s %d %d",h2->name,h2->sum,h2->x);
    h2=h2->next;
    }
    }
    }
    }
    void BC3(Z *head,char *N){//保存公交路线的所有站点//
    FILE *f;
    Z p=(*head);
    char F[200]="C:\Users\许和伟\Desktop\";
    strcat(F,N);
    char f1[10]=".txt";
    strcat(F,f1);
    f=fopen(F,"w+");
    while(p!=NULL){
    fwrite(p,sizeof(z),1,f);
    p=p->next;
    }
    fclose(f);
    }
    struct Zhan *Read3(char *N){//读取公交路线的所有站点//
    FILE *f;
    char F[200]="C:\Users\许和伟\Desktop\";
    strcat(F,N);
    char f1[10]=".txt";
    strcat(F,f1);
    f=fopen(F,"r+");
    Z p,head;
    head=(Z)malloc(sizeof(z));
    head->next=NULL;
    p=head;
    while(1){
    Z q;
    q=(Z)malloc(sizeof(z));
    fread(q,sizeof(z),1,f);
    if(feof(f)){
    p->next=NULL;
    break;
    }
    p->next=q;
    p=q;
    p->next=NULL;
    }
    fclose(f);
    return head;
    }
    void BC2(S *head){//保存所有站点编号//
    FILE *f;
    S p=*head;
    f=fopen("Shu.txt","w+");
    while(p!=NULL){
    fwrite(p,sizeof(s),1,f);
    p=p->next;
    }
    fclose(f);
    }
    struct Shu *Read2(){//读取所有站点编号//
    FILE *f;
    S head,p;
    head=(S)malloc(sizeof(s));
    head->next=NULL;
    f=fopen("Shu.txt","r+");

    评论
    解决 无用
    打赏 举报
  • qq601707399 2017-12-23 03:36

    这个问题其实很简单,我直接上代码吧,记得给一个分
    //获取文件指针
    FILE *pFile = fopen("1.txt", //打开文件的名称
    "w"); // 文件打开方式 如果原来有内容也会销毁
    //向文件写数据
    fwrite ("hello", //要输入的文字
    1,//文字每一项的大小 以为这里是字符型的 就设置为1 如果是汉字就设置为4
    strlog("hello"), //单元个数 我们也可以直接写5
    pFile //我们刚刚获得到的地址
    );
    //fclose(pFile); //告诉系统我们文件写完了数据更新,但是我们要要重新打开才能在写
    fflush(pFile); //数据刷新 数据立即更新

    评论
    解决 无用
    打赏 举报
  • qq_40435081 2018-01-09 09:23

    运行的时候没有出错的话就是sizeof(s)出错了,改一下就好

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题