| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 640 人关注过本帖
标题:为什么我的程序执行后保存到文件后(比如我在文件中输入20个数,在键盘上输 ...
只看楼主 加入收藏
zc1992312
Rank: 2
等 级:论坛游民
帖 子:43
专家分:12
注 册:2013-3-20
结帖率:57.14%
收藏
已结贴  问题点数:20 回复次数:4 
为什么我的程序执行后保存到文件后(比如我在文件中输入20个数,在键盘上输入n为15),文件中后面的5个数就乱了?
#include <stdio.h>
#include <stdlib.h>

#define FALSE -1
#define TURE  0
#define Max_size 30
#define LIST_INIT_SIZE 10
typedef int ElemType;

typedef struct
{
     ElemType pBase[Max_size];
    int nLength;
    int nSize;         
}SqList;

int InitList(SqList *pList)    /*线性表初始化*/
{
     pList = (SqList *) malloc(sizeof (SqList));
     pList -> nLength = 0;
     pList -> nSize = Max_size;
     return TURE;      
}

void DestroyList(SqList *pList)        /*销毁线性表*/
{
      free(pList);      
}

int ListLength(SqList *pList)        /*线性表的长度*/
{
      return (pList -> nLength);     
}

int ListEmpty(SqList *pList)        /*判断线性表是否为空*/
{
      if (pList -> nLength == 0)
          return FALSE;
      else
         return TURE;      
}

int ShowList(SqList *pList)        /*输出线性表*/
{
     int pos, n = 0;
     if (ListEmpty (pList))
        return FALSE;
     for (pos = 0; pos < pList -> nLength; pos++)
     {
        printf ("%d  ", pList -> pBase[pos]);
        n++;
        if (n % 5 == 0)
            printf ("\n");
     }
     
     printf ("\n");     
}

int GetElem(SqList *pList, int pos, int e)        /*求线性表中某个元素的值*/
{
     if (pos < 1 || pos > pList -> nLength)
         return FALSE;
     else
         e = pList -> pBase [pos - 1];
         
     return TURE;
         
}

int LocateElem(SqList *pList, int e)        /*查找元素e*/
{
      int pos;
      for (pos = 0; pos < pList -> nLength && pList -> pBase[pos] != e;)
           pos++;
          if (pos >= pList -> nLength)
              return FALSE;
          else
             return (pos + 1);      
}

int ListDelete(SqList *pList, int pos, int e)            /*删除数据元素*/
{
      int j;
     if (pos < 1 || pos > pList -> nLength)
          return FALSE;
     pos--;
     e = pList -> pBase[pos];
     
     for (j = pos; j <= pList -> nLength - 1; ++j)
     {
           pList -> pBase[j - 1] = pList -> pBase[j];      
     }
      
     --pList -> nLength;
     return TURE;     
}

int ListInsert(SqList *pList, int pos, int e)            /*插入数据元素*/
{
      int j;
     if (pos < 1 || pos > pList -> nLength)
         return FALSE;
     pos--;
     
     for (j = pList -> nLength; j > pos; j--)
     {
           pList -> pBase[j] = pList -> pBase[j - 1];      
     }
     
     pList -> pBase[pos] = e;
     ++pList -> nLength;
     return TURE;
}

int ReadList(SqList *pList,int n)        /*从文件读入n(n > 10)个整数,填入顺序表*/
{
     int i;
     FILE * fp = fopen("list.txt","rw");
     
     if (fp == NULL)
     {
           printf ("错误!无法读入文件!");
         exit(0);         
     }
     
     for(i = 0; i < n; i++)
        fscanf(fp,"%d ",&pList -> pBase[i]);

     pList -> nLength = n;
     fclose(fp);
     return TURE;
}

int SaveList(SqList *pList)            /*将线性表保存到文件中*/
{
     FILE *fp;
     int i;
     fp=fopen("list.txt","r+");
     
     for(i = 0;i < pList -> nLength; i++)
       fprintf(fp,"%d ", pList -> pBase[i]);
     
     fclose(fp);
     return TURE;
}

int cShowList(SqList *pList)
{
     int pos, n, t = 0;
     if (ListEmpty (pList))
        return FALSE;
     for (pos = 0; pos < (pList -> nLength) / 2; pos++)
     {
        n = pList -> pBase[pos];
        pList -> pBase[pos] = pList -> pBase[pList -> nLength - pos - 1];
        pList -> pBase[pList -> nLength - pos - 1] = n;
     }
   
     for (pos = 0; pos < pList -> nLength; pos++)
     {
          t++;
          printf ("%d  ", pList -> pBase[pos]);
          if (t % 5 == 0)
             printf ("\n");
            
    }        
}

int main()
{     int n, e1, e2;
      SqList pList;
      printf ("(1).初始化线性表\n");
      InitList (&pList);
      
      printf ("请输入n的值:  ");
      scanf ("%d", &n);
      ReadList(&pList, n);
      printf ("\n");
      
      printf ("(2).输出%d个数至屏幕\n", n);
      ShowList(&pList);
      printf ("\n");
      
      printf ("(3).查找元素的位序\n");
      printf ("元素7的位序为%d\n", LocateElem(&pList, 7));
      printf ("\n");
      
      printf ("(4).在顺序表头、中、尾各插一个元素\n");
      ListInsert(&pList, 1, 1);
      ListInsert(&pList, 9, 8);
      ListInsert(&pList, 17, 15);
      ShowList(&pList);
      printf ("\n");
      
      printf ("(5).输入修改后的表至屏幕,并保存至文件\n");
      ShowList(&pList);
      SaveList(&pList);
      printf ("\n");
      
      printf ("(6).删除顺序表的任意两个元素\n");
      ListDelete(&pList, 5, e1);
      ListDelete(&pList, 11, e2);
      ShowList(&pList);
      SaveList(&pList);
      printf ("\n");
      
      printf ("(7).输入修改后的表至屏幕,并保存至文件\n");
      ShowList(&pList);
      SaveList(&pList);
      printf ("\n");
      
      printf ("(8).逆序排列顺序表,输出至屏幕,并保存至文件\n");
      cShowList(&pList);
      SaveList(&pList);
      
      DestroyList(&pList);
      printf ("\n");
      
      system("pause");
            
}
搜索更多相关主题的帖子: include return 线性表 键盘 
2013-09-22 22:44
yuccn
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:何方
等 级:版主
威 望:167
帖 子:6815
专家分:42393
注 册:2010-12-16
收藏
得分:0 
int InitList(SqList *pList)    /*线性表初始化*/
{
     pList = (SqList *) malloc(sizeof (SqList));
     pList -> nLength = 0;
     pList -> nSize = Max_size;
     return TURE;      
}

SqList pList;
InitList (&pList);

这样初始化就已经错了,你还不懂指针。同时对形参实参了解也不够深刻。有时间补下基础

SqList pList; 已经是申请了一个栈的内存了的。你在初始化它,就把它清空等操作就行了把。而你的init中又申请了一块内存,而这个内存并没有带出来,注定是内存泄漏了的,你别指望SqList *pList 参数能帮你带这块申请的内存出来(这就是不理解形参和实参的表现)。如果想带出来,要得用指针的指针了。
估计你搞错了create和init了

我行我乐
公众号:逻辑客栈
我的博客:
https://blog.yuccn. net
2013-09-23 08:54
yuccn
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:何方
等 级:版主
威 望:167
帖 子:6815
专家分:42393
注 册:2010-12-16
收藏
得分:20 
int ShowList(SqList *pList)        /*输出线性表*/
{
     int pos, n = 0;
     if (ListEmpty (pList))
        return FALSE;
     for (pos = 0; pos < pList -> nLength; pos++)
     {
        printf ("%d  ", pList -> pBase[pos]);
        n++;
        if (n % 5 == 0)
            printf ("\n");
     }
     
     printf ("\n");     
}

这个函数,不是每个分支有返回值
int cShowList(SqList *pList) 也一样


==================================================
int GetElem(SqList *pList, int pos, int e)        /*求线性表中某个元素的值*/
{
     if (pos < 1 || pos > pList -> nLength)
         return FALSE;
     else
         e = pList -> pBase [pos - 1];
         
     return TURE;
}

e带不出来的,理解下形参和实参

[ 本帖最后由 yuccn 于 2013-9-23 08:58 编辑 ]

我行我乐
公众号:逻辑客栈
我的博客:
https://blog.yuccn. net
2013-09-23 08:55
zc1992312
Rank: 2
等 级:论坛游民
帖 子:43
专家分:12
注 册:2013-3-20
收藏
得分:0 
回复 2楼 yuccn
非常谢谢你这样解释,  虽然不是很懂, 还是谢谢!!!
2013-09-23 22:13
zc1992312
Rank: 2
等 级:论坛游民
帖 子:43
专家分:12
注 册:2013-3-20
收藏
得分:0 
回复 3楼 yuccn
嗯 。。。  这个明白,, 谢谢
2013-09-23 22:13
快速回复:为什么我的程序执行后保存到文件后(比如我在文件中输入20个数,在键盘 ...
数据加载中...
 
   



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

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