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

  • Home
  • SEARCH
  • 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 8945257
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T12:12:28+00:00 2026-06-15T12:12:28+00:00

I wrote a function that reads an unknown length string until Enter is pressed

  • 0

I wrote a function that reads an unknown length string until Enter is pressed and returns a char pointer. When I call the function from inside a switch case, it does not wait for my input.

char *get_paths()
{
    unsigned int length_max = 256; /*Initial length of string*/
    unsigned int current_size; 
    current_size = length_max;

    /* Allocating memory for string input */
    char *tmpStr = malloc(length_max);

    /* Simple check to make sure our pointer is not NULL */
    if(tmpStr != NULL)
    {
        int c = EOF; 
        unsigned int i = 0;

        printf("Enter The EXACT or RELATIVE File Paths Separated by a Space: ");

        /* Accept input until Enter key is pressed or user inserts EOF */
        while((c = getchar()) != '\n' && c != EOF)
        {
            tmpStr[i] = (char)c;
            ++i;

            /* If memory is filled up, reallocate memory with bigger size */
            if(i == current_size)
            {
                current_size = i + length_max;
                /* realloc does magic */
                tmpStr = realloc(tmpStr, current_size); 
            }
        }

        /* A valid string always end with a '\0' */
        tmpStr[i] = '\0';
        printf("Got it: %s \n", tmpStr); /*TODO: REMOVE;; USED FOR TESTING*/
        return tmpStr; 
    }
}

The switch case (I have a char *ptr = NULL out of the switch block):

/*File input*/
case 1:
    ptr = get_filepaths();
break;

Output:

Enter The EXACT or RELATIVE File Paths Separated by a Space: Got it:

  • 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-06-15T12:12:29+00:00Added an answer on June 15, 2026 at 12:12 pm

    The solution I was able to find to ‘somehow’ get away with this issue was to add a getchar() right after the first printf() call. not sure why this works!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a function that reads files from a deferred directory which
I wrote a function that tries to get a y/n (yes/no) answer from a
I wrote a pretty simple function that reads in possible player names and stores
I have a function that reads user input from std::cin, and I want to
I´m trying to make a function that reads elements from an array and distributes
I want to write a function that read line by line from a socket
I wrote a function that scans a tab delimited file of baseball stats. public
So I wrote this function that is given possible numbers, and it has to
i wrote a custom jquery function that accept a callback but not being able
I wrote a simple function, that makes emacs add matching quotes (so when I

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.