14204

Input of dynamic size using fgets in C

Question:

I want to have a string(can contain spaces) as input. I want to do dynamic allocation. Structure of my program is somewhat like this.

#include<stdio.h> #include<string.h> #include<stdlib.h> typedef struct msgclient { int msglen; int msgtype; char *cp; }M1; int main() { M1 *m; m=malloc(sizeof(M1)); m->msglen=5; m->msgtype=6; printf("Enter\t"); fgets(m->cp,50,stdin); //here // m->cp[strlen(m->cp)]='\0'; printf("\n%d\n%d\n",m->msglen,m->msgtype); fputs(m->cp,stdout); return 0; }

I want to know how to get input. Is there any way that the second argument of fgets be dynamic?

Answer1:

Use <a href="http://man7.org/linux/man-pages/man3/getline.3.html" rel="nofollow">getline(3)</a> -instead of <a href="http://man7.org/linux/man-pages/man3/fgets.3.html" rel="nofollow">fgets(3)</a>- which reads a dynamically allocated line.

typedef struct msgclient { ssize_t msglen; int msgtype; char *cp; }M1;

then in your main function

M1 *m; m=malloc(sizeof(M1)); if (!m) { perror("malloc"); exit(EXIT_FAILURE); }; m->msglen=0; m->msgtype=6; m->cp = NULL; printf("Enter\t"); fflush(stdout); size_t msgsize = 0; m->msglen = getline(&msg->cp, &msgsize, stdin);

you might consider adding the allocated size of the buffer (i.e. msgsize) as an additional field of struct msgclient

<h3>addenda:</h3>

Notice that you might perhaps consider using <a href="http://www.gnu.org/software/readline/" rel="nofollow">GNU readline</a>. It offers edition and completion facilities (when reading from a terminal).

Recommend

  • Send Byte array plus String over socket Java server to c++ client
  • Reset again and persist the data of JqGrid Row in Editable mode when some error return from Server
  • Client/server sockets in c
  • afplay Error: AudioFileOpen failed (-43)
  • How to call RESTful WCF-Service from PHP
  • SQLite not storing blobs containing \\0 bytes
  • Apache - Configuring mod_auth_sspi.so
  • Annoying tick with speex [closed]
  • Frama-C anagram function behavior verification
  • How to Send a structure using sendto()
  • How to use discontinuous range in SUMIF
  • Reading a line from file in C, dynamically
  • Display p-values under 0.1 in r stargazer
  • vsprintf and vsnprintf [-Wformat-nonliteral] warning on Clang 5.0
  • How can I exit a while(1) based on a user input?
  • How to extract the p-value for the slope from an ols object in R
  • Can we reuse allocated memory
  • Salted sha512 in C, cannot synchronise with Symfony2's FOSUserBundle
  • How to preserve alias property while signing app?
  • why doesn't this visNetwork in R show edge
  • Getting segmentation fault while using malloc
  • PHP socket_read() only gets the first byte of the stream
  • Strings appear exact, but they do not match?
  • OSStatus error -50 (invalid parameters) AudioQueueNewInput recording audio on iOS
  • How to work with AMMediaType for video filters
  • GWT Widget.addHandler
  • How can I prevent the need to copy strings passed to a avr-gcc C++ constructor?
  • How to unpack 32bit integer packed in a QByteArray?
  • allocating memory to an array of string
  • OSX - always hide certain files
  • SAVE attribute needed for Fortran variables when only the C_LOC address is returned to a C program?
  • why overloaded new operator is calling constructor even I am using malloc inside overloading functio
  • Why is the size of this struct 32?
  • output of program is not same as passed argument
  • Convert array of 8 bytes to signed long in C++
  • Akka Routing: Reply's send to router ends up as dead letters
  • Linker errors when using intrinsic function via function pointer
  • LevelDB C iterator
  • unknown Exception android
  • Checking variable from a different class in C#