How do I read and store string of arbitrary length using malloc and realloc in C?

后端 未结 4 1440
生来不讨喜
生来不讨喜 2021-01-22 03:53

I have a structure

typedef struct store
{
   char name[11];
    int age;
} store;

and a main function(below is part of it):

int         


        
相关标签:
4条回答
  • 2021-01-22 04:11

    What you need to do is read the line in smaller increments, and resize your buffer as you go.

    As an example (not tested and not meaning to be particularly elegant, just an example):

    char *readline(FILE *f)
    {
       char *buf = NULL;
       size_t bufsz = 0, len = 0;
       int keep_going = 1;
    
       while (keep_going)
       {
          int c = fgetc(f);
          if (c == EOF || c == '\n')
          {
             c = 0;             // we'll add zero terminator
             keep_going = 0;    // and terminate the loop afterwards
          }
    
          if (bufsz == len)
          {
             // time to resize the buffer.
             //
             void *newbuf = NULL;
             if (!buf)
             {
                bufsz = 512; // some arbitrary starting size.
                newbuf = malloc(bufsz);
             }
             else
             {
                bufsz *= 2; // issue - ideally you'd check for overflow here.
                newbuf = realloc(buf, bufsz);
             }
    
             if (!newbuf)
             {
                // Allocation failure.  Free old buffer (if any) and bail.
                //
                free(buf);
                buf = NULL;
                break;
             }
    
             buf = newbuf;
          }
    
          buf[len++] = c;
       }
    
       return buf;
    }
    
    0 讨论(0)
  • 2021-01-22 04:14

    Change the name[11] to *name; Allocate memory for that everytime using malloc.

    By the way, register is a keyword in C language. You can't use it like you did !

    0 讨论(0)
  • 2021-01-22 04:27

    I think what you're looking for is:

    char* name;
    
    name = (char*)malloc(sizeof(char));
    
    0 讨论(0)
  • 2021-01-22 04:30

    This alternative approach is similar to @asveikau's, but economize on the use of malloc() by copying on the stack.

    Please do not use this for homework answer.

    #include <string.h>
    #include <stdlib.h>
    #include <stdio.h>
    char * slurponeline(FILE *f, int s) {
     const int size = 4096;
     char buffer[size];
     char * r;
     int c,i=0;
     while( i<size && (c = fgetc(f),c>=0 && c!='\n')) buffer[i++]=c;
     if (0 == s && 0 == i) return 0;
     r = (size==i)? slurponeline(f,s+size):malloc(s+i);
     memcpy(r+s,buffer,i);
     return r;
    }
    int main(int argc, char ** argv) {
     FILE * f = fopen(argc>1?argv[1]:"a.out","rb");
     char * a,*command,*commandend,*name,*nameend;
     int age;
     while (a = slurponeline(f,0)) {
      char * p = a;
      while (*p && *p == ' ') ++p; // skip blanks.
      command = p;
      while (*p && *p != ' ') ++p; // skip non-blanks.
      commandend = p;
      while (*p && *p == ' ') ++p; // skip blanks.
      name = p;
      while (*p && *p != ' ') ++p; // skip non-blanks.
      nameend = p;
      while (*p && *p == ' ') ++p; // skip blanks.
      age = atoi(p);
      *commandend=0;
      *nameend=0;
      printf("command: %s, name: %s, age: %d\n",command,name,age);
      free(a);
     }
    }
    
    0 讨论(0)
提交回复
热议问题