Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4077206
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T17:31:16+00:00 2026-05-20T17:31:16+00:00

If I want to read in a string of arbitrary length from the command

  • 0

If I want to read in a string of arbitrary length from the command line, what’s the best way of going about it?

At the moment I’m doing this:

char name_buffer [ 80 ];
int chars_read = 0;
while ( ( chars_read < 80 ) && ( !feof( stdin ) ) ) {
   name_buffer [ chars_read ] = fgetc ( stdin );
   chars_read++;
}

But what can I do if the string is longer than 80 characters? Obviously I could just initialise the array to a bigger number but I’m sure there must be a better way to give the array more space using malloc or something?

Any hints would be great.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T17:31:17+00:00Added an answer on May 20, 2026 at 5:31 pm

    Found this somewhere on the net long ago, its really useful:

    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        unsigned int len_max = 128;
        unsigned int current_size = 0;
    
        char *pStr = malloc(len_max);
        current_size = len_max;
    
        printf("\nEnter a very very very long String value:");
    
        if(pStr != NULL)
        {
        int c = EOF;
        unsigned int i =0;
            //accept user input until hit enter or end of file
        while (( c = getchar() ) != '\n' && c != EOF)
        {
            pStr[i++]=(char)c;
    
            //if i reached maximize size then realloc size
            if(i == current_size)
            {
                            current_size = i+len_max;
                pStr = realloc(pStr, current_size);
            }
        }
    
        pStr[i] = '\0';
    
            printf("\nLong String value:%s \n\n",pStr);
            //free it 
        free(pStr);
        pStr = NULL;
    
    
        }
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to read line n1->n2 from file foo.c into the current buffer. I
I hav a string e.g. 2010-09-24 . Now I want this string read as
I want to store a string in memory and read it later: $$->desc.constant->base.id =
I want to read and write from serial using events/interrupts. Currently, I have it
I want to read UTF-8 input in Perl, no matter if it comes from
I want to split a string via String.split(k) where k is an arbitrary string
Is there a simple way, using C#, to open an arbitrary URL, read in
I have this kind of structure that I want to both read and write
I have a String that looks like this String read = 1130:5813|1293:5803|1300:5755|1187:5731| As you
I have a text file that I want to read line by line and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.