Click here to Skip to main content
15,898,134 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone,
I am trying to get input string from the keyboard and then store that data so i can use it in other function which i am not able to.can you please point out that what am i doing wrong.
Thank you

What I have tried:

static char get_input(_CLI *const PtrCLI)
{
    char input[1024];
    
    /* get string */
    printf("Enter input data of size less than 1024 bytes and write end to end the data: ");
    fgets(input,1024,stdin);
    
    /* create buffer and copy */
    InData = (char *)malloc( strlen(input) + 1);
    strlcpy(InData, input, 1024);
    return InData;
    
}
Posted
Updated 12-Nov-18 21:31pm

What is the type of InData ?
Assuming it is char *

Your function should be something like (not the best solution but with minimum changes in your code):

#include <string.h>
#include <stdio.h>
#include <stdlib.h>

static char* get_input()
{
   char input[1024];

   /* get string */
   printf("Enter input data of size less than 1024 bytes and write end to end the data: ");
   fgets(input, 1024, stdin);

   /* create buffer and copy */
   char * InData = (char *)malloc(strlen(input) + 1);
   strncpy_s(InData, 1024, input, 1024);
   return InData;
}
 
Share this answer
 
v2
I would mimic the C runtime API (i.e. memory handling is caller responsibility):
C
char * get_input(char * buffer, size_t buffer_size)
{
   /* get string */
   printf("Enter input data of size less than %u bytes and write end to end the data: ", buffer_size);
   return fgets(buffer, buffer_size, stdin);
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900