在c语言中如何实现数组的动态输入?

在c语言中如何实现数组的动态输入?,第1张

文件malloc.h

使用malloc来申请一个初始地址空间。

然后在循环输入的过程中不断检查初始空间是否已满,满了就是使用realloc来扩展地址空间。

最后,如申请的地址不需要使用了,且程序没有结束,需要用free来释放。

另外,使用malloc或realloc申请时,需要先判断下返回值是否为空,如有异常申请失败,用空指针直接使用,会造成程序错误。

下面简单示范:(初始申请2个字节,之后每次输入字符扩展1个字节,回车结束输入)

#include <stdio.h>

#include <malloc.h>

int main()

{

  int len=2

  char *a=NULL,*aSave=NULL,c

  a=(char*)malloc(sizeof(char)*len)

  if(!a)

      return 1

  a[0]=0

  while(1)

  {

      c=getchar()

      if(c==10)

          break

      if(a[0]==0)

          a[0]=c,a[1]=0

      else

      {

          aSave=realloc(a,sizeof(char)*len)

          if(!aSave)

              return 1

          a=aSave

          a[len-2]=c,a[len-1]=0

      }

      len++

  }

  printf("输入的字符串数组是:\n%s\n",a)

  free(a)

  return 0

}

struct student{

char name

int number

struct student *next

}

这样你定义了三个字段,姓名,number

我不知道你为什么这么定义,如果是我可能这么定义

struct student {

char name/*学生姓名*/

int 性别/*1代表femail (女性), 0 代表mail(男性)*/

int age

struct student *next/*为了用链表实现而采用*/

};

这样完全可以实现你需要的数据类型.只需要再加上一些算法就可以了.

如果还有什么问题可以与我联系.

一般工业上都会使用 typedef 来定义公司内部的统一定义如

typedef struct student {

}

使用这几个函数。

int fseek (FILE *stream,long offset,int fromwhere)

long ftell(FILE *stream)

int rewind(FILE *stream)

可以实现在一个文件的任意位置添加信息。但除了在文件尾添加信息之外,你写将要插入位置后面的文件内容读到缓冲,然后定位文件偏移量插入数据,在将读出来的写入

因为文件在磁盘上是连续存放的,不可能说直接在中间插入内容而不覆盖原来的,别的语言提供的都是自己按照上述思路写的。

例程:

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <conio.h>

#include <Windows.h>

/* 读出文件放到数组中,新增数据插入到该数组中;

   重新将数组中的数据写入该文件中

*/

void main(int argc,char * agrv)

{

    FILE *fp

    char name[20]       //输入变量

    int sum             //输入变量

    char fName[10][20]  //可存储10个人名

    int fScore[10]      //存储10个分数记录

    char buff1[20]

    char buff2[20]

    int i=0

    //打开存储文件

    if ((fp=fopen("c:\\scorelist.txt","r"))==NULL)

    {

        printf("Can not open the file")

        getch()

        exit(0)

    } 

    else

    {   

        while (!feof(fp))

        {   

            ZeroMemory(buff1,sizeof(buff1))       //清空内存

            ZeroMemory(buff2,sizeof(buff1))

            fgets(buff1,sizeof(buff1),fp)         //读取名称

            fgets(buff2,sizeof(buff2),fp)         //读取第二行分数

            if (strcmp(buff1,"")==0)

            {

                continue

            }

            else

            {

                strcpy(fName[i],buff1) 

                printf("%s",fName[i])                 //输出名称

                fScore[i] = atoi(buff2)                //将字符型转换成int型

                printf("%i\n",fScore[i])              //打印输出分数值

            }

            i++

        }

    }

    fclose(fp)

    //打开存储文件,将排好序的数据重新写入文件中

    if ((fp=fopen("c:\\scorelist.txt","w"))==NULL)

    {

        printf("Can not open the file")

        getch()

        exit(0)

    } 

    else

    {   

        printf("Input the new name:\n")

        scanf("%s",name)

        printf("Input the new score:\n")

        scanf("%i",&sum)

        int j =0

        //获取新增积分排序位置

        while(sum < fScore[j])

        {

             j++

        

        }

        //移动数据重新对数组排序,从后往前排序

        int m = i

        while (i>j)

        {

            strcpy(fName[i],fName[i-1])

            fScore[i] = fScore[i-1]

            i--

        }

        strcpy(fName[j],name)

        strcat(fName[j],"\n")

        fScore[j] = sum

        //写入文本文件

        int k=0

        while(k<=m)

        {

            fputs(fName[k],fp)

            fprintf(fp,"%i\n",fScore[k])

            k++

        }

    }    

    fclose(fp)

}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/bake/11751950.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-18
下一篇 2023-05-18

发表评论

登录后才能评论

评论列表(0条)

保存